/
slact.go
131 lines (106 loc) · 3.05 KB
/
slact.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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/bjacobel/slact/Godeps/_workspace/src/github.com/davecgh/go-spew/spew"
"github.com/bjacobel/slact/Godeps/_workspace/src/github.com/go-martini/martini"
"github.com/bjacobel/slact/Godeps/_workspace/src/github.com/joho/godotenv"
"github.com/bjacobel/slact/Godeps/_workspace/src/github.com/martini-contrib/render"
"github.com/bjacobel/slact/Godeps/_workspace/src/github.com/nlopes/slack"
"github.com/bjacobel/slact/Godeps/_workspace/src/gopkg.in/mgo.v2"
"github.com/bjacobel/slact/Godeps/_workspace/src/gopkg.in/mgo.v2/bson"
)
var spw = spew.NewDefaultConfig()
func main() {
// Add some configuration to our JSON logger
spw = &spew.ConfigState{Indent: "\t", MaxDepth: 5}
// load .env into os.Getenv
err := godotenv.Load()
if err != nil {
log.Println("No .env, running in production mode, hope that's what you wanted")
}
creds := ""
if os.Getenv("MGUSER") != "" && os.Getenv("MGPW") != "" {
creds = fmt.Sprintf("%s:%s@", os.Getenv("MGUSER"), os.Getenv("MGPW"))
}
// connect to a mongodb session - local or through Docker
session, err := mgo.DialWithTimeout(
fmt.Sprintf("mongodb://%s%s/%s", creds, os.Getenv("MGHOST"), os.Getenv("MGDB")),
time.Second,
)
if err == nil {
defer session.Close()
session.SetMode(mgo.Monotonic, true)
} else {
log.Fatal(err)
}
db := session.Clone().DB(os.Getenv("MGDB")).C(os.Getenv("MGCOLL"))
// Slack API client
api := slack.New(os.Getenv("SLACK"))
// Martini router and renderer
app := martini.Classic()
app.Use(render.Renderer())
// Open a connection to Slack Real Time Messaging API
rtm := api.NewRTM()
go rtm.ManageConnection()
// expose Martini endpoints for our data
app.Group("/", func(r martini.Router) {
r.Get("reactions", func(r render.Render) {
var result []bson.M
pipe := db.Pipe(
[]bson.M{
{
"$sort": bson.M{"eventtimestamp": 1},
},
{
"$group": bson.M{
"_id": "$reaction",
"reactions": bson.M{
"$push": "$$CURRENT",
},
"count": bson.M{"$sum": 1},
},
},
{
"$sort": bson.M{"count": -1},
},
},
)
iter := pipe.Iter()
err := iter.All(&result)
if err != nil {
log.Println(err.Error())
r.JSON(400, bson.M{"error": err.Error()})
} else {
r.JSON(200, result)
}
})
})
go app.Run()
for {
select {
case msg := <-rtm.IncomingEvents:
switch msg.Data.(type) {
case *slack.ReactionAddedEvent:
go insertReaction(msg.Data.(*slack.ReactionAddedEvent), db)
case *slack.ReactionRemovedEvent:
go deleteReaction(msg.Data.(*slack.ReactionRemovedEvent), db)
default:
continue
}
}
}
}
func insertReaction(reax *slack.ReactionAddedEvent, db *mgo.Collection) {
db.Insert(&reax)
// log.Printf("Added reaction %s\n", reax.Reaction)
}
func deleteReaction(reax *slack.ReactionRemovedEvent, db *mgo.Collection) {
err := db.Remove(bson.M{"user": reax.User, "item.item.timestamp": reax.Item.Item.Timestamp})
if err != nil {
return
}
log.Println("Could not find right object to delete. Not a huge deal.")
}