-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactivity.go
More file actions
85 lines (64 loc) · 1.75 KB
/
activity.go
File metadata and controls
85 lines (64 loc) · 1.75 KB
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
package main
import (
"bytes"
"fmt"
"strconv"
"text/template"
)
type Activity struct {
Id int64 `json:"id"`
TemplateId string `json:"template_id"`
Event ActivityPayload `json:"event"`
TimeStamp
}
type ActivityList []Activity
func (a *ActivityList) List() error {
_, err := dbmap.Select(a, "SELECT * FROM ACTIVITIES ORDER BY CreatedAt DESC")
return err
}
func (a *Activity) Create() error {
// run the UpdateTime ethod on the user model
a.UpdateTime()
// run the DB insert function
err := dbmap.Insert(a)
return err
}
func ActivityParser(p []ActivityPayload) []Activity {
activities := []Activity{}
for _, payload := range p {
for _, template := range templates {
activity := Activity{}
if CheckEventType(template.Event, payload) && ValidateConditional(template.Condition, payload) {
fmt.Println("Building activity")
//Create the activity for this template and append to list
activity.TemplateId = template.Id
activity.Event = payload
activity.UpdateTime()
err := activity.Create()
checkErr(err, "Problem saving activity")
activities = append(activities, activity)
}
}
}
return activities
}
func CheckEventType(e string, p ActivityPayload) bool {
if e == "*" {
return true
}
return e == p.EventType
}
func ValidateConditional(c string, p ActivityPayload) bool {
if c == "" {
return true
}
buf := new(bytes.Buffer)
tmpl, err := template.New("condition").Parse(fmt.Sprintf("{{%s}}", c))
checkErr(err, "Failed to parse template condition")
err = tmpl.Execute(buf, p)
checkErr(err, "Failed to execute template condition")
resultStr := buf.String()
result, err := strconv.ParseBool(resultStr)
checkErr(err, "Failed to parse bool from condition")
return result
}