/
grpc.go
191 lines (147 loc) · 4.8 KB
/
grpc.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 web
import (
"context"
"fmt"
"time"
"encoding/json"
pb "github.com/hawkwithwind/chat-bot-hub/proto/web"
"github.com/hawkwithwind/chat-bot-hub/server/chatbothub"
"github.com/hawkwithwind/chat-bot-hub/server/domains"
"github.com/hawkwithwind/chat-bot-hub/server/utils"
)
func (server *WebServer) GetChatUser(_ context.Context, req *pb.GetChatUserRequest) (*pb.GetChatUserResponse, error) {
o := ErrorHandler{}
tx := o.Begin(server.db)
if o.Err != nil {
return nil, o.Err
}
defer o.CommitOrRollback(tx)
user := o.GetChatUserByName(tx, req.Type, req.UserName)
if user == nil {
return nil, fmt.Errorf("can not find user: %s %s", req.UserName, req.Type)
}
response := &pb.GetChatUserResponse{}
payload, _ := json.Marshal(user)
response.Payload = payload
return response, nil
}
func (server *WebServer) GetChatUserSync(_ context.Context, req *pb.GetChatUserSyncRequest) (*pb.GetChatUserResponse, error) {
o := ErrorHandler{}
// 1. do same thing with getchatuser, if not null, return
tx := o.Begin(server.db)
if o.Err != nil {
return nil, o.Err
}
defer o.CommitOrRollback(tx)
user := o.GetChatUserByName(tx, req.Type, req.UserName)
if o.Err != nil {
return nil, o.Err
}
if user != nil {
server.Info("[sync get chatuser debug] get chatuser by db")
response := &pb.GetChatUserResponse{}
payload, _ := json.Marshal(user)
response.Payload = payload
return response, nil
}
// 2. if get null from db call get contacts
if len(req.BotLogin) == 0 {
return nil, fmt.Errorf(
"can not find user: %s %s, botLogin is null", req.UserName, req.Type)
}
wrapper, err := server.NewGRPCWrapper()
if err != nil {
return nil, err
}
defer wrapper.Cancel()
ar := o.NewActionRequest(req.BotLogin, chatbothub.GetContact,
o.ToJson(map[string]interface{}{
"userId": req.UserName,
}), "NEW")
if o.Err != nil {
return nil, o.Err
}
server.Info("[sync get chatuser debug] call grpc")
actionReply := o.CreateAndRunAction(server, ar)
if o.Err != nil {
return nil, o.Err
}
if actionReply.ClientError != nil && actionReply.ClientError.Code != 0 {
return nil, utils.NewClientError(
utils.ClientErrorCode(actionReply.ClientError.Code),
fmt.Errorf(actionReply.ClientError.Message))
}
// 3. wait for async return
ch := make(chan domains.ChatUser)
go server.contactInfoDispatcher.Listen(req.UserName, ch)
server.Info("[sync get chatuser debug] wait for reply")
select {
case chatuser := <-ch:
server.Info("[sync get chatuser debug] get reply from channel")
return &pb.GetChatUserResponse{
Payload: []byte(o.ToJson(chatuser)),
}, nil
case <-time.After(3 * time.Second):
server.Info("[sync get chatuser debug] wait reply timeout")
return nil, fmt.Errorf(
"cannot find user: %s %s %s, getcontact timeout",
req.UserName, req.Type, req.BotLogin)
}
}
func (server *WebServer) GetBot(_ context.Context, req *pb.GetBotRequest) (*pb.GetBotResponse, error) {
o := ErrorHandler{}
tx := o.Begin(server.db)
if o.Err != nil {
return nil, o.Err
}
defer o.CommitOrRollback(tx)
bot := o.GetBotById(tx, req.BotId)
if o.Err != nil {
return nil, o.Err
}
response := &pb.GetBotResponse{}
payload, _ := json.Marshal(bot)
response.Payload = payload
return response, nil
}
func (server *WebServer) ValidateToken(_ context.Context, req *pb.ValidateTokenRequest) (*pb.ValidateTokenResponse, error) {
o := ErrorHandler{}
user := o.ValidateJWTToken(server.Config.SecretPhrase, req.Token)
if o.Err != nil {
return nil, o.Err
}
response := &pb.ValidateTokenResponse{}
payload, _ := json.Marshal(user)
response.Payload = payload
return response, nil
}
func (server *WebServer) GetBotChatRooms(ctx context.Context, request *pb.GetBotChatRoomsRequest) (*pb.GetBotChatRoomsResponse, error) {
o := &domains.ErrorHandler{}
chatRooms := o.GetChatRooms(server.messageDb, request.BotIds, request.ChatType, request.FromRoomId, request.Limit)
if o.Err != nil {
return nil, o.Err
}
response := &pb.GetBotChatRoomsResponse{}
response.Items = chatRooms
return response, nil
}
func (server *WebServer) GetBotChatRoom(ctx context.Context, request *pb.GetBotChatRoomRequest) (*pb.GetBotChatRoomResponse, error) {
o := &ErrorHandler{}
response := &pb.GetBotChatRoomResponse{}
room := o.GetChatRoomWithPeerId(server.messageDb, request.BotId, request.PeerId)
if room != nil {
response.ChatRoom = room
} else if request.CreateIfNotExist {
response.ChatRoom = o.CreateChatRoom(server.messageDb, request.BotId, request.PeerId)
}
return response, o.Err
}
func (server *WebServer) UpdateBotChatRoom(ctx context.Context, request *pb.UpdateBotChatRoomRequest) (*pb.UpdateBotChatRoomResponse, error) {
o := &ErrorHandler{}
o.UpdateOrCreateChatRoom(server.messageDb, request.BotId, request.PeerId)
if o.Err != nil {
return nil, o.Err
}
response := &pb.UpdateBotChatRoomResponse{}
return response, nil
}