domain

package
v0.0.0-...-b02505f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScopeActivation        = "activation"
	ScopeAuthentication    = "authentication"
	ScopeActivationTTL     = 15 * time.Minute
	ScopeAuthenticationTTL = 7 * 24 * time.Hour
)

Variables

View Source
var (
	ErrDuplicateEmail = errors.New("duplicate email")
	ErrRecordNotFound = errors.New("record not found")
	ErrEditConflict   = errors.New("edit conflict")
	ErrAlreadyActive  = errors.New("user already active")
	ErrInactive       = errors.New("user inactive")
)
View Source
var (
	RgxEmail      = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
	AnonymousUser = &User{}
)
View Source
var (
	RgxOtp = regexp.MustCompile("^[0-9]{6}$")
)

Functions

func ValidPlainPassword

func ValidPlainPassword(pass string, ev *ErrValidation)

func ValidPlainPasswordWithKey

func ValidPlainPasswordWithKey(pass string, ev *ErrValidation, errKey string)

func ValidateAuthenticationToken

func ValidateAuthenticationToken(token string, ev *ErrValidation)

func ValidateEmail

func ValidateEmail(email string, ev *ErrValidation)

func ValidateFilters

func ValidateFilters(ev *ErrValidation, f *Filter)

func ValidateMessageBody

func ValidateMessageBody(body string, ev *ErrValidation)

func ValidateMessageRcvrID

func ValidateMessageRcvrID(id string, ev *ErrValidation)

func ValidateName

func ValidateName(name string, ev *ErrValidation)

func ValidateOTP

func ValidateOTP(otp string, ev *ErrValidation)

Types

type Conversation

type Conversation struct {
	SenderID   string `json:"-"              db:"sender_id"`
	ReceiverID string `json:"-"              db:"receiver_id"`
	// Below given attributes will only be used on TUI (frontend side)
	UserID    string `json:"userID"          db:"user_id"`
	Username  string `json:"username"        db:"username"`
	UserEmail string `json:"userEmail"       db:"user_email"`
	// status of user other than the currently logged-in user, can be either sender or receiver
	LastOnline *time.Time `json:"lastOnline" db:"last_online"`
	// latest msg to display under user's name in TUI, only used on frontend side
	LatestMsg       *string    `json:"-"`
	LatestMsgSentAt *time.Time `json:"-"`
}

type ConversationRepository

type ConversationRepository interface {
	CreateConversation(ctx context.Context, senderID, receiverID string) error
	GetConversations(ctx context.Context, usrID string) ([]*Conversation, error)
	ConversationExists(ctx context.Context, senderID, receiverID string) (bool, error)
}

type ConversationService

type ConversationService interface {
	CreateConversation(ctx context.Context, senderID, receiverID string) error
	GetConversations(ctx context.Context) ([]*Conversation, error)
	ConversationExists(ctx context.Context, senderID, receiverID string) (bool, error)
}

type ErrValidation

type ErrValidation struct {
	Errors map[string]string
}

func NewErrValidation

func NewErrValidation() *ErrValidation

func (*ErrValidation) AddError

func (e *ErrValidation) AddError(field, message string)

func (ErrValidation) Error

func (ErrValidation) Error() string

implements error interface, so unwrap the error to get the validation errors

func (*ErrValidation) Evaluate

func (e *ErrValidation) Evaluate(ok bool, field, message string)

func (*ErrValidation) HasErrors

func (e *ErrValidation) HasErrors() bool

type Filter

type Filter struct {
	Page         int
	PageSize     int
	Sort         *string
	SafeSortList *[]string
}

func (*Filter) Limit

func (f *Filter) Limit() int

func (*Filter) Offset

func (f *Filter) Offset() int

func (*Filter) SortColumn

func (f *Filter) SortColumn() string

func (*Filter) SortDirection

func (f *Filter) SortDirection() string

type LatestMsgBody

type LatestMsgBody struct {
	Body   *string    `db:"body"`
	SentAt *time.Time `db:"sent_at"`
}

type Message

type Message struct {
	ID          string       `json:"id,omitempty"`
	SenderID    string       `json:"senderID,omitempty"     db:"sender_id"`
	ReceiverID  string       `json:"receiverID,omitempty"   db:"receiver_id"`
	Body        string       `json:"body,omitempty"`
	SentAt      *time.Time   `json:"sent_at,omitempty"      db:"sent_at"`
	DeliveredAt *time.Time   `json:"delivered_at,omitempty" db:"delivered_at"`
	ReadAt      *time.Time   `json:"read_at,omitempty"      db:"read_at"`
	Version     int          `json:"-"`
	Operation   MsgOperation `json:"operation"              db:"operation"`
}

type MessageRepository

type MessageRepository interface {
	GetByID(ctx context.Context, id string, op MsgOperation) (*Message, error)
	GetUnDeliveredMessages(ctx context.Context, rcvrID string, op MsgOperation, c MsgChan) error
	InsertMessage(ctx context.Context, m *Message) error
	DeleteMessage(ctx context.Context, mID string) error
}

type MessageSent

type MessageSent struct {
	ID          *string      `json:"id"`
	ReceiverID  string       `json:"receiverID"`
	Body        *string      `json:"body"`
	SentAt      *time.Time   `json:"sent_at"`
	DeliveredAt *time.Time   `json:"delivered_at"`
	ReadAt      *time.Time   `json:"read_at"`
	Operation   MsgOperation `json:"operation"`
}

func (MessageSent) ValidateMessageSent

func (m MessageSent) ValidateMessageSent() *ErrValidation

TODO: Update this logic

type MessageService

type MessageService interface {
	PopulateMessage(m MessageSent, sndr *User) *Message
	ProcessSentMessages(ctx context.Context, m *Message) error
	GetUnDeliveredMessages(ctx context.Context, c MsgChan) error
	SaveMessage(ctx context.Context, m *Message) error
}

type Metadata

type Metadata struct {
	CurrentPage  int `json:"currentPage,omitempty"`
	PageSize     int `json:"pageSize,omitempty"`
	FirstPage    int `json:"firstPage,omitempty"`
	LastPage     int `json:"lastPage,omitempty"`
	TotalRecords int `json:"totalRecords,omitempty"`
}

func CalculateMetadata

func CalculateMetadata(totalRecords, pageSize, currentPage int) Metadata

type MsgChan

type MsgChan chan *Message

type MsgOperation

type MsgOperation int
const (
	// CreateMsg indicates the sender has sent a new msg
	CreateMsg MsgOperation = iota
	// DeliveredMsg indicates the receiver has received the msg
	DeliveredMsg
	// DeliveredConfirmMsg indicates the sender's acknowledgment for the msg delivery.
	// not to be persisted
	DeliveredConfirmMsg
	// ReadMsg indicates the receiver has read the msg
	ReadMsg
	// ReadConfirmMsg indicates the sender's acknowledgment, for the msg seen.
	// not to be persisted
	ReadConfirmMsg
	// DeleteMsg indicates the sender has deleted this msg
	DeleteMsg
	// DeleteConfirmMsg indicates the receiver's acknowledgment of the deleted message;
	// the receiving side will delete the msg, before sending this confirmation.
	// not to be persisted
	DeleteConfirmMsg
	// OnlineMsg indicates the user is online; a msg with this OP must not be persisted
	OnlineMsg
	// OfflineMsg indicates the user is offline; a msg with this OP must not be persisted
	OfflineMsg
	// TypingMsg indicates the user is typing; a msg with this OP must not be persisted
	TypingMsg
	// SyncConvosMsg tells the users to re-fetch their conversations from the server
	// not to be persisted, as we only want to send this for conversations' online users
	// offline ones will fetch from the server, when the TUI starts
	SyncConvosMsg
)

type Token

type Token struct {
	PlainText string    `json:"plainText"`
	Hash      []byte    `json:"-"`
	UserID    string    `json:"-" db:"user_id"`
	Expiry    time.Time `json:"expiry"`
	Scope     string    `json:"-"`
}

type TokenRepository

type TokenRepository interface {
	Insert(ctx context.Context, token *Token) error
	DeleteAllForUser(ctx context.Context, userID, scope string) error
}

type TokenService

type TokenService interface {
	GenerateToken(ctx context.Context, userID string, scope string) (string, error)
	DeleteAllForUser(ctx context.Context, userID string, scope string) error
}

type User

type User struct {
	ID         string     `json:"id"`
	Name       string     `json:"name"`
	Email      string     `json:"email"`
	Password   []byte     `json:"-"`
	Activated  bool       `json:"-"`
	LastOnline *time.Time `json:"lastOnline,omitempty" db:"last_online"`
	CreatedAt  time.Time  `json:"createdAt"  db:"created_at"`
	Version    int        `json:"-"`
	// Websocket related
	Messages  MsgChan `json:"-"`
	CloseSlow func()  `json:"-"`
}

func (*User) IsAnonymousUser

func (u *User) IsAnonymousUser() bool

type UserAuth

type UserAuth struct {
	Email    string `json:"email"`
	Password string `json:"password"`
}

type UserRegister

type UserRegister struct {
	Name     string `json:"name"`
	Email    string `json:"email"`
	Password string `json:"password"`
}

type UserRepository

type UserRepository interface {
	RegisterUser(ctx context.Context, u *User) (string, error)
	ExistsUser(ctx context.Context, email string) (bool, error)
	GetByUniqueField(ctx context.Context, fieldName, fieldValue string) (*User, error)
	UpdateUser(ctx context.Context, u *User) error
	GetForToken(ctx context.Context, scope string, hash []byte) (*User, error)
	ActivateUser(ctx context.Context, user *User) error
	GetByQuery(ctx context.Context, paramName string, paramValue string, filter Filter) ([]*User, *Metadata, error)
	SetOnlineUsersLastSeen(ctx context.Context, t time.Time) error
}

type UserService

type UserService interface {
	RegisterUser(ctx context.Context, u *UserRegister) (string, error)
	ExistsUser(ctx context.Context, email string) (bool, error)
	GetByUniqueField(ctx context.Context, fieldValue string) (*User, error)
	UpdateUser(ctx context.Context, u *UserUpdate) error
	UpdateUserOnlineStatus(ctx context.Context, usr *User, online bool) error
	GetForToken(ctx context.Context, scope string, plainToken string) (*User, error)
	ActivateUser(ctx context.Context, user *User) error
	AuthenticateUser(ctx context.Context, u *UserAuth) (string, error)
	GetByQuery(ctx context.Context, queryParam string, filter Filter) ([]*User, *Metadata, error)
	SetOnlineUsersLastSeen(ctx context.Context, t time.Time) error
}

type UserUpdate

type UserUpdate struct {
	ID              string  `json:"id"`
	Name            string  `json:"name"`
	Email           string  `json:"email"`
	NewPassword     *string `json:"newPassword"`
	CurrentPassword *string `json:"currentPassword"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL