-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
258 lines (206 loc) · 6.61 KB
/
message.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package accord
import (
"time"
)
// Message is a single message in the chat
type Message struct {
MessageID uint64
Timestamp time.Time
// TODO: add "Sender" field, denoting the user, who sent the message.
Content string
}
// ChannelConfigMessage is used in ChannelStreamRequest- and Response
// to initiate and broadcast channel-related changes.
type ChannelConfigMessage struct {
Msg isChannelConfigMessageMsg
}
type isChannelConfigMessageMsg interface {
isChannelConfigMessageMsg()
}
func (*ChannelConfigMessage) isChannelStreamRequestMsg() {}
func (*ChannelConfigMessage) isChannelStreamResponseMsg() {}
func (m *ChannelConfigMessage) getMsg() isChannelConfigMessageMsg {
if m != nil {
return m.Msg
}
return nil
}
func (m *ChannelConfigMessage) getNameMsg() *NameChannelConfigMessage {
if x, ok := m.getMsg().(*NameChannelConfigMessage); ok {
return x
}
return nil
}
func (m *ChannelConfigMessage) getRoleMsg() *RoleChannelConfigMessage {
if x, ok := m.getMsg().(*RoleChannelConfigMessage); ok {
return x
}
return nil
}
func (m *ChannelConfigMessage) getPinMsg() *PinChannelConfigMessage {
if x, ok := m.getMsg().(*PinChannelConfigMessage); ok {
return x
}
return nil
}
type NameChannelConfigMessage struct {
NewChannelName string
}
func (*NameChannelConfigMessage) isChannelConfigMessageMsg() {}
type RoleChannelConfigMessage struct {
Username string
Role Role
}
func (*RoleChannelConfigMessage) isChannelConfigMessageMsg() {}
type PinChannelConfigMessage struct {
MessageID uint64
}
func (*PinChannelConfigMessage) isChannelConfigMessageMsg() {}
// ChannelStreamRequestType is a type of channel stream request message.
type ChannelStreamRequestType int
const (
// FromUserChannelStreamRequestType is for sending new messages, and updating or
// deletion of messages.
FromUserChannelStreamRequestType ChannelStreamRequestType = iota
// ChannelConfigChannelStreamRequestType carries channel configuration changes,
// including name change, updating user roles, and specifying pinned message.
ChannelConfigChannelStreamRequestType
)
// ChannelStreamRequest represents a stream request for a single channel.
type ChannelStreamRequest struct {
ChannelID uint64
Msg isChannelStreamRequestMsg
}
type isChannelStreamRequestMsg interface {
isChannelStreamRequestMsg()
}
func (m *ChannelStreamRequest) GetChannelID() uint64 {
if m != nil {
return m.ChannelID
}
return 0
}
func (m *ChannelStreamRequest) GetMsg() isChannelStreamRequestMsg {
if m != nil {
return m.Msg
}
return nil
}
func (x *ChannelStreamRequest) GetUserMsg() *UserChannelStreamRequest {
if x, ok := x.GetMsg().(*UserChannelStreamRequest); ok {
return x
}
return nil
}
func (x *ChannelStreamRequest) GetConfMsg() *ChannelConfigMessage {
if x, ok := x.GetMsg().(*ChannelConfigMessage); ok {
return x
}
return nil
}
// ChannelStreamResponseType is a type of channel stream response message.
type ChannelStreamResponseType int
const (
// FromUserChannelStreamResponseType is for sending new messages, and updating or
// deletion of messages.
FromUserChannelStreamResponseType ChannelStreamResponseType = iota
// ChannelConfigChannelStreamResponseType carries channel configuration changes,
// including name change, updating user roles, and specifying pinned message.
ChannelConfigChannelStreamResponseType
)
type ChannelStreamResponse struct {
Msg isChannelStreamResponseMsg
}
type isChannelStreamResponseMsg interface {
isChannelStreamResponseMsg()
}
// UserChannelStreamRequest is a stream message sent by one of the users to the channel.
type UserChannelStreamRequest struct {
UserMsg isUserChannelStreamRequestUserMsg
}
type isUserChannelStreamRequestUserMsg interface {
isUserChannelStreamRequestUserMsg()
}
func (*UserChannelStreamRequest) isChannelStreamRequestMsg() {}
type NewMessageUserChannelStreamRequest struct {
Content string
}
func (*NewMessageUserChannelStreamRequest) isUserChannelStreamRequestUserMsg() {}
type EditMessageUserChannelStreamRequest struct {
MessageID uint64
Content string
}
func (*EditMessageUserChannelStreamRequest) isUserChannelStreamRequestUserMsg() {}
type DeleteMessageUserChannelStreamRequest struct {
MessageID uint64
}
func (*DeleteMessageUserChannelStreamRequest) isUserChannelStreamRequestUserMsg() {}
func (m *UserChannelStreamRequest) getUserMsg() isUserChannelStreamRequestUserMsg {
if m != nil {
return m.UserMsg
}
return nil
}
func (m *UserChannelStreamRequest) getNewUserMsg() *NewMessageUserChannelStreamRequest {
if x, ok := m.getUserMsg().(*NewMessageUserChannelStreamRequest); ok {
return x
}
return nil
}
func (m *UserChannelStreamRequest) getEditUserMsg() *EditMessageUserChannelStreamRequest {
if x, ok := m.getUserMsg().(*EditMessageUserChannelStreamRequest); ok {
return x
}
return nil
}
func (m *UserChannelStreamRequest) getDeleteUserMsg() *DeleteMessageUserChannelStreamRequest {
if x, ok := m.getUserMsg().(*DeleteMessageUserChannelStreamRequest); ok {
return x
}
return nil
}
// UserChannelStreamResponse is a stream message broadcasted to all users in the channel.
type UserChannelStreamResponse struct {
MessageID uint64
UserMsg isUserChannelStreamResponseUserMsg
}
type isUserChannelStreamResponseUserMsg interface {
isUserChannelStreamResponseUserMsg()
}
func (*UserChannelStreamResponse) isChannelStreamResponseMsg() {}
type NewAndUpdateMessageUserChannelStreamResponse struct {
Timestamp time.Time
Content string
}
func (*NewAndUpdateMessageUserChannelStreamResponse) isUserChannelStreamResponseUserMsg() {}
type DeleteMessageUserChannelStreamResponse struct{}
func (*DeleteMessageUserChannelStreamResponse) isUserChannelStreamResponseUserMsg() {}
// GetMessageID returns message ID or the user stream response. It returns 0
// if the user stream response is nil.
func (m *UserChannelStreamResponse) GetMessageID() uint64 {
if m != nil {
return m.MessageID
}
return 0
}
// GetUserMsg returns the user message contained in the user channel stream response.
func (m *UserChannelStreamResponse) GetUserMsg() isUserChannelStreamResponseUserMsg {
if m != nil {
return m.UserMsg
}
return nil
}
// GetNewAndUpdateUserMsg gets the new and update response messages from some user.
func (m *UserChannelStreamResponse) GetNewAndUpdateUserMsg() *NewAndUpdateMessageUserChannelStreamResponse {
if x, ok := m.GetUserMsg().(*NewAndUpdateMessageUserChannelStreamResponse); ok {
return x
}
return nil
}
// GetDeleteUserMsg gets the deleted message in user response message.
func (m *UserChannelStreamResponse) GetDeleteUserMsg() *DeleteMessageUserChannelStreamResponse {
if x, ok := m.GetUserMsg().(*DeleteMessageUserChannelStreamResponse); ok {
return x
}
return nil
}