-
Notifications
You must be signed in to change notification settings - Fork 16
/
emoticon.go
118 lines (91 loc) · 2.97 KB
/
emoticon.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
package dao
import (
"context"
"errors"
"github.com/gogf/gf/v2/text/gstr"
"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"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"slices"
)
var Emoticon = NewEmoticonDao()
type EmoticonDao struct {
*MongoDB[entity.Emoticon]
}
func NewEmoticonDao(database ...string) *EmoticonDao {
if len(database) == 0 {
database = append(database, db.DefaultDatabase)
}
return &EmoticonDao{
MongoDB: NewMongoDB[entity.Emoticon](database[0], do.EMOTICON_COLLECTION),
}
}
// 获取用户激活的表情包
func (d *EmoticonDao) GetUserInstallIds(ctx context.Context, uid int) []string {
userEmoticon := new(entity.UserEmoticon)
if err := FindOne(ctx, d.Database, do.USER_EMOTICON_COLLECTION, bson.M{"user_id": uid}, &userEmoticon); err != nil {
if !errors.Is(err, mongo.ErrNoDocuments) {
logger.Error(ctx, err)
}
return []string{}
}
return gconv.Strings(gstr.Split(userEmoticon.EmoticonIds, ","))
}
// 获取系统表情包分组列表
func (d *EmoticonDao) GetSystemEmoticonList(ctx context.Context) ([]*entity.Emoticon, error) {
emoticonList, err := d.Find(ctx, bson.M{})
if err != nil {
return nil, err
}
return emoticonList, nil
}
// 获取系统表情包分组详情列表
func (d *EmoticonDao) GetDetailsAll(ctx context.Context, uid int) ([]*entity.EmoticonItem, error) {
filter := bson.M{
"user_id": uid,
}
emoticonItemList := make([]*entity.EmoticonItem, 0)
if err := Find(ctx, d.Database, do.EMOTICON_ITEM_COLLECTION, filter, &emoticonItemList, "-created_at"); err != nil {
return nil, err
}
return emoticonItemList, nil
}
func (d *EmoticonDao) RemoveUserSysEmoticon(ctx context.Context, uid int, emoticonId string) error {
ids := d.GetUserInstallIds(ctx, uid)
if !slices.Contains(ids, emoticonId) {
return errors.New("数据不存在")
}
items := make([]string, 0, len(ids)-1)
for _, id := range ids {
if id != emoticonId {
items = append(items, id)
}
}
if err := UpdateOne(ctx, d.Database, do.USER_EMOTICON_COLLECTION, bson.M{"user_id": uid}, bson.M{
"emoticon_ids": gstr.JoinAny(items, ","),
}); err != nil {
return err
}
return nil
}
func (d *EmoticonDao) AddUserSysEmoticon(ctx context.Context, uid int, emoticonId string) error {
ids := d.GetUserInstallIds(ctx, uid)
if slices.Contains(ids, emoticonId) {
return nil
}
ids = append(ids, emoticonId)
if err := UpdateOne(ctx, d.Database, do.USER_EMOTICON_COLLECTION, bson.M{"user_id": uid}, bson.M{
"emoticon_ids": gstr.JoinAny(ids, ","),
}); err != nil {
return err
}
return nil
}
// 删除自定义表情包
func (d *EmoticonDao) DeleteCollect(ctx context.Context, uid int, ids []int) (int64, error) {
return DeleteOne(ctx, d.Database, do.EMOTICON_ITEM_COLLECTION, bson.M{"_id": bson.M{"$in": ids}, "emoticon_id": "", "user_id": uid})
}