-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
191 lines (152 loc) · 4.58 KB
/
messages.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
package main
import (
"encoding/json"
"errors"
"github.com/unixpickle/essentials"
)
const (
// Client messages.
MsgTypeLogin = "login"
MsgTypeRegister = "register"
MsgTypeRegisterVerify = "register_verify"
MsgTypeSetPassword = "set_password"
MsgTypeResetPassword = "reset_password"
MsgTypeLogout = "logout"
MsgTypeLogoutOther = "logout_other"
MsgTypeSetStatus = "set_status"
MsgTypeAddBuddy = "add_buddy"
MsgTypeAcceptRequest = "accept_request"
MsgTypeRemoveBuddy = "remove_buddy"
// Control messages.
MsgTypeRegisterSuccess = "register_success"
MsgTypeRegisterFailure = "register_failure"
MsgTypeLoginSuccess = "login_success"
MsgTypeLoginFailure = "login_failure"
MsgTypeForcedLogout = "forced_logout"
MsgTypeNoSuchEmail = "no_email"
MsgTypeSetPasswordSuccess = "set_password_success"
MsgTypeSetPasswordFailure = "set_password_failure"
// State messages.
MsgTypeFullState = "full_state"
MsgTypeRequestSent = "request_sent"
MsgTypeRequestReceived = "request_received"
MsgTypeAcceptSent = "accept_sent"
MsgTypeRequestAccepted = "request_accepted"
MsgTypeBuddyRemoved = "buddy_removed"
MsgTypeStatusChanged = "status_changed"
)
// A Message is the main unit of information sent between
// the client and the server.
type Message interface {
Type() string
}
type LoginMessage struct {
Email string `json:"email"`
Password string `json:"password"`
}
type RegisterMessage LoginMessage
type RegisterVerifyMessage struct {
Email string `json:"email"`
Token string `json:"token"`
}
type SetPasswordMessage struct {
Email string `json:"email"`
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
}
type ResetPasswordMessage struct {
Email string `json:"email"`
}
type LogoutMessage struct{}
type LogoutOtherMessage struct{}
type SetStatusMessage struct {
UserStatus
}
type AddBuddyMessage ResetPasswordMessage
type AcceptRequestMessage ResetPasswordMessage
type RemoveBuddyMessage ResetPasswordMessage
type LoginSuccessMessage struct{}
type LoginFailureMessage struct {
Message string `json:"message"`
}
type RegisterSuccessMessage struct{}
type RegisterFailureMessage LoginFailureMessage
type ForcedLogoutMessage struct{}
func (*LoginMessage) Type() string {
return MsgTypeLogin
}
func (*RegisterMessage) Type() string {
return MsgTypeRegister
}
func (*RegisterVerifyMessage) Type() string {
return MsgTypeRegisterVerify
}
func (*SetPasswordMessage) Type() string {
return MsgTypeSetPassword
}
func (*ResetPasswordMessage) Type() string {
return MsgTypeResetPassword
}
func (*LogoutMessage) Type() string {
return MsgTypeLogout
}
func (*LogoutOtherMessage) Type() string {
return MsgTypeLogoutOther
}
func (*SetStatusMessage) Type() string {
return MsgTypeSetStatus
}
func (*AddBuddyMessage) Type() string {
return MsgTypeAddBuddy
}
func (*AcceptRequestMessage) Type() string {
return MsgTypeAcceptRequest
}
func (*RemoveBuddyMessage) Type() string {
return MsgTypeRemoveBuddy
}
func (*LoginSuccessMessage) Type() string {
return MsgTypeLoginSuccess
}
func (*LoginFailureMessage) Type() string {
return MsgTypeLoginFailure
}
func (*RegisterSuccessMessage) Type() string {
return MsgTypeRegisterSuccess
}
func (*RegisterFailureMessage) Type() string {
return MsgTypeRegisterFailure
}
func (*ForcedLogoutMessage) Type() string {
return MsgTypeForcedLogout
}
// DecodeMessage decodes a message into its Go type.
func DecodeMessage(msgType string, data []byte) (msg Message, err error) {
defer essentials.AddCtxTo("decode message", &err)
mapping := map[string]Message{
MsgTypeLogin: &LoginMessage{},
MsgTypeRegister: &RegisterMessage{},
MsgTypeRegisterVerify: &RegisterVerifyMessage{},
MsgTypeSetPassword: &SetPasswordMessage{},
MsgTypeResetPassword: &ResetPasswordMessage{},
MsgTypeLogout: &LogoutMessage{},
MsgTypeLogoutOther: &LogoutOtherMessage{},
MsgTypeSetStatus: &SetStatusMessage{},
MsgTypeAddBuddy: &AddBuddyMessage{},
MsgTypeAcceptRequest: &AcceptRequestMessage{},
MsgTypeRemoveBuddy: &RemoveBuddyMessage{},
MsgTypeLoginSuccess: &LoginSuccessMessage{},
MsgTypeLoginFailure: &LoginFailureMessage{},
MsgTypeRegisterSuccess: &RegisterSuccessMessage{},
MsgTypeRegisterFailure: &RegisterFailureMessage{},
MsgTypeForcedLogout: &ForcedLogoutMessage{},
}
if obj, ok := mapping[msgType]; ok {
if err := json.Unmarshal(data, obj); err != nil {
return nil, err
}
return obj, nil
} else {
return nil, errors.New("unknown message type: " + msgType)
}
}