-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslack-upvote.go
109 lines (101 loc) · 2.47 KB
/
slack-upvote.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
package main
import (
"bytes"
"fmt"
"net/http"
"os"
"strings"
"github.com/codegangsta/negroni"
"github.com/jqatampa/gadget-arm/session"
"gopkg.in/mgo.v2/bson"
)
type Mention struct {
Id string `bson:"_id"`
TeamId string `bson:"team_id"`
Votes int64 `bson:"votes"`
}
func VoteHandler(rw http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(5120)
isValid := len(r.Form["text"]) > 0 && len(r.Form["team_id"]) > 0
if !isValid {
rw.Write([]byte("See ya homies"))
return
}
isCmd := len(r.Form["command"]) > 0
isTrg := len(r.Form["trigger_word"]) > 0
db := session.Get("SLACKVOTE_DB").DB("slack-upvote")
mentionId := r.Form["text"][0]
teamId := r.Form["team_id"][0]
sfx := ""
if isTrg {
mentionId = strings.TrimLeft(mentionId, r.Form["trigger_word"][0])
}
mentionId = strings.Trim(mentionId, " '\"")
if mentionId == "" {
rw.Write([]byte(""))
return
}
isLoserBoard := mentionId == "-"
isLeaderBoard := mentionId == "+"
if !isLeaderBoard && !isLoserBoard {
m := Mention{
Id: strings.ToLower(mentionId),
TeamId: teamId,
}
db.C("mentions").Find(bson.M{"_id": m.Id, "team_id": m.TeamId}).One(&m)
if isCmd {
cmd := r.Form["command"][0]
switch cmd {
case "/up":
m.Votes++
case "/down":
m.Votes--
}
} else if isTrg {
trg := r.Form["trigger_word"][0]
switch trg {
case "+":
m.Votes++
case "-":
m.Votes--
}
}
db.C("mentions").UpsertId(m.Id, m)
if m.Votes > 1 || m.Votes < -1 || m.Votes == 0 {
sfx = "s"
}
text := fmt.Sprintf("'%s' has %v vote%s.", mentionId, m.Votes, sfx)
if isCmd {
rw.Write([]byte(text))
} else if isTrg {
rw.Write([]byte(fmt.Sprintf("{\"text\":\"%s\"}", text)))
}
} else {
var sort string
var sb bytes.Buffer
if isLeaderBoard {
sb.WriteString(fmt.Sprintf("*Leader Board*\\n\\n"))
sort = "-votes"
} else {
sb.WriteString(fmt.Sprintf("*Loser Board*\\n\\n"))
sort = "votes"
}
iter := db.C("mentions").Find(bson.M{"team_id": teamId}).Limit(10).Sort(sort).Iter()
var m Mention
for iter.Next(&m) {
sb.WriteString(fmt.Sprintf("*%v*\\t\\t*%s*\\n", m.Votes, m.Id))
}
responseText := fmt.Sprintf("{\"text\":\"%s\", \"mkdown\":true, \"username\":\"Voting\", \"icon_emoji\":\":thumbsdown:\" }", sb.String())
fmt.Println(responseText)
rw.Write([]byte(responseText))
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", VoteHandler)
n := negroni.New(
negroni.NewLogger(),
negroni.Wrap(mux),
)
n.Run(":" + os.Getenv("PORT"))
}