chat

package
v0.0.0-...-897d125 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2018 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EventNameMessageCreated          = "message_created"
	EventNameActiveClientActivated   = "client_activated"
	EventNameActiveClientInactivated = "client_inactivated"
	EventNameRoomCreated             = "room_created"
	EventNameRoomDeleted             = "room_deleted"
	EventNameRoomAddedMember         = "room_added_member"
	EventNameRoomRemovedMember       = "room_removed_member"
	EventNameRoomMessagesReadByUser  = "room_messages_read_by_user"
	EventNameUnknown                 = "unknown"
)
View Source
const (
	MaxRoomMessagesLimit = 50
)

Variables

View Source
var ErrInternalError = errors.New("Internal error")

ErrInternalError is the proxy for the internal error which should not be shown for the client.

HubHandlingEventTypes a list of event types to be handled by the Hub interface.

Functions

func IsNotFoundError

func IsNotFoundError(err error) bool

It returns true when the type of given err is *NotFoundError or NotFoundError, otherwise false.

Types

type CommandService

type CommandService interface {
	// It creates room specified by given actiom.CreateRoom.
	// It returns created Room's ID and InfraError if any.
	CreateRoom(ctx context.Context, m action.CreateRoom) (roomID uint64, err error)

	// It deletes room specified by given actiom message.
	// It returns deleted Room's ID and InfraError if any.
	DeleteRoom(ctx context.Context, m action.DeleteRoom) (roomID uint64, err error)

	// AddRoomMember adds the new room member to the specified room.
	// It returns affected room result and InfraError if any.
	AddRoomMember(ctx context.Context, m action.AddRoomMember) (*result.AddRoomMember, error)

	// RemoveRoomMember removes the room member from the specified room.
	// It returns affected room result and InfraError if any.
	RemoveRoomMember(ctx context.Context, m action.RemoveRoomMember) (*result.RemoveRoomMember, error)

	// Mark that the room messages are read by the specified user.
	// It returns updated room ID and nil, or
	// returns InfraError when the message can not be marked to read.
	ReadRoomMessages(ctx context.Context, m action.ReadMessages) (roomID uint64, err error)

	// Post the message to the specified room.
	// It returns posted message id and nil or InfraError
	// which indicates the message can not be posted.
	PostRoomMessage(ctx context.Context, m action.ChatMessage) (msgID uint64, err error)
}

CommandService is the interface for sending the command to the chat application.

type CommandServiceImpl

type CommandServiceImpl struct {
	// contains filtered or unexported fields
}

CommandServiceImpl provides the usecases for creating/updating/editing/deleting the application data. It implements CommandService interface.

func NewCommandServiceImpl

func NewCommandServiceImpl(repos domain.Repositories, pubsub Pubsub) *CommandServiceImpl

func (*CommandServiceImpl) AddRoomMember

implements AddRoomMember for CommandService interface.

func (*CommandServiceImpl) CancelUpdateService

func (s *CommandServiceImpl) CancelUpdateService()

Stop RunUpdateService(). Multiple calling will occurs panic.

func (*CommandServiceImpl) CreateRoom

func (s *CommandServiceImpl) CreateRoom(ctx context.Context, m action.CreateRoom) (roomID uint64, err error)

It creates room specified by given actiom message. It returns created Room's ID and error if any.

func (*CommandServiceImpl) DeleteRoom

func (s *CommandServiceImpl) DeleteRoom(ctx context.Context, m action.DeleteRoom) (roomID uint64, err error)

It deletes room specified by given actiom message. It returns deleted Room's ID and error if any.

func (*CommandServiceImpl) PostRoomMessage

func (s *CommandServiceImpl) PostRoomMessage(ctx context.Context, m action.ChatMessage) (msgID uint64, err error)

Post the message to the specified room. It returns posted message id and nil or error which indicates the message can not be posted.

func (*CommandServiceImpl) ReadRoomMessages

func (s *CommandServiceImpl) ReadRoomMessages(ctx context.Context, m action.ReadMessages) (uint64, error)

Mark the message is read by the specified user. It returns updated room ID or error when the message can not be marked to read.

func (*CommandServiceImpl) RemoveRoomMember

implements RemoveRoomMember for CommandService interface.

func (*CommandServiceImpl) RunUpdateService

func (s *CommandServiceImpl) RunUpdateService(ctx context.Context)

Run updating service for the domain events. It blocks until calling CancelUpdate() or context is done.

type Conn

type Conn domain.Conn

Conn is exported at the chat package so that the higher layer need not to import domain package.

type EventJSON

type EventJSON struct {
	EventName string      `json:"event"`
	Data      event.Event `json:"data"`
}

EventJSON is a data-transfer-object which represents domain event to sent to the client connection. It implement Event interface.

func NewEventJSON

func NewEventJSON(ev event.Event) EventJSON

func (EventJSON) StreamID

func (e EventJSON) StreamID() event.StreamID

func (EventJSON) Timestamp

func (e EventJSON) Timestamp() time.Time

func (EventJSON) Type

func (EventJSON) Type() event.Type

type EventQueryer

type EventQueryer interface {
	// Find events from the data-store.
	// The returned events are, ordered by older created at
	// and all of after specified after time.
	// It returns NotFoundError if not found.
	FindAllByTimeCursor(ctx context.Context, after time.Time, limit int) ([]event.Event, error)

	// Find events, associated with specified stream ID, from the data-store.
	// The returned events are, ordered by older created at
	// and all of after specified after time.
	// It returns NotFoundError if not found.
	FindAllByStreamID(ctx context.Context, streamID event.StreamID, after time.Time, limit int) ([]event.Event, error)
}

EventQueryer queries events stored in the data-store.

type Hub

type Hub interface {
	// Connect accepts new connection to the hub.
	// If conection is invalid then return error.
	Connect(ctx context.Context, c Conn) error

	// Send sends ActionMessage with the connection which sent the message, to the hub.
	// The Conn is used to verify that the message is exactlly
	// sent by the connected user.
	// The error is sent to given conn when the message is invalid.
	Send(conn Conn, message action.ActionMessage)

	// Disconnect disconnects the given connection from the hub.
	// it will no-operation when non-connected connection is given.
	Disconnect(conn Conn)
}

Hub is a interface for a hub which conmmunicates the event/action messages between the active client connections.

type HubImpl

type HubImpl struct {
	// contains filtered or unexported fields
}

HubImpl accepts any user connections to propagates domain events for those connections. It implements Hub interface.

func NewHubImpl

func NewHubImpl(cmd *CommandServiceImpl) *HubImpl

func (*HubImpl) Connect

func (hub *HubImpl) Connect(ctx context.Context, c Conn) error

Connect new websocket connection to the hub.

func (*HubImpl) Disconnect

func (hub *HubImpl) Disconnect(conn Conn)

Disconnect the given websocket connection from the hub. it will no-operation when non-connected connection is given.

func (*HubImpl) Listen

func (hub *HubImpl) Listen(ctx context.Context)

Start handling messages from the connections and sending events to connections. It will blocks untill called Shutdown() or context is done.

func (*HubImpl) Send

func (hub *HubImpl) Send(conn Conn, message action.ActionMessage)

Send ActionMessage with the connection which sent the message. the connection is used to verify that the message is exactlly sent by the connected user. The error is sent to given conn when the message is invalid.

func (*HubImpl) Shutdown

func (hub *HubImpl) Shutdown()

Stop handling messages from the connections and sending events to connections. Multiple calling will cause panic.

type InfraError

type InfraError struct {
	Cause error
}

InfraError represents error caused on the infrastructure layer. It should be not shown directly for the client side.

It implements error interface.

func NewInfraError

func NewInfraError(msgFormat string, args ...interface{}) *InfraError

NewInfraError create new InfraError with same syntax as fmt.Errorf().

func (InfraError) Error

func (err InfraError) Error() string

type LoginService

type LoginService interface {

	// Login finds authenticated user profile matched with given user name and password.
	// It returns queried user profile and nil when the user is authenticated, or
	// returns nil and NotFoundError when the user is not found.
	Login(ctx context.Context, username, password string) (*queried.AuthUser, error)

	// Logout logouts User specified userID from the chat service.
	Logout(ctx context.Context, userID uint64)
}

LoginService is the interface for the login/logut user.

type LoginServiceImpl

type LoginServiceImpl struct {
	// contains filtered or unexported fields
}

func NewLoginServiceImpl

func NewLoginServiceImpl(users UserQueryer, pubsub Pubsub) *LoginServiceImpl

func (*LoginServiceImpl) Login

func (ls *LoginServiceImpl) Login(ctx context.Context, username, password string) (*queried.AuthUser, error)

func (*LoginServiceImpl) Logout

func (ls *LoginServiceImpl) Logout(ctx context.Context, userID uint64)

type MessageQueryer

type MessageQueryer interface {

	// Find all messages from the room specified by room_id.
	// The returned messages are, ordered by latest created at,
	// all of before specified before time,
	// and the number of messages is limted to less than
	// specified limit.
	// It returns InfraError if infrastructure raise some errors.
	// It returns NotFoundError if not found.
	FindRoomMessagesOrderByLatest(ctx context.Context, roomID uint64, before time.Time, limit int) ([]domain.Message, error)

	// Find all unread messages from the room specified by room_id.
	// The returned messages are, ordered by latest created at,
	// It returns NotFoundError if not found.
	FindUnreadRoomMessages(ctx context.Context, userID, roomID uint64, limit int) (*queried.UnreadRoomMessages, error)
}

MessageQueryer queries messages stored in the data-store.

type NotFoundError

type NotFoundError InfraError

NotFoundError represents error caused on the infrastructure layer but it is specified to that the request data is not found. It can be shown directly for the client side.

It implements error interface.

func NewNotFoundError

func NewNotFoundError(msgFormat string, args ...interface{}) *NotFoundError

NewInfraError create new InfraError with same syntax as fmt.Errorf().

func (NotFoundError) Error

func (err NotFoundError) Error() string

type Pubsub

type Pubsub interface {
	Pub(...event.Event)
	Sub(...event.Type) chan interface{}
}

interface for the publisher/subcriber pattern.

type QueryService

type QueryService interface {
	// It finds authenticated user profile matched with given user name and password.
	// It returns queried user profile and nil when the user is found in the data-store, or
	// returns nil and NotFoundError when the user is not found.
	FindUserByNameAndPassword(ctx context.Context, name, password string) (*queried.AuthUser, error)

	// Find the relational information of user specified by userID.
	// It returns queried result and nil, or nil and NotFoundError if the information is not found.
	FindUserRelation(ctx context.Context, userID uint64) (*queried.UserRelation, error)

	// Find the room information specified by roomID with userID.
	// It returns queried result and nil, or nil and NotFoundError if the information is not found.
	FindRoomInfo(ctx context.Context, userID, roomID uint64) (*queried.RoomInfo, error)

	// Find the messages belonging to the room specified by QueryRoomMessages with userID.
	// It returns queried messages and nil, or nil and InfraError if infrastructure raise some errors.
	FindRoomMessages(ctx context.Context, userID uint64, q action.QueryRoomMessages) (*queried.RoomMessages, error)

	// Find the unread messages belonging to the room specified by QueryUnreadRoomMessages with userID.
	// It returns queried messages and nil, or nil and InfraError if infrastructure raise some errors.
	FindUnreadRoomMessages(ctx context.Context, userID uint64, q action.QueryUnreadRoomMessages) (*queried.UnreadRoomMessages, error)
}

QueryService is the interface for the querying the information from backend datastore.

type QueryServiceImpl

type QueryServiceImpl struct {
	// contains filtered or unexported fields
}

QueryServiceImpl implements QueryService interface.

func NewQueryServiceImpl

func NewQueryServiceImpl(qs *Queryers) *QueryServiceImpl

func (*QueryServiceImpl) FindEventsByTimeCursor

func (s *QueryServiceImpl) FindEventsByTimeCursor(ctx context.Context, after time.Time, limit int) ([]event.Event, error)

TODO event permittion in for user id,

func (*QueryServiceImpl) FindRoomInfo

func (s *QueryServiceImpl) FindRoomInfo(ctx context.Context, userID, roomID uint64) (*queried.RoomInfo, error)

Find detailed room information specified by room ID. It also requires userID to query the information which can be permmited to the user. It returns queried room information and error if not found.

func (*QueryServiceImpl) FindRoomMessages

func (s *QueryServiceImpl) FindRoomMessages(ctx context.Context, userID uint64, q action.QueryRoomMessages) (*queried.RoomMessages, error)

Find messages from specified room. It returns error if infrastructure raise some errors.

func (*QueryServiceImpl) FindUnreadRoomMessages

Find unread messages from specified room. It returns error if infrastructure raise some errors.

func (*QueryServiceImpl) FindUserByNameAndPassword

func (s *QueryServiceImpl) FindUserByNameAndPassword(ctx context.Context, name, password string) (*queried.AuthUser, error)

Find user profile matched with user name and password. It returns queried user profile and nil when found in the data-store. It returns nil and error when the user is not found.

func (*QueryServiceImpl) FindUserRelation

func (s *QueryServiceImpl) FindUserRelation(ctx context.Context, userID uint64) (*queried.UserRelation, error)

Find abstract information associated with the User. It returns queried result and error if the information is not found.

type Queryers

Queryers is just data struct which have some XXXQueryers.

type RoomQueryer

type RoomQueryer interface {
	// Find a room specified by roomID and return it.
	// It returns NotFoundError if not found.
	Find(ctx context.Context, roomID uint64) (domain.Room, error)

	// Find all rooms which user has.
	// It returns NotFoundError if not found.
	FindAllByUserID(ctx context.Context, userID uint64) ([]domain.Room, error)

	// Find room information with specified userID and roomID.
	// It returns NotFoundError if not found.
	FindRoomInfo(ctx context.Context, userID, roomID uint64) (*queried.RoomInfo, error)
}

RoomQueryer queries rooms stored in the data-store.

type UserQueryer

type UserQueryer interface {
	// Find a user specified by userID and return it.
	// It returns NotFoundError if not found.
	Find(ctx context.Context, userID uint64) (domain.User, error)

	// Find a user profile specified by user name and password.
	// It returns error if not found.
	FindByNameAndPassword(ctx context.Context, name, password string) (*queried.AuthUser, error)

	// Find a user related information with userID.
	// It returns queried result or NotFoundError if the information is not found.
	FindUserRelation(ctx context.Context, userID uint64) (*queried.UserRelation, error)
}

UserQueryer queries users stored in the data-store.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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