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
/
GroupAPI.go
137 lines (108 loc) · 2.92 KB
/
GroupAPI.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
package arn
import (
"errors"
"fmt"
"reflect"
"github.com/aerogo/aero"
"github.com/aerogo/api"
)
// Force interface implementations
var (
_ Joinable = (*Group)(nil)
_ Publishable = (*Group)(nil)
_ PostParent = (*Group)(nil)
_ fmt.Stringer = (*Group)(nil)
_ api.Newable = (*Group)(nil)
_ api.Editable = (*Group)(nil)
_ api.Deletable = (*Group)(nil)
)
// Actions
func init() {
API.RegisterActions("Group", []*api.Action{
// Publish
PublishAction(),
// Unpublish
UnpublishAction(),
// Join
JoinAction(),
// Leave
LeaveAction(),
})
}
// Create ...
func (group *Group) Create(ctx aero.Context) error {
user := GetUserFromContext(ctx)
if user == nil {
return errors.New("Not logged in")
}
if !user.IsPro() {
return errors.New("Not available for normal users during the BETA phase")
}
group.ID = GenerateID("Group")
group.Created = DateTimeUTC()
group.CreatedBy = user.ID
group.Edited = group.Created
group.EditedBy = group.CreatedBy
group.Members = []*GroupMember{
{
UserID: user.ID,
Role: "founder",
Joined: group.Created,
},
}
// Write log entry
logEntry := NewEditLogEntry(user.ID, "create", "Group", group.ID, "", "", "")
logEntry.Save()
return group.Unpublish()
}
// Edit creates an edit log entry.
func (group *Group) Edit(ctx aero.Context, key string, value reflect.Value, newValue reflect.Value) (consumed bool, err error) {
return edit(group, ctx, key, value, newValue)
}
// OnAppend saves a log entry.
func (group *Group) OnAppend(ctx aero.Context, key string, index int, obj interface{}) {
onAppend(group, ctx, key, index, obj)
}
// OnRemove saves a log entry.
func (group *Group) OnRemove(ctx aero.Context, key string, index int, obj interface{}) {
onRemove(group, ctx, key, index, obj)
}
// Delete deletes the object from the database.
func (group *Group) Delete() error {
if group.IsDraft {
draftIndex := group.Creator().DraftIndex()
draftIndex.GroupID = ""
draftIndex.Save()
}
// Delete image files
group.DeleteImages()
// Delete group
DB.Delete("Group", group.ID)
return nil
}
// DeleteInContext deletes the amv in the given context.
func (group *Group) DeleteInContext(ctx aero.Context) error {
user := GetUserFromContext(ctx)
// Write log entry
logEntry := NewEditLogEntry(user.ID, "delete", "Group", group.ID, "", fmt.Sprint(group), "")
logEntry.Save()
return group.Delete()
}
// Authorize returns an error if the given API POST request is not authorized.
func (group *Group) Authorize(ctx aero.Context, action string) error {
user := GetUserFromContext(ctx)
if user == nil {
return errors.New("Not logged in")
}
if action == "edit" && group.CreatedBy != user.ID {
return errors.New("Can't edit groups from other people")
}
if action == "join" && group.Restricted {
return errors.New("Can't join restricted groups")
}
return nil
}
// Save saves the group in the database.
func (group *Group) Save() {
DB.Set("Group", group.ID, group)
}