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
/
CharacterAPI.go
150 lines (118 loc) · 3.43 KB
/
CharacterAPI.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
package arn
import (
"errors"
"fmt"
"reflect"
"github.com/aerogo/aero"
"github.com/aerogo/api"
)
// Force interface implementations
var (
_ Likeable = (*Character)(nil)
_ Publishable = (*Character)(nil)
_ PostParent = (*Character)(nil)
_ fmt.Stringer = (*Character)(nil)
_ api.Newable = (*Character)(nil)
_ api.Editable = (*Character)(nil)
_ api.Deletable = (*Character)(nil)
)
// Actions
func init() {
API.RegisterActions("Character", []*api.Action{
// Publish
PublishAction(),
// Unpublish
UnpublishAction(),
// Like character
LikeAction(),
// Unlike character
UnlikeAction(),
})
}
// Create sets the data for a new character with data we received from the API request.
func (character *Character) Create(ctx aero.Context) error {
user := GetUserFromContext(ctx)
if user == nil {
return errors.New("Not logged in")
}
character.ID = GenerateID("Character")
character.Created = DateTimeUTC()
character.CreatedBy = user.ID
// Write log entry
logEntry := NewEditLogEntry(user.ID, "create", "Character", character.ID, "", "", "")
logEntry.Save()
return character.Unpublish()
}
// Authorize returns an error if the given API request is not authorized.
func (character *Character) Authorize(ctx aero.Context, action string) error {
user := GetUserFromContext(ctx)
if user == nil {
return errors.New("Not logged in")
}
// Allow custom actions (like, unlike) for normal users
if action == "like" || action == "unlike" {
return nil
}
if user.Role != "editor" && user.Role != "admin" {
return errors.New("Insufficient permissions")
}
return nil
}
// Edit creates an edit log entry.
func (character *Character) Edit(ctx aero.Context, key string, value reflect.Value, newValue reflect.Value) (consumed bool, err error) {
return edit(character, ctx, key, value, newValue)
}
// OnAppend saves a log entry.
func (character *Character) OnAppend(ctx aero.Context, key string, index int, obj interface{}) {
onAppend(character, ctx, key, index, obj)
}
// OnRemove saves a log entry.
func (character *Character) OnRemove(ctx aero.Context, key string, index int, obj interface{}) {
onRemove(character, ctx, key, index, obj)
}
// DeleteInContext deletes the character in the given context.
func (character *Character) DeleteInContext(ctx aero.Context) error {
user := GetUserFromContext(ctx)
// Write log entry
logEntry := NewEditLogEntry(user.ID, "delete", "Character", character.ID, "", fmt.Sprint(character), "")
logEntry.Save()
return character.Delete()
}
// Delete deletes the object from the database.
func (character *Character) Delete() error {
if character.IsDraft {
draftIndex := character.Creator().DraftIndex()
draftIndex.CharacterID = ""
draftIndex.Save()
}
// Delete from anime characters
for list := range StreamAnimeCharacters() {
list.Lock()
for index, animeCharacter := range list.Items {
if animeCharacter.CharacterID == character.ID {
list.Items = append(list.Items[:index], list.Items[index+1:]...)
list.Save()
break
}
}
list.Unlock()
}
// Delete from quotes
for quote := range StreamQuotes() {
if quote.CharacterID == character.ID {
err := quote.Delete()
if err != nil {
return err
}
}
}
// Delete image files
character.DeleteImages()
// Delete character
DB.Delete("Character", character.ID)
return nil
}
// Save saves the character in the database.
func (character *Character) Save() {
DB.Set("Character", character.ID, character)
}