-
Notifications
You must be signed in to change notification settings - Fork 14
/
talk_session.go
250 lines (197 loc) · 6.18 KB
/
talk_session.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
package dao
import (
"context"
"errors"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/iimeta/iim-client/internal/model/do"
"github.com/iimeta/iim-client/internal/model/entity"
"github.com/iimeta/iim-client/utility/db"
"github.com/iimeta/iim-client/utility/logger"
"github.com/iimeta/iim-client/utility/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"strings"
)
var TalkSession = NewTalkSessionDao()
type TalkSessionDao struct {
*MongoDB[entity.TalkSession]
}
func NewTalkSessionDao(database ...string) *TalkSessionDao {
if len(database) == 0 {
database = append(database, db.DefaultDatabase)
}
return &TalkSessionDao{
MongoDB: NewMongoDB[entity.TalkSession](database[0], do.TALK_SESSION_COLLECTION),
}
}
func (d *TalkSessionDao) List(ctx context.Context, uid int) ([]*entity.TalkSession, []*entity.User, []*entity.Group, error) {
talkSessionList, err := d.Find(ctx, bson.M{"user_id": uid, "is_delete": 0}, "-updated_at")
if err != nil {
return nil, nil, nil, err
}
userReceiverIds := make([]int, 0)
groupReceiverIds := make([]int, 0)
for _, talkSession := range talkSessionList {
if talkSession.TalkType == 1 {
userReceiverIds = append(userReceiverIds, talkSession.ReceiverId)
} else if talkSession.TalkType == 2 {
groupReceiverIds = append(groupReceiverIds, talkSession.ReceiverId)
}
}
userList, err := User.FindUserListByUserIds(ctx, userReceiverIds)
if err != nil {
return nil, nil, nil, err
}
groupList, err := Group.FindGroupListByGroupIds(ctx, groupReceiverIds)
if err != nil {
return nil, nil, nil, err
}
return talkSessionList, userList, groupList, nil
}
// 创建会话列表
func (d *TalkSessionDao) Create(ctx context.Context, create *do.TalkSessionCreate) (*entity.TalkSession, error) {
talkSession, err := d.FindOne(ctx, bson.M{"talk_type": create.TalkType, "user_id": create.UserId, "receiver_id": create.ReceiverId})
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
return nil, err
}
if err != nil && errors.Is(err, mongo.ErrNoDocuments) {
talkSession = &entity.TalkSession{
TalkType: create.TalkType,
UserId: create.UserId,
ReceiverId: create.ReceiverId,
IsRobot: create.IsRobot,
IsTalk: create.IsTalk,
}
if _, err = d.Insert(ctx, &do.TalkSession{
TalkType: talkSession.TalkType,
UserId: talkSession.UserId,
ReceiverId: talkSession.ReceiverId,
IsRobot: talkSession.IsRobot,
IsTalk: talkSession.IsTalk,
}); err != nil {
return nil, err
}
} else {
talkSession.IsTop = 0
talkSession.IsDelete = 0
talkSession.IsDisturb = 0
talkSession.IsRobot = create.IsRobot
talkSession.IsTalk = create.IsTalk
if err = d.UpdateById(ctx, talkSession.Id, &do.TalkSession{
IsTop: talkSession.IsTop,
IsDisturb: talkSession.IsDisturb,
IsDelete: talkSession.IsDelete,
IsRobot: talkSession.IsRobot,
IsTalk: talkSession.IsTalk,
UpdatedAt: gtime.Timestamp(),
}); err != nil {
return nil, err
}
}
return talkSession, nil
}
// 删除会话
func (d *TalkSessionDao) Delete(ctx context.Context, uid int, id string) error {
if err := d.UpdateOne(ctx, bson.M{"_id": id, "user_id": uid}, bson.M{
"is_delete": 1,
}); err != nil {
return err
}
return nil
}
// 会话置顶
func (d *TalkSessionDao) Top(ctx context.Context, top *do.TalkSessionTop) error {
if err := d.UpdateOne(ctx, bson.M{"_id": top.Id, "user_id": top.UserId}, bson.M{
"is_top": util.BoolToInt(top.Type == 1),
}); err != nil {
return err
}
return nil
}
// 会话免打扰
func (d *TalkSessionDao) Disturb(ctx context.Context, disturb *do.TalkSessionDisturb) error {
if err := d.UpdateOne(ctx, bson.M{"user_id": disturb.UserId, "receiver_id": disturb.ReceiverId, "talk_type": disturb.TalkType}, bson.M{
"is_disturb": disturb.IsDisturb,
}); err != nil {
return err
}
return nil
}
// 是否开启会话免打扰
func (d *TalkSessionDao) IsDisturb(ctx context.Context, uid int, receiverId int, talkType int) bool {
talkSession, err := d.FindOne(ctx, bson.M{"user_id": uid, "receiver_id": receiverId, "talk_type": talkType})
if err != nil {
if !errors.Is(err, mongo.ErrNoDocuments) {
logger.Error(ctx, err)
}
return false
}
return talkSession.IsDisturb == 1
}
// 批量添加会话列表
func (d *TalkSessionDao) BatchAddList(ctx context.Context, uid int, values map[string]int) {
ctime := gtime.Timestamp()
sessionList, err := d.Find(ctx, bson.M{"user_id": uid})
if err != nil {
logger.Error(ctx, err)
return
}
sessionMap := util.ToMap(sessionList, func(t *entity.TalkSession) int {
return t.ReceiverId
})
talkSessionList := make([]interface{}, 0)
for k, v := range values {
if v == 0 {
continue
}
value := strings.Split(k, "_")
if len(value) != 2 {
continue
}
if sessionMap[gconv.Int(value[1])] != nil {
continue
}
// 获取机器人信息, 判断是不是机器人 todo
robotInfo, err := Robot.GetRobotByUserId(ctx, gconv.Int(value[1]))
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
logger.Error(ctx, err)
continue
}
talkSession := &do.TalkSession{
TalkType: gconv.Int(value[0]),
UserId: uid,
ReceiverId: gconv.Int(value[1]),
CreatedAt: ctime,
UpdatedAt: ctime,
}
if robotInfo != nil {
talkSession.IsRobot = 1
talkSession.IsTalk = robotInfo.IsTalk
}
talkSessionList = append(talkSessionList, talkSession)
}
if len(talkSessionList) == 0 {
return
}
if _, err := d.Inserts(ctx, talkSessionList); err != nil {
logger.Error(ctx, err)
}
}
func (d *TalkSessionDao) FindBySessionId(ctx context.Context, uid int, receiverId int, talkType int) (string, error) {
talkSession, err := d.FindOne(ctx, bson.M{"user_id": uid, "receiver_id": receiverId, "talk_type": talkType})
if err != nil {
logger.Error(ctx, err)
return "", err
}
return talkSession.Id, nil
}
// 开启/关闭上下文
func (d *TalkSessionDao) OpenContext(ctx context.Context, openContext *do.TalkSessionOpenContext) error {
if err := d.UpdateOne(ctx, bson.M{"user_id": openContext.UserId, "receiver_id": openContext.ReceiverId, "talk_type": openContext.TalkType}, bson.M{
"is_open_context": openContext.IsOpenContext,
}); err != nil {
return err
}
return nil
}