-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverAPI.go
223 lines (178 loc) · 4.44 KB
/
serverAPI.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
package main
import (
"fmt"
"time"
)
func initDataStore() DbManager {
var dataStorageInterface DbManager
var inMem = InMemory{
Users: make(map[UserId]User),
GlobalChatRooms: []ChatRoomId{},
HistoryMessagePerChatRoom: []ChatRoom{},
}
dataStorageInterface = inMem
return dataStorageInterface
}
var globalDataStore = initDataStore()
func getDataStore() DbManager {
return globalDataStore
}
type User struct {
Uid UserId
Uname string
JoinedChatRoom []ChatRoomId
}
type ChatRoom struct {
Cid ChatRoomId
Cname string
HistoryMessages []string
}
type UserId struct {
Id int
}
type ChatRoomId struct {
Id int
}
type CreateUserRequest struct {
UserName string
}
type CreateUserResponse struct {
Uid UserId
}
//CreateUser API create an user with id when user name is provided.
func CreateUser(req CreateUserRequest) CreateUserResponse {
userName := req.UserName
var userCreated User
fmt.Println(userName)
//TODO: do sth to userName to create user id...
userCreated.Uid.Id = -1
var res CreateUserResponse
res.Uid = userCreated.Uid
return res
}
type GetChatRoomListRequest struct {
}
type GetChatRoomListResponse struct {
ChatRoomList []ChatRoomId
}
//GetChatRoomList provide the global list of all chat rooms on the server.
func GetChatRoomList(req GetChatRoomListRequest) GetChatRoomListResponse {
var chatRoomList []ChatRoomId
chatRoomList = nil
var res GetChatRoomListResponse
res.ChatRoomList = chatRoomList
return res
}
type JoinChatRoomRequest struct {
Uid UserId
Cid ChatRoomId
CreateIfNotExist bool
}
type JoinChatRoomResponse struct {
Success bool
JoinedChatRooms []ChatRoomId
HistoryMessages []string
}
//JoinChatRoom put specific user into requested chat room,
//provide the history chat in that requested chat room.
func JoinChatRoom(req JoinChatRoomRequest) JoinChatRoomResponse {
uid := req.Uid
cid := req.Cid
createIfNotExist := req.CreateIfNotExist
fmt.Println(uid, cid, createIfNotExist)
var res JoinChatRoomResponse
res.Success = false
res.JoinedChatRooms = nil
res.HistoryMessages = nil
return res
}
type LeaveChatRoomRequest struct {
Uid UserId
Cid ChatRoomId
}
type LeaveChatRoomResponse struct {
Success bool
JoinedChatRooms []ChatRoomId
LeavingTime time.Time
}
//LeaveChatRoom removes requested user from the specific chat room.
func LeaveChatRoom(req LeaveChatRoomRequest) LeaveChatRoomResponse {
uid := req.Uid
cid := req.Cid
fmt.Println(uid, cid)
var res LeaveChatRoomResponse
res.Success = false
res.LeavingTime = time.Now()
res.JoinedChatRooms = nil
return res
}
type SendMessageRequest struct {
SenderId UserId
ReceiverId UserId
Message string
}
type SendMessageResponse struct {
Success bool
SentTime time.Time
}
//SendMessage send defined message from sender to receiver. Receiver can be either an user or chat room.
func SendMessage(req SendMessageRequest) SendMessageResponse {
senderId := req.SenderId
receiverId := req.ReceiverId
msg := req.Message
fmt.Println(senderId, receiverId, msg)
var res SendMessageResponse
res.Success = false
res.SentTime = time.Now()
return res
}
type LoginRequest struct {
UserName string
}
type LoginResponse struct {
Success bool
Uid UserId
JoinedChatRoom []ChatRoom
}
//Login lets specific user login into the chat service.
func Login(req LoginRequest) LoginResponse {
userName := req.UserName
loginTime := time.Now()
fmt.Println(userName, loginTime)
dataStore := getDataStore()
uid := dataStore.GetUser(userName)
if uid.Id == 0 {
uid = dataStore.SaveUser(userName)
}
//joinedRoomIDs := dataStore.GetUserChatRoom(uid)
chatRooms := dataStore.GetHistoryMessage(uid)
var chatRoomInfo []ChatRoom
for _, chatRoom := range chatRooms {
var chatRoomItem ChatRoom
chatRoomItem.Cid = chatRoom.Cid
chatRoomItem.Cname = chatRoom.Cname
chatRoomItem.HistoryMessages = chatRoom.HistoryMessages
chatRoomInfo = append(chatRoomInfo, chatRoomItem)
}
var res LoginResponse
res.Success = true
res.Uid = uid
res.JoinedChatRoom = chatRoomInfo
return res
}
type LogoutRequest struct {
Uid UserId
LogoutTime time.Time
}
type LogoutResponse struct {
Success bool
}
//Logout lets specific user logout from the chat service.
func Logout(req LogoutRequest) LogoutResponse {
uid := req.Uid
logoutTime := req.LogoutTime
fmt.Println(uid, logoutTime)
var res LogoutResponse
res.Success = false
return res
}