notifyhub

package
v0.0.0-...-a67b36e Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// indicates that a user has been invited to join an App
	EventAppInvite MessageEvent = iota
	// indicated that a user has been reminded to join an app
	EventAppInviteReminder
	// indicates that the App has changed and can be synced
	EventSyncApp

	EventIsOnline
	EventIsOffline

	// MutationAppInvite maps to the corresponding Vue function
	// when the client socket receives a new message
	MutationAppInvite  VueMutation = "APP_INVITE"
	MutationLoadFromDB VueMutation = "INIT_LOAD"
	MutationIsOnline   VueMutation = "IS_ONLINE"
)

Variables

View Source
var (
	ErrUserInPool      = fmt.Errorf("user already present in pool")
	ErrUserNotFound    = fmt.Errorf("could not find user in pool")
	ErrNoRecevierFound = fmt.Errorf("could not find receiver for message to send to")
	ErrWriteToConn     = fmt.Errorf("could not write to connection")
)

Functions

This section is empty.

Types

type BatchNotification

type BatchNotification struct {
	Mutation VueMutation    `json:"mutation"`
	Messages []Notification `json:"notifications"`
}

type Connection

type Connection struct {
	Conn         *websocket.Conn
	Uuid         string
	Organization string
}

func (*Connection) Health

func (conn *Connection) Health(hub *NotifyHub)

Health checks periodically if the connection is still alive if not conn signals to hub that connection can be removed since the client should never send any messages Health will block at conn.ReadMessage which will interrupt if the connection breaks

type HideEvent

type HideEvent struct {
	UserUuid  string `json:"user_uuid"`
	Timestamp int64  `json:"timestamp"`
}

type IncomingEvent

type IncomingEvent struct {
	UserUuid     string                 `json:"receiver_uuid"`
	Organization string                 `json:"receiver_orgn"`
	Timestamp    int64                  `json:"timestamp"`
	Mutation     VueMutation            `json:"mutation"`
	Event        MessageEvent           `json:"event"`
	Value        map[string]interface{} `json:"value"`
}

type MessageEvent

type MessageEvent int

type Notification

type Notification struct {
	Hidden    bool                   `bson:"hidden"`
	Timestamp int64                  `json:"timestamp"`
	Mutation  VueMutation            `json:"mutation" bson:"mutation"`
	Event     MessageEvent           `json:"event" bson:"event"`
	Value     map[string]interface{} `json:"value" bson:"value"`
}

Message represents a message as it will be streamed to the client socket

type NotifyHub

type NotifyHub struct {
	Notify       chan *IncomingEvent
	RemoveNotify chan *RemoveEvent
	HideNotify   chan *HideEvent

	// key is the organization name the client registered with
	Organizations map[string]*OrganizationPool
	// contains filtered or unexported fields
}

func New

func New(repo repo.Repo) *NotifyHub

New creates a new NotifyHub and starts its run function in a goroutine

func (*NotifyHub) HasRecord

func (hub *NotifyHub) HasRecord(userUuid string) (bool, error)

HasRecords checks if a user already has a record in the database

func (*NotifyHub) Hide

func (hub *NotifyHub) Hide(hideEvent *HideEvent) error

func (*NotifyHub) HideNotification

func (hub *NotifyHub) HideNotification(notification *HideEvent) error

func (*NotifyHub) LookUpAndSend

func (hub *NotifyHub) LookUpAndSend(userUuid string)

LookUpAndSend looks up all stored notifications of a given user and sends them to the hub.Notify channel

func (*NotifyHub) OpenSocket

func (hub *NotifyHub) OpenSocket(ctx context.Context, w http.ResponseWriter, r *http.Request, uuid, orgn string) error

func (*NotifyHub) PersistInitRecord

func (hub *NotifyHub) PersistInitRecord(conn *Connection) error

PersistInitRecord stores an empty UserNotification if the user dose not yet hold a record in the database

func (*NotifyHub) Remove

func (hub *NotifyHub) Remove(removeEvent *RemoveEvent) error

Remove removes one notification form a user notification array

func (*NotifyHub) SaveEvent

func (hub *NotifyHub) SaveEvent(userUuid string, event *IncomingEvent)

func (*NotifyHub) Stop

func (hub *NotifyHub) Stop()

type OrganizationPool

type OrganizationPool []*Connection

func (*OrganizationPool) Add

func (pool *OrganizationPool) Add(newConn *Connection) error

Add adds a new connection to its pool if the connection dose not already exists

func (*OrganizationPool) Broadcast

func (pool *OrganizationPool) Broadcast(msg *IncomingEvent) error

func (*OrganizationPool) BroadcastOnline

func (pool *OrganizationPool) BroadcastOnline(msg *IncomingEvent) error

func (*OrganizationPool) Find

func (pool *OrganizationPool) Find(uuid string) *Connection

func (*OrganizationPool) Length

func (pool *OrganizationPool) Length() int

func (*OrganizationPool) Remove

func (pool *OrganizationPool) Remove(delConn *Connection) error

Remove removes a connection form its pool

func (*OrganizationPool) Send

func (pool *OrganizationPool) Send(receiver string, msg *IncomingEvent) error

Send iterate over the pool and sends the message to the connection which uuid matches with the receiver ones. If not receiver is found Send returns an ErrNoRecevierFound

func (*OrganizationPool) SendBatch

func (pool *OrganizationPool) SendBatch(receiver string, msgs BatchNotification) error

type RemoveEvent

type RemoveEvent struct {
	UserUuid  string `json:"user_uuid"`
	Timestamp int64  `json:"timestamp"`
}

type UserNotifications

type UserNotifications struct {
	UserUuid      string         `bson:"_id"`
	Organization  string         `bson:"organization"`
	Notifications []Notification `bson:"notifications"`
}

Notification represents one notification as it will be stored in the database. Each user will have its own information with a slice of messages

type VueMutation

type VueMutation string

VueMutation represents the actual function names on the vuejs client side. The "mutation" in a message will trigger the function on the client side

Jump to

Keyboard shortcuts

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