/
user.go
176 lines (144 loc) · 3.83 KB
/
user.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
package service
import (
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
modelcommand "github.com/namhq1989/maid-bots/internal/model/command"
"github.com/namhq1989/maid-bots/config"
"github.com/namhq1989/maid-bots/pkg/sentryio"
"github.com/namhq1989/maid-bots/pkg/mongodb"
"github.com/namhq1989/maid-bots/util/appcontext"
"github.com/namhq1989/maid-bots/internal/dao"
"github.com/namhq1989/maid-bots/pkg/sso"
"go.mongodb.org/mongo-driver/bson"
)
type User struct{}
func (User) FindByID(ctx *appcontext.AppContext, id primitive.ObjectID) (*mongodb.User, error) {
span := sentryio.NewSpan(ctx.Context, "[service][user] find by id")
defer span.Finish()
var (
d = dao.User{}
condition = bson.M{
"_id": id,
}
)
return d.FindOneByCondition(ctx, condition)
}
func (User) FindByGitHubID(ctx *appcontext.AppContext, githubID string) (*mongodb.User, error) {
span := sentryio.NewSpan(ctx.Context, "[service][user] find by GitHub id")
defer span.Finish()
var (
d = dao.User{}
condition = bson.M{
"github.id": githubID,
}
)
return d.FindOneByCondition(ctx, condition)
}
func (User) FindByGoogleID(ctx *appcontext.AppContext, googleID string) (*mongodb.User, error) {
span := sentryio.NewSpan(ctx.Context, "[service][user] find by Google id")
defer span.Finish()
var (
d = dao.User{}
condition = bson.M{
"google.id": googleID,
}
)
return d.FindOneByCondition(ctx, condition)
}
func (User) CreateWithGitHubData(ctx *appcontext.AppContext, githubData sso.GitHubUserData) (*mongodb.User, error) {
span := sentryio.NewSpan(ctx.Context, "[service][user] create with GitHub data")
defer span.Finish()
var (
d = dao.User{}
)
user := mongodb.User{
ID: mongodb.NewObjectID(),
Name: githubData.Name,
Avatar: githubData.Avatar,
Google: nil,
GitHub: &mongodb.UserSocialProviderInformation{
ID: githubData.ID,
Name: githubData.Name,
Email: githubData.Email,
Avatar: githubData.Avatar,
},
CreatedAt: time.Now(),
}
if err := d.InsertOne(ctx, user); err != nil {
return nil, err
}
return &user, nil
}
func (User) CreateWithGoogleData(ctx *appcontext.AppContext, googleData sso.GoogleUserData) (*mongodb.User, error) {
span := sentryio.NewSpan(ctx.Context, "[service][user] create with Google data")
defer span.Finish()
var (
d = dao.User{}
)
user := mongodb.User{
ID: mongodb.NewObjectID(),
Name: googleData.Name,
GitHub: nil,
Google: &mongodb.UserSocialProviderInformation{
ID: googleData.ID,
Name: googleData.Name,
Email: googleData.Email,
Avatar: googleData.Avatar,
},
CreatedAt: time.Now(),
}
if err := d.InsertOne(ctx, user); err != nil {
return nil, err
}
return &user, nil
}
func (User) FindOrCreateWithPlatformID(ctx *appcontext.AppContext, platform, roomID string, u modelcommand.User) (*mongodb.User, error) {
span := sentryio.NewSpan(ctx.Context, "[service][user] find or create with platform id")
defer span.Finish()
var (
d = dao.User{}
condition = bson.M{
fmt.Sprintf("%s.userId", platform): u.ID,
}
)
// find
user, err := d.FindOneByCondition(ctx, condition)
if err != nil {
return nil, err
}
// return if user existed
if user != nil {
return user, nil
}
// create new user
user = &mongodb.User{
ID: mongodb.NewObjectID(),
Name: u.Name,
Username: u.Username,
Google: nil,
GitHub: nil,
CreatedAt: time.Now(),
}
switch platform {
case config.Platform.Telegram:
user.Telegram = &mongodb.UserPlatform{
UserID: u.ID,
RoomID: roomID,
}
case config.Platform.Slack:
user.Slack = &mongodb.UserPlatform{
UserID: u.ID,
RoomID: roomID,
}
case config.Platform.Discord:
user.Discord = &mongodb.UserPlatform{
UserID: u.ID,
RoomID: roomID,
}
}
if err = d.InsertOne(ctx, *user); err != nil {
return nil, err
}
return user, nil
}