-
Notifications
You must be signed in to change notification settings - Fork 3
/
render.go
292 lines (245 loc) · 6.49 KB
/
render.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package slack
import (
"bufio"
"fmt"
"io"
"strings"
"github.com/tchap/steemwatch/notifications/events"
"github.com/pkg/errors"
)
//
// Slack webhook payload
//
type Payload struct {
Text string `json:"text,omitempty"`
Attachments []*Attachment `json:"attachments,omitempty"`
}
type Attachment struct {
Fallback string `json:"fallback"`
Color string `json:"color,omitempty"`
Pretext string `json:"pretext,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Text string `json:"text,omitempty"`
Fields []*Field `json:"fields,omitempty"`
ImageURL string `json:"image_url,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
Footer string `json:"footer,omitempty"`
FooterIcon string `json:"footer_icon,omitempty"`
Timestamp uint64 `json:"ts,omitempty"`
}
type Field struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short,omitempty"`
}
//
// Rendering
//
func makeMessage(attachment *Attachment) *Payload {
return &Payload{
Attachments: []*Attachment{attachment},
}
}
// AccountUpdated
func renderAccountUpdatedEvent(event *events.AccountUpdated) (*Payload, error) {
summary := fmt.Sprintf("@%v's account was updated", event.Op.Account)
return makeMessage(&Attachment{
Title: "Account Update Detected",
Fallback: summary,
Color: "#DC143C",
Text: summary,
}), nil
}
// AccountWitnessVoted
func renderAccountWitnessVotedEvent(event *events.AccountWitnessVoted) (*Payload, error) {
var verb string
if event.Op.Approve {
verb = "approved"
} else {
verb = "unapproved"
}
txt := fmt.Sprintf("@%v %v witness @%v", event.Op.Account, verb, event.Op.Witness)
return &Payload{
Text: txt,
}, nil
}
// TransferMade
func renderTransferMadeEvent(event *events.TransferMade) (*Payload, error) {
op := event.Op
summary := fmt.Sprintf("@%v transferred %v to @%v", op.From, op.Amount, op.To)
attachment := &Attachment{
Fallback: summary,
Color: "#00B2EE",
Pretext: "A transfer you are interested in was made.",
Fields: []*Field{
{
Title: "From",
Value: op.From,
Short: true,
},
{
Title: "To",
Value: op.To,
Short: true,
},
{
Title: "Amount",
Value: op.Amount,
Short: true,
},
},
}
if op.Memo != "" {
attachment.Fields = append(attachment.Fields, &Field{
Title: "Memo",
Value: op.Memo,
})
}
return makeMessage(attachment), nil
}
// UserMentioned
func renderUserMentionedEvent(event *events.UserMentioned) (*Payload, error) {
c := event.Content
txt := fmt.Sprintf("@%v was <https://steemit.com%v|mentioned> by @%v in %v",
event.User, c.URL, c.Author, c.Permlink)
return &Payload{
Text: txt,
}, nil
}
// UserFollowStatusChanged
func renderUserFollowStatusChangedEvent(event *events.UserFollowStatusChanged) (*Payload, error) {
op := event.Op
var txt string
switch {
case event.Followed():
txt = fmt.Sprintf("@%v started following @%v.", op.Follower, op.Following)
case event.Muted():
txt = fmt.Sprintf("@%v muted @%v.", op.Follower, op.Following)
case event.Reset():
txt = fmt.Sprintf("@%v reset the follow status for @%v.", op.Follower, op.Following)
}
return &Payload{
Text: txt,
}, nil
}
// StoryPublished
func renderStoryPublishedEvent(event *events.StoryPublished) (*Payload, error) {
c := event.Content
r := bufio.NewReader(strings.NewReader(c.Body))
summary, err := r.ReadString('\n')
if err != nil {
if err == io.EOF {
if summary == "" {
summary = "<empty>"
}
} else {
return nil, errors.Wrap(err, "failed to get story summary")
}
}
return makeMessage(&Attachment{
Fallback: fmt.Sprintf(`@%v has published "%v".`, c.Author, c.Title),
Color: "#00C957",
Pretext: fmt.Sprintf("@%v has published or updated a story.", c.Author),
Title: c.Title,
TitleLink: "https://steemit.com" + c.URL,
Fields: []*Field{
{
Title: "Summary",
Value: summary,
},
{
Title: "Tags",
Value: fmt.Sprintf("%v", c.JsonMetadata.Tags),
},
},
ThumbURL: "https://steemit.com/images/favicons/favicon-96x96.png",
}), nil
}
// StoryVoted
func renderStoryVotedEvent(event *events.StoryVoted) (*Payload, error) {
o := event.Op
c := event.Content
evt := fmt.Sprintf("@%v cast a vote on a story by @%v.", o.Voter, o.Author)
return makeMessage(&Attachment{
Fallback: evt,
Color: "#BDFCC9",
Pretext: evt,
Title: c.Title,
TitleLink: "https://steemit.com" + c.URL,
Fields: []*Field{
{
Title: "Vote Weight",
Value: fmt.Sprintf("%v", o.Weight),
Short: true,
},
{
Title: "Story Pending Payout",
Value: c.PendingPayoutValue,
Short: true,
},
},
}), nil
}
// CommentPublished
func renderCommentPublishedEvent(event *events.CommentPublished) (*Payload, error) {
c := event.Content
commentLines := make([]string, 0, 5)
scanner := bufio.NewScanner(strings.NewReader(c.Body))
for scanner.Scan() {
commentLines = append(commentLines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrap(err, "failed to read comment body")
}
extractLines := commentLines
if len(extractLines) > 5 {
extractLines = extractLines[:5]
}
extract := strings.Join(extractLines, "\n")
if len(commentLines) > 5 {
extract += fmt.Sprintf("\n<https://steemit.com%v|Read more...>", c.URL)
}
evt := fmt.Sprintf("@%v commented on @%v/%v", c.Author, c.ParentAuthor, c.ParentPermlink)
pre := fmt.Sprintf("@%v <https://steemit.com%v|commented> on @%v/%v",
c.Author, c.URL, c.ParentAuthor, c.ParentPermlink)
return makeMessage(&Attachment{
Fallback: evt,
Color: "#FF9912",
Pretext: pre,
Fields: []*Field{
{
Title: "Comment Body",
Value: extract,
},
},
}), nil
}
// CommentVoted
func renderCommentVotedEvent(event *events.CommentVoted) (*Payload, error) {
o := event.Op
c := event.Content
evt := fmt.Sprintf("@%v cast a vote on comment @%v/%v", o.Voter, o.Author, o.Permlink)
return makeMessage(&Attachment{
Fallback: evt,
Color: "#FFEBCD",
Pretext: evt,
Title: fmt.Sprintf("@%v/%v", c.Author, c.Permlink),
TitleLink: "https://steemit.com" + c.URL,
Fields: []*Field{
{
Title: "Vote Weight",
Value: fmt.Sprintf("%v", o.Weight),
Short: true,
},
{
Title: "Comment Pending Payout",
Value: c.PendingPayoutValue,
Short: true,
},
},
}), nil
}