-
Notifications
You must be signed in to change notification settings - Fork 14
/
handler.go
224 lines (210 loc) · 5.9 KB
/
handler.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
package main
import (
"context"
"fmt"
"strconv"
"time"
"toktik/constant/biz"
"toktik/constant/config"
"toktik/kitex_gen/douyin/wechat"
"toktik/logging"
"toktik/service/wechat/db"
"github.com/golang/protobuf/proto"
"github.com/redis/go-redis/extra/redisotel/v9"
"github.com/redis/go-redis/v9"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/encoding/protojson"
)
var rdb *redis.Client
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: config.EnvConfig.REDIS_ADDR,
Password: config.EnvConfig.REDIS_PASSWORD,
DB: config.EnvConfig.REDIS_DB,
})
if err := redisotel.InstrumentTracing(rdb); err != nil {
panic(err)
}
if err := redisotel.InstrumentMetrics(rdb); err != nil {
panic(err)
}
}
// WechatServiceImpl implements the last service interface defined in the IDL.
type WechatServiceImpl struct{}
// generateKey generates a unique key for each pair of users
func (s *WechatServiceImpl) generateKey(sender, receiver *uint32) *string {
if sender == nil || receiver == nil {
key := fmt.Sprintf("chat:%d:%d", 0, 0)
return &key
}
var key string
if *sender < *receiver {
key = fmt.Sprintf("chat:%d:%d", *sender, *receiver)
} else {
key = fmt.Sprintf("chat:%d:%d", *receiver, *sender)
}
return &key
}
// WechatAction implements the WechatServiceImpl interface.
func (s *WechatServiceImpl) WechatAction(ctx context.Context, req *wechat.MessageActionRequest) (resp *wechat.MessageActionResponse, err error) {
actionTime := time.Now()
logger := logging.Logger.WithFields(logrus.Fields{
"actionTime": actionTime,
"method": "WechatAction",
})
logger.Debugf("Process start")
if req == nil {
logger.Warningf("request is nil")
return &wechat.MessageActionResponse{
StatusCode: biz.RequestIsNil,
StatusMsg: "request is nil",
}, nil
}
senderID := req.SenderId
receiverID := req.ReceiverId
// 0 means chat with GPT
if receiverID == 0 {
err := s.handleChatGPT(ctx, senderID, req.Content, false, actionTime.UnixMilli())
if err != nil {
logger.Warningf("handleChatGPT error: %v", err)
return &wechat.MessageActionResponse{
StatusCode: biz.ServiceNotAvailable,
StatusMsg: err.Error(),
}, nil
}
return &wechat.MessageActionResponse{
StatusCode: 0,
StatusMsg: "success",
}, nil
}
msg := &db.ChatMessage{
From: senderID,
To: receiverID,
Msg: req.Content,
Time: actionTime.UnixMilli(),
}
msgStr, err := proto.Marshal(msg)
if err != nil {
logger.Warningf("proto marshal error: %v", err)
resp = &wechat.MessageActionResponse{
StatusCode: biz.ProtoMarshalError,
StatusMsg: err.Error(),
}
return
}
key := *s.generateKey(&senderID, &receiverID)
cmd := rdb.ZAdd(ctx, key, redis.Z{
Score: float64(actionTime.UnixMilli()),
Member: msgStr,
})
if cmd.Err() != nil {
logger.Warningf("redis error: %v", cmd.Err())
resp = &wechat.MessageActionResponse{
StatusCode: biz.RedisError,
StatusMsg: cmd.Err().Error(),
}
return
}
resp = &wechat.MessageActionResponse{
StatusCode: 0,
StatusMsg: "success",
}
logger.WithFields(logrus.Fields{
"sender_id": senderID,
"receiver_id": receiverID,
"content": req.Content,
"cost_time": time.Since(actionTime).Milliseconds(),
}).Debugf("Process end")
return
}
// WechatChat implements the WechatServiceImpl interface.
func (s *WechatServiceImpl) WechatChat(ctx context.Context, req *wechat.MessageChatRequest) (resp *wechat.MessageChatResponse, err error) {
logger := logging.Logger.WithFields(logrus.Fields{
"method": "WechatChat",
})
logger.Debugf("Process start")
if req == nil {
logger.Warningf("request is nil")
return &wechat.MessageChatResponse{
StatusCode: biz.RequestIsNil,
StatusMsg: "request is nil",
}, nil
}
senderID := req.SenderId
receiverID := req.ReceiverId
min := strconv.FormatInt(req.PreMsgTime+1000, 10)
max := "+inf"
key := *s.generateKey(&senderID, &receiverID)
zRangeCMD := rdb.ZRangeByScore(ctx, key, &redis.ZRangeBy{Min: fmt.Sprintf("%s", min), Max: max})
if zRangeCMD.Err() != nil {
logger.Warningf("redis lrange error: %v", zRangeCMD.Err())
resp = &wechat.MessageChatResponse{
StatusCode: biz.RedisError,
StatusMsg: zRangeCMD.Err().Error(),
}
return
}
respMessageList := make([]*wechat.Message, 0)
messages := zRangeCMD.Val()
for i, message := range messages {
msg := &db.ChatMessage{}
err = proto.Unmarshal([]byte(message), msg)
var content string
if err != nil {
content = fmt.Sprintf("%s", message)
logger.Warningf("proto unmarshal error: %v", err)
} else {
content = msg.Msg
}
respMsg := &wechat.Message{
Id: uint32(i),
Content: content,
CreateTime: msg.Time,
FromUserId: &msg.From,
ToUserId: &msg.To,
}
if receiverID == 0 {
// ChatGPT logic
s.receiveChatGPT(respMsg, senderID)
}
respMessageList = append(respMessageList, respMsg)
}
resp = &wechat.MessageChatResponse{
StatusCode: 0,
StatusMsg: "success",
MessageList: respMessageList,
}
logger.WithFields(logrus.Fields{
"message_list": respMessageList,
}).Debugf("Process end")
return resp, nil
}
func (s *WechatServiceImpl) receiveChatGPT(respMsg *wechat.Message, senderID uint32) {
respMsg.CreateTime = time.Now().UnixMilli() + 3000
*respMsg.FromUserId = 0
*respMsg.ToUserId = senderID
}
func (s *WechatServiceImpl) handleChatGPT(ctx context.Context, senderID uint32, content string, resetSession bool, time int64) (err error) {
logger := logging.Logger.WithFields(logrus.Fields{
"time": time,
"method": "handleChatGPT",
})
logger.Debugf("Process start")
chatGPTMessage := db.ChatGPTMessage{
SenderId: senderID,
Msg: content,
ResetSession: resetSession,
Time: time,
}
msgStr, err := protojson.Marshal(&chatGPTMessage)
if err != nil {
logger.Warningf("proto marshal error: %v", err)
return err
}
cmd := rdb.Publish(ctx, "chatgpt", msgStr)
if cmd.Err() != nil {
logger.Warningf("redis publish error: %v", cmd.Err())
return cmd.Err()
}
return
}