-
Notifications
You must be signed in to change notification settings - Fork 0
/
userEvents.go
87 lines (69 loc) · 2.26 KB
/
userEvents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package events
import (
"fmt"
"time"
"github.com/niakr1s/chatty-server/app/models"
)
// UserEvent ...
type UserEvent struct {
models.User
models.Chat
Time models.UnixTime
}
// NewUserEvent ...
func NewUserEvent(username string, chatname string, time time.Time) *UserEvent {
return &UserEvent{User: models.NewUser(username), Chat: models.NewChat(chatname), Time: models.UnixTime(time)}
}
func (ue *UserEvent) String() string {
return fmt.Sprintf("user %s, chat: %s, time: %v", ue.UserName, ue.ChatName, ue.Time)
}
// LoginEvent used to emit when user logins chat globally (ie to db.LoggedDB)
type LoginEvent struct {
*UserEvent
}
// NewLoginEvent ...
func NewLoginEvent(username string, chatname string, time time.Time) *LoginEvent {
return &LoginEvent{NewUserEvent(username, chatname, time)}
}
func (le *LoginEvent) String() string {
return fmt.Sprintf("login: %v", le.UserEvent)
}
// LogoutEvent used to emit when user logouts globally (ie from db.LoggedDB)
type LogoutEvent struct {
*UserEvent
}
// NewLogoutEvent ...
func NewLogoutEvent(username string, chatname string, time time.Time) *LogoutEvent {
return &LogoutEvent{NewUserEvent(username, chatname, time)}
}
func (le *LogoutEvent) String() string {
return fmt.Sprintf("logout: %v", le.UserEvent)
}
// ChatJoinEvent used to emit when user joins chat (ie to db.Chat)
type ChatJoinEvent struct {
*UserEvent
models.UserStatus
}
// NewChatJoinEvent ...
func NewChatJoinEvent(username string, chatname string, time time.Time) *ChatJoinEvent {
return &ChatJoinEvent{UserEvent: NewUserEvent(username, chatname, time)}
}
// WithStatus ...
func (ce *ChatJoinEvent) WithStatus(status models.UserStatus) *ChatJoinEvent {
ce.UserStatus = status
return ce
}
func (ce *ChatJoinEvent) String() string {
return fmt.Sprintf("join chat: %v", ce.UserEvent)
}
// ChatLeaveEvent used to emit when user leaves chat (ie from db.Chat)
type ChatLeaveEvent struct {
*UserEvent
}
// NewChatLeaveEvent ...
func NewChatLeaveEvent(username string, chatname string, time time.Time) *ChatLeaveEvent {
return &ChatLeaveEvent{NewUserEvent(username, chatname, time)}
}
func (ce *ChatLeaveEvent) String() string {
return fmt.Sprintf("leave chat: %v", ce.UserEvent)
}