Skip to content

Commit a4edf4f

Browse files
committed
1. 增加 /api/v1/chat/message/last 接口
2. 修改workflows文件,增加触发docs自动编译
1 parent a9742c5 commit a4edf4f

File tree

14 files changed

+386
-323
lines changed

14 files changed

+386
-323
lines changed

.github/workflows/cross-build.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989
bin_arrays=($(ls jim-*))
9090
for ((i=0; i<${#bin_arrays[@]}; i++)); do
9191
zip_name=$(echo ${bin_arrays[$i]}|awk -F '-' '{print $2"-"$3}'|awk -F'.' '{print $1}')
92-
zip -r ${zip_name}.zip ./${bin_arrays[$i]} ./config/ ./sql/
92+
zip -r ${zip_name}-${{ github.event.inputs.version}}.zip ./${bin_arrays[$i]} ./config/ ./sql/
9393
done
9494
echo "status=success" >> $GITHUB_OUTPUT
9595
@@ -124,6 +124,14 @@ jobs:
124124
git push "https://${{ secrets.ACCESS_TOKEN }}@github.com/jerbe/jim-docs.git" HEAD:main
125125
echo "推送标签(tag)"
126126
git push --force origin ${{ github.event.inputs.version }}
127+
128+
curl -L \
129+
-X POST \
130+
-H "Accept: application/vnd.github+json" \
131+
-H "Authorization: Bearer ${{ secrets.ACCESS_TOKEN }}" \
132+
-H "X-GitHub-Api-Version: 2022-11-28" \
133+
https://api.github.com/repos/jerbe/jim-docs/actions/workflows/build.yml/dispatches \
134+
-d '{"ref":"main","inputs":{"version":"${{ github.event.inputs.version }} "}}'
127135
else
128136
echo "没有东西需要提交"
129137
exit 0

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Here are the planned features:
115115
### Chat
116116
- [ ] Chat List
117117
- [ ] Pin chats
118-
- [ ] Last message in chat list
118+
- [x] Last message in chat room
119119
- [x] Private Chat
120120
- [x] Send plain text
121121
- [ ] Send images

README_zh.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ JIM 是一个轻量的聊天系统,
107107
### 聊天
108108
- [ ] 聊天列表
109109
- [ ] 置顶聊天
110-
- [ ] 聊天列表最后一条消息
110+
- [x] 聊天房间的最后消息
111111
- [x] 私聊
112112
- [x] 纯文本聊天
113113
- [ ] 发送图片

database/chat.go

100644100755
+46-34
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ type ChatMessage struct {
127127
SessionType int `bson:"session_type" json:"session_type"`
128128

129129
// 发送人ID
130-
SenderID int64 `bson:"sender_id"`
130+
SenderID int64 `bson:"sender_id" json:"sender_id"`
131131

132132
// 接收人ID
133-
ReceiverID int64 `bson:"receiver_id"`
133+
ReceiverID int64 `bson:"receiver_id" json:"receiver_id"`
134134

135135
// 消息发送状态,1-已发送,2-未抵达,3-已抵达
136136
SendStatus int `bson:"send_status" json:"send_status"`
@@ -278,10 +278,10 @@ func AddChatMessage(msg *ChatMessage) error {
278278
err = jcache.Push(GlobCtx, cacheKey, msg)
279279
if err != nil && err.Error() == "WRONGTYPE Operation against a key holding the wrong kind of value" {
280280
jcache.Del(GlobCtx, cacheKey)
281-
err = jcache.Push(GlobCtx, cacheKey, msg)
282-
if err != nil {
283-
log.Warn().Err(err).Msg("缓存插入聊天消息失败")
284-
}
281+
}
282+
283+
if err == nil {
284+
jcache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
285285
}
286286

287287
return nil
@@ -326,12 +326,6 @@ func AddChatMessageTx(msg *ChatMessage) error {
326326
return nil, errors.Wrap(err)
327327
}
328328

329-
// 将聊天数据推送到缓存定长队列中去
330-
err = jcache.Push(GlobCtx, cacheKeyFormatLastMessageList(msg.RoomID, msg.SessionType), msg)
331-
if err != nil {
332-
log.Error().Err(err).Msgf("推送消息到缓存列表失败:\n %+v", err)
333-
}
334-
335329
// 更新聊天室的最后一条消息
336330
_, err = db.Collection(CollectionRoom).
337331
UpdateOne(ctxb, bson.M{
@@ -344,6 +338,18 @@ func AddChatMessageTx(msg *ChatMessage) error {
344338
if err != nil {
345339
return nil, errors.Wrap(err)
346340
}
341+
342+
// 将聊天数据推送到缓存定长队列中去
343+
cacheKey := cacheKeyFormatLastMessageList(msg.RoomID, msg.SessionType)
344+
err = jcache.Push(GlobCtx, cacheKey, msg)
345+
if err != nil {
346+
jcache.Del(GlobCtx, cacheKey)
347+
log.Error().Err(err).Msgf("推送消息到缓存列表失败:\n %+v", err)
348+
}
349+
if err == nil {
350+
jcache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
351+
}
352+
347353
return nil, nil
348354
}, options.Transaction().SetWriteConcern(writeconcern.Majority()).SetReadConcern(readconcern.Snapshot()))
349355
if err != nil {
@@ -416,38 +422,40 @@ func NewChatMessageListOptions() *GetChatMessageListOptions {
416422
}
417423
}
418424

425+
type GetChatMessageListFilter struct {
426+
RoomID string `bson:"room_id"`
427+
SessionType int `bson:"session_type"`
428+
Sort any `bson:"sort"`
429+
LastMessageID *int64 `bson:"last_message_id"`
430+
Limit *int `bson:"limit"`
431+
}
432+
419433
// GetChatMessageList 获取消息列表
420-
func GetChatMessageList(roomID string, sessionType int, opts ...*GetChatMessageListOptions) ([]*ChatMessage, error) {
421-
opt := NewChatMessageListOptions()
434+
func GetChatMessageList(filter *GetChatMessageListFilter, opts ...*GetOptions) ([]*ChatMessage, error) {
435+
if filter.Sort == nil {
436+
filter.Sort = bson.M{"message_id": -1}
437+
}
422438

423-
for _, o := range opts {
424-
if opt.Sort == nil && o.Sort != nil {
425-
opt.Sort = o.Sort
426-
}
427-
if o.LastMessageID > opt.LastMessageID {
428-
opt.LastMessageID = o.LastMessageID
429-
}
430-
if o.Limit > opt.Limit {
431-
opt.Limit = o.Limit
439+
if filter.LastMessageID == nil {
440+
filter.LastMessageID = new(int64)
441+
}
432442

433-
if opt.Limit > defaultMaxLimit {
434-
opt.Limit = defaultMaxLimit
435-
}
436-
}
443+
if filter.Limit == nil {
444+
filter.Limit = new(int)
437445
}
438446

439447
rs, err := GlobDB.Mongo.Database(DatabaseMongodbIM).
440448
Collection(CollectionMessage).
441449
Find(GlobCtx, bson.M{
442-
"room_id": roomID,
443-
"session_type": sessionType,
450+
"room_id": filter.RoomID,
451+
"session_type": filter.SessionType,
444452
"message_id": bson.M{
445-
"$gte": opt.LastMessageID,
446-
"$lt": opt.LastMessageID + opt.Limit,
453+
"$gte": *filter.LastMessageID,
454+
"$lt": (*filter.LastMessageID) + int64(*filter.Limit),
447455
},
448-
}, options.Find().SetSort(opt.Sort))
456+
}, options.Find().SetSort(filter.Sort))
449457
if err != nil {
450-
if err == mongo.ErrNoDocuments {
458+
if errors.IsNoRecord(err) {
451459
return nil, errors.Wrap(err)
452460
}
453461
return nil, errors.Wrap(err)
@@ -469,6 +477,7 @@ func GetChatMessageList(roomID string, sessionType int, opts ...*GetChatMessageL
469477
func GetLastChatMessageList(roomID string, sessionType int, opts ...*GetOptions) ([]*ChatMessage, error) {
470478
opt := MergeGetOptions(opts)
471479
cacheKey := cacheKeyFormatLastMessageList(roomID, sessionType)
480+
472481
// 如果有使用缓存,则从缓存中获取
473482
if opt.UseCache() {
474483
exists, _ := jcache.Exists(GlobCtx, cacheKey)
@@ -491,7 +500,7 @@ func GetLastChatMessageList(roomID string, sessionType int, opts ...*GetOptions)
491500
Find(GlobCtx, bson.M{
492501
"room_id": roomID,
493502
"session_type": sessionType,
494-
}, options.Find().SetSort(bson.M{"created_at": -1}).SetLimit(defaultLastLimit))
503+
}, options.Find().SetSort(bson.M{"message_id": -1}).SetLimit(defaultLastLimit))
495504

496505
if err != nil {
497506
if errors.Is(err, mongo.ErrNoDocuments) {
@@ -528,6 +537,9 @@ func GetLastChatMessageList(roomID string, sessionType int, opts ...*GetOptions)
528537
log.Warn().Err(err).Msg("缓存插入聊天消息失败")
529538
}
530539
}
540+
if err == nil {
541+
jcache.Expire(GlobCtx, cacheKey, jcache.RandomExpirationDuration())
542+
}
531543

532544
// 设置缓存
533545
return messages, nil

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require (
2929
github.com/go-playground/validator/v10 v10.15.3 // indirect
3030
github.com/golang/snappy v0.0.1 // indirect
3131
github.com/google/go-cmp v0.5.9 // indirect
32+
github.com/google/uuid v1.3.1 // indirect
3233
github.com/json-iterator/go v1.1.12 // indirect
3334
github.com/klauspost/compress v1.13.6 // indirect
3435
github.com/klauspost/cpuid/v2 v2.2.4 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
4343
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
4444
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
4545
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
46+
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
47+
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4648
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
4749
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
4850
github.com/jerbe/jcache v1.0.0 h1:xi2ErWRAZQTB5hHTcB0nF8kdvx4ZQ/4SJrhmDsIEStM=

0 commit comments

Comments
 (0)