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 pathEditLogEntry.go
168 lines (130 loc) · 3.52 KB
/
EditLogEntry.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
package arn
import (
"reflect"
"sort"
"github.com/aerogo/nano"
)
// EditLogEntry is an entry in the editor log.
type EditLogEntry struct {
ID string `json:"id"`
UserID string `json:"userId"`
Action string `json:"action"`
ObjectType string `json:"objectType"` // The typename of what was edited
ObjectID string `json:"objectId"` // The ID of what was edited
Key string `json:"key"`
OldValue string `json:"oldValue"`
NewValue string `json:"newValue"`
Created string `json:"created"`
}
// NewEditLogEntry ...
func NewEditLogEntry(userID, action, objectType, objectID, key, oldValue, newValue string) *EditLogEntry {
return &EditLogEntry{
ID: GenerateID("EditLogEntry"),
UserID: userID,
Action: action,
ObjectType: objectType,
ObjectID: objectID,
Key: key,
OldValue: oldValue,
NewValue: newValue,
Created: DateTimeUTC(),
}
}
// User returns the user the log entry belongs to.
func (entry *EditLogEntry) User() *User {
user, _ := GetUser(entry.UserID)
return user
}
// Object returns the object the log entry refers to.
func (entry *EditLogEntry) Object() interface{} {
obj, _ := DB.Get(entry.ObjectType, entry.ObjectID)
return obj
}
// EditorScore returns the editing score for this log entry.
func (entry *EditLogEntry) EditorScore() int {
switch entry.Action {
case "create":
obj, err := DB.Get(entry.ObjectType, entry.ObjectID)
if err != nil {
return 0
}
v := reflect.Indirect(reflect.ValueOf(obj))
isDraft := v.FieldByName("IsDraft")
if isDraft.Kind() == reflect.Bool && isDraft.Bool() {
// No score for drafts
return 0
}
return 4
case "edit":
score := 4
// Bonus score for editing anime
if entry.ObjectType == "Anime" {
score++
// Bonus score for editing anime synopsis
if entry.Key == "Summary" || entry.Key == "Synopsis" {
score++
}
}
return score
case "delete", "arrayRemove":
return 3
case "arrayAppend":
return 0
}
return 0
}
// ActionHumanReadable returns the human readable version of the action.
func (entry *EditLogEntry) ActionHumanReadable() string {
switch entry.Action {
case "create":
return "Created"
case "edit":
return "Edited"
case "delete":
return "Deleted"
case "arrayAppend":
return "Added an element"
case "arrayRemove":
return "Removed an element"
default:
return entry.Action
}
}
// StreamEditLogEntries returns a stream of all log entries.
func StreamEditLogEntries() <-chan *EditLogEntry {
channel := make(chan *EditLogEntry, nano.ChannelBufferSize)
go func() {
for obj := range DB.All("EditLogEntry") {
channel <- obj.(*EditLogEntry)
}
close(channel)
}()
return channel
}
// AllEditLogEntries returns a slice of all log entries.
func AllEditLogEntries() []*EditLogEntry {
all := make([]*EditLogEntry, 0, DB.Collection("EditLogEntry").Count())
stream := StreamEditLogEntries()
for obj := range stream {
all = append(all, obj)
}
return all
}
// FilterEditLogEntries filters all log entries by a custom function.
func FilterEditLogEntries(filter func(*EditLogEntry) bool) []*EditLogEntry {
var filtered []*EditLogEntry
channel := DB.All("EditLogEntry")
for obj := range channel {
realObject := obj.(*EditLogEntry)
if filter(realObject) {
filtered = append(filtered, realObject)
}
}
return filtered
}
// SortEditLogEntriesLatestFirst puts the latest entries on top.
func SortEditLogEntriesLatestFirst(entries []*EditLogEntry) {
sort.Slice(entries, func(i, j int) bool {
return entries[i].Created > entries[j].Created
})
}