-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresolver.go
128 lines (114 loc) · 2.62 KB
/
resolver.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
package main
import (
"time"
)
type Resolver struct {
channels map[string]*Channel
users map[string]*User
}
type MessageResolved struct {
User *User
BotID string
Subtype string
MessageTokens []MessageToken
Ts time.Time
}
type MessageToken interface{}
type MessageTokenNewLine struct {
}
type MessageTokenText struct {
Text string
}
type MessageTokenLink struct {
Href string
Text string
}
type MessageTokenChannel struct {
Channel *Channel
Text string
}
type MessageTokenUser struct {
User *User
Text string
}
type MessageTokenVariable struct {
Variable string
Text string
}
type messageTokenListener struct {
resolver *Resolver
Tokens []MessageToken
}
func (l *messageTokenListener) add(token MessageToken) {
l.Tokens = append(l.Tokens, token)
}
func (l *messageTokenListener) OnNewLine() {
l.add(MessageTokenNewLine{})
}
func (l *messageTokenListener) OnText(text string) {
l.add(MessageTokenText{text})
}
func (l *messageTokenListener) OnLink(href, text string) {
text2 := text
if "" == text2 {
text2 = href
}
l.add(MessageTokenLink{href, text2})
}
func (l *messageTokenListener) OnChannel(channelID, alt string) {
channel := l.resolver.ResolveChannel(channelID)
text := alt
if "" == text {
text = channel.Name
}
l.add(MessageTokenChannel{channel, text})
}
func (l *messageTokenListener) OnUser(userID, alt string) {
user := l.resolver.ResolveUser(userID)
text := alt
if "" == text && nil != user {
text = user.Name
}
l.add(MessageTokenUser{user, text})
}
func (l *messageTokenListener) OnVariable(variableID, alt string) {
text := alt
if "" == text {
text = variableID
}
l.add(MessageTokenVariable{variableID, text})
}
func newMessageTokenListener(r *Resolver) *messageTokenListener {
return &messageTokenListener{r, nil}
}
func NewResolver(channels []Channel, users []User) *Resolver {
r := new(Resolver)
r.channels = make(map[string]*Channel)
for i, c := range channels {
r.channels[c.ID] = &channels[i]
}
r.users = make(map[string]*User)
for i, u := range users {
r.users[u.ID] = &users[i]
}
return r
}
func (r *Resolver) ResolveUser(userID string) *User {
return r.users[userID]
}
func (r *Resolver) ResolveChannel(channelID string) *Channel {
return r.channels[channelID]
}
func (r *Resolver) Resolve(m *Message) MessageResolved {
res := new(MessageResolved)
if "" != m.User {
res.User = r.ResolveUser(m.User)
}
res.BotID = m.BotID
res.Ts = SlackTsToTime(m.Ts)
res.Subtype = m.Subtype
messageListner := newMessageTokenListener(r)
NewSlackMessageParser(messageListner).Parse(m.Text)
res.MessageTokens = messageListner.Tokens
return *res
}