/
1module.go
153 lines (139 loc) · 4.26 KB
/
1module.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
package user
import (
"embed"
"fmt"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/common"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/config"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/model"
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/pkg/register"
"github.com/pkg/errors"
"go.uber.org/zap"
)
//go:embed sql
var sqlFS embed.FS
//go:embed swagger/api.yaml
var swaggerContent string
//go:embed swagger/friend.yaml
var friendSwaggerContent string
func init() {
// ====================== 注册用户模块 ======================
register.AddModule(func(ctx interface{}) register.Module {
x := ctx.(*config.Context)
api := New(x)
return register.Module{
Name: "user",
SetupAPI: func() register.APIRouter {
return api
},
Swagger: swaggerContent,
SQLDir: register.NewSQLFS(sqlFS),
IMDatasource: register.IMDatasource{
SystemUIDs: func() ([]string, error) {
users, err := api.userService.GetUsersWithCategory(CategoryService)
if err != nil {
return nil, err
}
uids := make([]string, 0, len(users))
if len(users) > 0 {
for _, user := range users {
uids = append(uids, user.UID)
}
}
return uids, nil
},
},
BussDataSource: register.BussDataSource{
ChannelGet: func(channelID string, channelType uint8, loginUID string) (*model.ChannelResp, error) {
if channelType != common.ChannelTypePerson.Uint8() {
return nil, register.ErrDatasourceNotProcess
}
userDetailResp, err := api.userService.GetUserDetail(channelID, loginUID)
if err != nil {
return nil, err
}
if userDetailResp == nil {
api.Error("用户不存在!", zap.String("channel_id", channelID))
return nil, errors.New("用户不存在!")
}
return newChannelRespWithUserDetailResp(userDetailResp), nil
},
},
}
})
// ====================== 注册好友模块 ======================
register.AddModule(func(ctx interface{}) register.Module {
api := NewFriend(ctx.(*config.Context))
return register.Module{
Name: "friend",
SetupAPI: func() register.APIRouter {
return api
},
Swagger: friendSwaggerContent,
IMDatasource: register.IMDatasource{
HasData: func(channelID string, channelType uint8) register.IMDatasourceType {
if channelType == common.ChannelTypePerson.Uint8() {
return register.IMDatasourceTypeWhitelist
}
return register.IMDatasourceTypeNone
},
Whitelist: func(channelID string, channelType uint8) ([]string, error) {
friends, err := api.userService.GetFriends(channelID)
if err != nil {
return nil, err
}
firendUIDs := make([]string, 0, len(friends))
if len(friends) > 0 {
for _, friend := range friends {
if friend.IsAlone == 0 {
firendUIDs = append(firendUIDs, friend.UID)
}
}
}
return firendUIDs, nil
},
},
}
})
// ====================== 注册用户管理模块 ======================
register.AddModule(func(ctx interface{}) register.Module {
return register.Module{
Name: "user_manager",
SetupAPI: func() register.APIRouter {
return NewManager(ctx.(*config.Context))
},
}
})
}
func newChannelRespWithUserDetailResp(user *UserDetailResp) *model.ChannelResp {
resp := &model.ChannelResp{}
resp.Channel.ChannelID = user.UID
resp.Channel.ChannelType = uint8(common.ChannelTypePerson)
resp.Name = user.Name
resp.Username = user.Username
resp.Logo = fmt.Sprintf("users/%s/avatar", user.UID)
resp.Mute = user.Mute
resp.Stick = user.Top
resp.Receipt = user.Receipt
resp.Robot = user.Robot
resp.Online = user.Online
resp.LastOffline = int64(user.LastOffline)
resp.DeviceFlag = user.DeviceFlag
resp.Category = user.Category
resp.Follow = user.Follow
resp.Remark = user.Remark
resp.Status = user.Status
resp.BeBlacklist = user.BeBlacklist
resp.BeDeleted = user.BeDeleted
resp.Flame = user.Flame
resp.FlameSecond = user.FlameSecond
extraMap := make(map[string]interface{})
extraMap["sex"] = user.Sex
extraMap["chat_pwd_on"] = user.ChatPwdOn
extraMap["short_no"] = user.ShortNo
extraMap["source_desc"] = user.SourceDesc
extraMap["vercode"] = user.Vercode
extraMap["screenshot"] = user.Screenshot
extraMap["revoke_remind"] = user.RevokeRemind
resp.Extra = extraMap
return resp
}