This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
89 lines (66 loc) · 2.41 KB
/
message.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
package main
import (
"fmt"
"log"
"math/rand"
"time"
)
type trackedMessage struct {
ID string `json:"id"` //The unique message id that is used to identify the slack message
Submitted int64 `json:"submitted"` //Time the request was submitted in epoch
CallbackURL string `json:"callback_url"` //URL to send event stream of emoji usage
ValidEmojis []string `json:"valid_emojis"` //List of emojis to alert on
AuthToken string `json:"auth_token"` //Auth token given by app to auth on callback
MessageEvents []*messageEvent `json:"message_events"` //A list of messages events as they come in
Expire int `json:"expire"` //Length of time messages can be tracked. Limited to 24h
}
type messageEvent struct {
ID string `json:"id"` //The unique message ID
Submitted int64 `json:"submitted"` //Epoch time when event happened
EmojiUsed string `json:"emoji_used"` //EmojiUsed in event
SlackUserEmail string `json:"slack_user_email"` //Slack email of the user who used the emoji
SlackUserName string `json:"slack_user_name"` //Slack username of the user who used the emoji
}
func (app *app) getMessage(messageID string) (*trackedMessage, error) {
if _, ok := app.messages[messageID]; !ok {
return &trackedMessage{}, errMessageNotFound
}
message := app.messages[messageID]
return message, nil
}
func (app *app) createMessage(callbackURL, authToken string, validEmojs []string) trackedMessage {
messageID := app.generateNewMessageID()
newMessage := &trackedMessage{
ID: messageID,
Submitted: time.Now().Unix(),
CallbackURL: callbackURL,
AuthToken: authToken,
ValidEmojis: validEmojs,
}
app.messages[messageID] = newMessage
return *newMessage
}
func (app *app) deleteMessage(messageID string) error {
if _, ok := app.messages[messageID]; !ok {
return errMessageNotFound
}
delete(app.messages, messageID)
log.Printf("message %s deleted", messageID)
return nil
}
func generateRandomString(length int) string {
rand.Seed(time.Now().UTC().UnixNano())
token := make([]byte, length)
rand.Read(token)
return fmt.Sprintf("%x", token)
}
func (app *app) generateNewMessageID() string {
var messageID string
for {
messageID = generateRandomString(10)
if _, exists := app.messages[messageID]; !exists {
break
}
}
return messageID
}