This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGroup.go
310 lines (244 loc) · 7.35 KB
/
Group.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package arn
import (
"errors"
"fmt"
"os"
"path"
"sync"
"github.com/aerogo/nano"
"github.com/akyoto/color"
)
// Group represents a group of users.
type Group struct {
Name string `json:"name" editable:"true"`
Tagline string `json:"tagline" editable:"true"`
Image GroupImage `json:"image"`
Description string `json:"description" editable:"true" type:"textarea"`
Rules string `json:"rules" editable:"true" type:"textarea"`
Restricted bool `json:"restricted" editable:"true" tooltip:"Restricted groups can only be joined with the founder's permission."`
Tags []string `json:"tags" editable:"true"`
Members []*GroupMember `json:"members"`
Neighbors []string `json:"neighbors"`
// Applications []UserApplication `json:"applications"`
// Mixins
hasID
hasPosts
hasCreator
hasEditor
hasDraft
// Mutex
membersMutex sync.Mutex
}
// Link returns the URI to the group page.
func (group *Group) Link() string {
return "/group/" + group.ID
}
// TitleByUser returns the preferred title for the given user.
func (group *Group) TitleByUser(user *User) string {
if group.Name == "" {
return "untitled"
}
return group.Name
}
// String is the default text representation of the group.
func (group *Group) String() string {
return group.TitleByUser(nil)
}
// FindMember returns the group member by user ID, if available.
func (group *Group) FindMember(userID UserID) *GroupMember {
group.membersMutex.Lock()
defer group.membersMutex.Unlock()
for _, member := range group.Members {
if member.UserID == userID {
return member
}
}
return nil
}
// HasMember returns true if the user is a member of the group.
func (group *Group) HasMember(userID UserID) bool {
return group.FindMember(userID) != nil
}
// Users returns a slice of all users in the group.
func (group *Group) Users() []*User {
group.membersMutex.Lock()
defer group.membersMutex.Unlock()
users := make([]*User, len(group.Members))
for index, member := range group.Members {
users[index] = member.User()
}
return users
}
// TypeName returns the type name.
func (group *Group) TypeName() string {
return "Group"
}
// Self returns the object itself.
func (group *Group) Self() Loggable {
return group
}
// Publish ...
func (group *Group) Publish() error {
if len(group.Name) < 2 {
return errors.New("Name too short: Should be at least 2 characters")
}
if len(group.Name) > 35 {
return errors.New("Name too long: Should not be more than 35 characters")
}
if len(group.Tagline) < 4 {
return errors.New("Tagline too short: Should be at least 4 characters")
}
if len(group.Tagline) > 60 {
return errors.New("Tagline too long: Should not be more than 60 characters")
}
if len(group.Description) < 10 {
return errors.New("Your group needs a description (at least 10 characters)")
}
if len(group.Tags) < 1 {
return errors.New("At least one tag is required")
}
if !group.HasImage() {
return errors.New("Group image required")
}
return publish(group)
}
// Unpublish ...
func (group *Group) Unpublish() error {
return unpublish(group)
}
// Join makes the given user join the group.
func (group *Group) Join(user *User) error {
// Check if the user is already a member
member := group.FindMember(user.ID)
if member != nil {
return errors.New("Already a member of this group")
}
// Add user to the members list
group.membersMutex.Lock()
group.Members = append(group.Members, &GroupMember{
UserID: user.ID,
Joined: DateTimeUTC(),
})
group.membersMutex.Unlock()
// Trigger notifications
group.OnJoin(user)
return nil
}
// Leave makes the given user leave the group.
func (group *Group) Leave(user *User) error {
group.membersMutex.Lock()
defer group.membersMutex.Unlock()
for index, member := range group.Members {
if member.UserID == user.ID {
if member.UserID == group.CreatedBy {
return errors.New("The founder can not leave the group, please contact a staff member")
}
group.Members = append(group.Members[:index], group.Members[index+1:]...)
return nil
}
}
return nil
}
// OnJoin sends notifications to the creator.
func (group *Group) OnJoin(user *User) {
go func() {
group.Creator().SendNotification(&PushNotification{
Title: fmt.Sprintf(`%s joined your group!`, user.Nick),
Message: fmt.Sprintf(`%s has joined your group "%s"`, user.Nick, group.Name),
Icon: "https:" + user.AvatarLink("large"),
Link: "https://notify.moe" + group.Link() + "/members",
Type: NotificationTypeGroupJoin,
})
}()
}
// SendNotification sends a notification to all group members except for the excluded user ID.
func (group *Group) SendNotification(notification *PushNotification, excludeUserID UserID) {
for _, user := range group.Users() {
if user.ID == excludeUserID {
continue
}
user.SendNotification(notification)
}
}
// AverageColor returns the average color of the image.
func (group *Group) AverageColor() string {
color := group.Image.AverageColor
if color.Hue == 0 && color.Saturation == 0 && color.Lightness == 0 {
return ""
}
return color.String()
}
// ImageLink returns a link to the group image.
func (group *Group) ImageLink(size string) string {
if !group.HasImage() {
return fmt.Sprintf("//%s/images/elements/no-group-image.svg", MediaHost)
}
extension := ".jpg"
if size == "original" {
extension = group.Image.Extension
}
return fmt.Sprintf("//%s/images/groups/%s/%s%s?%v", MediaHost, size, group.ID, extension, group.Image.LastModified)
}
// DeleteImages deletes all images for the group.
func (group *Group) DeleteImages() {
if group.Image.Extension == "" {
return
}
// Original
err := os.Remove(path.Join(Root, "images/groups/original/", group.ID+group.Image.Extension))
if err != nil {
// Don't return the error.
// It's too late to stop the process at this point.
// Instead, log the error.
color.Red(err.Error())
}
// Small
os.Remove(path.Join(Root, "images/groups/small/", group.ID+".jpg"))
os.Remove(path.Join(Root, "images/groups/small/", group.ID+"@2.jpg"))
os.Remove(path.Join(Root, "images/groups/small/", group.ID+".webp"))
os.Remove(path.Join(Root, "images/groups/small/", group.ID+"@2.webp"))
// Large
os.Remove(path.Join(Root, "images/groups/large/", group.ID+".jpg"))
os.Remove(path.Join(Root, "images/groups/large/", group.ID+"@2.jpg"))
os.Remove(path.Join(Root, "images/groups/large/", group.ID+".webp"))
os.Remove(path.Join(Root, "images/groups/large/", group.ID+"@2.webp"))
}
// GetGroup ...
func GetGroup(id string) (*Group, error) {
obj, err := DB.Get("Group", id)
if err != nil {
return nil, err
}
return obj.(*Group), nil
}
// StreamGroups returns a stream of all groups.
func StreamGroups() <-chan *Group {
channel := make(chan *Group, nano.ChannelBufferSize)
go func() {
for obj := range DB.All("Group") {
channel <- obj.(*Group)
}
close(channel)
}()
return channel
}
// AllGroups returns a slice of all groups.
func AllGroups() []*Group {
all := make([]*Group, 0, DB.Collection("Group").Count())
stream := StreamGroups()
for obj := range stream {
all = append(all, obj)
}
return all
}
// FilterGroups filters all groups by a custom function.
func FilterGroups(filter func(*Group) bool) []*Group {
var filtered []*Group
for obj := range DB.All("Group") {
realObject := obj.(*Group)
if filter(realObject) {
filtered = append(filtered, realObject)
}
}
return filtered
}