forked from muesli/beehive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastodonbee.go
350 lines (313 loc) · 8.01 KB
/
mastodonbee.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (C) 2018 Nicolas Martin
* 2018 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Nicolas Martin <[email protected]>
* Christian Muehlhaeuser <[email protected]>
*/
// Package mastodonbee is a Bee that can connect to mastodon.
package mastodonbee
import (
"context"
mastodon "github.com/mattn/go-mastodon"
"github.com/muesli/beehive/bees"
)
// mastodonBee is a Bee that can connect to mastodon.
type mastodonBee struct {
bees.Bee
server string
clientID string
clientSecret string
email string
password string
client *mastodon.Client
evchan chan bees.Event
}
// Action triggers the action passed to it.
func (mod *mastodonBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}
switch action.Name {
case "toot":
var text string
action.Options.Bind("text", &text)
mod.Logf("Attempting to post \"%s\" to Mastodon", text)
// Post status toot on mastodon, event gets triggered automatically via
// the toot_fetched even.
_, err := mod.client.PostStatus(context.Background(), &mastodon.Toot{
Status: text,
})
if err != nil {
mod.LogErrorf("Error sending toot: %v", err)
}
case "delete_toot":
var id string
action.Options.Bind("id", &id)
mod.Logf("Attempting to delete toot \"%s\"", id)
// Event is automatically handled in handleStreamEvent()
err := mod.client.DeleteStatus(context.Background(), mastodon.ID(id))
if err != nil {
mod.LogErrorf("Error deleting toot: %v", err)
}
case "get_toots": // returns the current user's toots
mod.Logf("Attempting to get current user")
acc, err := mod.client.GetAccountCurrentUser(context.Background())
if err != nil {
mod.LogErrorf("Error getting current user: %v", err)
return outs
}
mod.Logf("Attempting to get current user's toots")
statuses, err := mod.client.GetAccountStatuses(context.Background(), acc.ID, &mastodon.Pagination{})
if err != nil {
mod.LogErrorf("Error getting current user's toots: %v", err)
return outs
}
// create a toot_fetched event for every status
for _, status := range statuses {
mod.handleStatus(status)
}
case "follow":
var id string
action.Options.Bind("id", &id)
mod.Logf("Attempting to follow account: %s", id)
rel, err := mod.client.AccountFollow(context.Background(), mastodon.ID(id))
if err != nil {
mod.LogErrorf("Failed to follow account %s: %v", id, err)
return outs
}
ev := bees.Event{
Bee: mod.Name(),
Name: "followed",
Options: []bees.Placeholder{
{
Name: "user_id",
Value: id,
Type: "string",
},
{
Name: "following",
Value: rel.Following,
Type: "bool",
},
{
Name: "requested",
Value: rel.Requested,
Type: "bool",
},
{
Name: "followed_by",
Value: rel.FollowedBy,
Type: "bool",
},
},
}
mod.evchan <- ev
case "unfollow":
var id string
action.Options.Bind("id", &id)
mod.Logf("Attempting to unfollow account: %s", id)
rel, err := mod.client.AccountUnfollow(context.Background(), mastodon.ID(id))
if err != nil {
mod.LogErrorf("Failed to unfollow account %s: %v", id, err)
return outs
}
ev := bees.Event{
Bee: mod.Name(),
Name: "unfollowed",
Options: []bees.Placeholder{
{
Name: "user_id",
Value: id,
Type: "string",
},
{
Name: "following",
Value: rel.Following,
Type: "bool",
},
{
Name: "followed_by",
Value: rel.FollowedBy,
Type: "bool",
},
},
}
mod.evchan <- ev
case "favourite":
var id string
action.Options.Bind("id", &id)
mod.Logf("Attempting to favourite toot: %s", id)
status, err := mod.client.Favourite(context.Background(), mastodon.ID(id))
if err != nil {
mod.LogErrorf("Failed to favourite toot: %v", err)
}
ev := bees.Event{
Bee: mod.Name(),
Name: "favourited",
Options: []bees.Placeholder{
{
Name: "id",
Value: id,
Type: "string",
},
{
Name: "user_id",
Value: string(status.Account.ID),
Type: "string",
},
{
Name: "username",
Value: status.Account.DisplayName,
Type: "string",
},
{
Name: "text",
Value: status.Content,
Type: "string",
},
{
Name: "url",
Value: status.URL,
Type: "string",
},
{
Name: "favourites",
Value: status.FavouritesCount,
Type: "int64",
},
},
}
mod.evchan <- ev
case "reblog":
var id string
action.Options.Bind("id", &id)
mod.Logf("Attempting to reblog toot %s", id)
// reblog-Event should automatically be handled in handleStreamEvent()
status, err := mod.client.Reblog(context.Background(), mastodon.ID(id))
if err != nil {
mod.LogErrorf("Failed to reblog toot: %v", err)
return outs
}
ev := bees.Event{
Bee: mod.Name(),
Name: "reblogged",
Options: []bees.Placeholder{
{
Name: "user_id",
Value: string(status.Account.ID),
Type: "string",
},
{
Name: "username",
Value: status.Account.DisplayName,
Type: "string",
},
{
Name: "text",
Value: status.Content,
Type: "string",
},
{
Name: "url",
Value: status.URL,
Type: "string",
},
{
Name: "reblogs",
Value: status.ReblogsCount,
Type: "int64",
},
},
}
mod.evchan <- ev
default:
mod.LogErrorf("Unkown action: %s", action.Name)
}
return outs
}
func (mod *mastodonBee) handleStreamEvent(item interface{}) {
switch e := item.(type) {
case *mastodon.UpdateEvent:
mod.handleStatus(e.Status)
case *mastodon.NotificationEvent:
mod.handleNotification(e.Notification)
case *mastodon.DeleteEvent:
ev := bees.Event{
Bee: mod.Name(),
Name: "deleted",
Options: []bees.Placeholder{
{
Name: "id",
Value: string(e.ID),
Type: "string",
},
},
}
mod.evchan <- ev
default:
mod.LogErrorf("Unkown event: %+v", e)
}
}
func (mod *mastodonBee) handleStream() {
timeline, err := mod.client.StreamingUser(context.Background())
if err != nil {
mod.LogErrorf("Failed to get user stream: %+v", err)
return
}
for {
select {
case <-mod.SigChan:
return
case item := <-timeline:
mod.handleStreamEvent(item)
}
}
}
// Run executes the Bee's event loop.
func (mod *mastodonBee) Run(eventChan chan bees.Event) {
// Create the new api client
c := mastodon.NewClient(&mastodon.Config{
Server: mod.server,
ClientID: mod.clientID,
ClientSecret: mod.clientSecret,
})
// authorize it
err := c.Authenticate(context.Background(), mod.email, mod.password)
if err != nil {
mod.LogErrorf("Authorization failed, make sure the mastodon credentials are correct: %s", err)
return
}
// try to get user account
acc, err := c.GetAccountCurrentUser(context.Background())
if err != nil {
mod.LogErrorf("Failed to get current user account: %v", err)
}
mod.Logf("Successfully logged in: %s", acc.URL)
// set client
mod.client = c
// set and start eventchan
mod.evchan = eventChan
mod.handleStream()
}
// ReloadOptions parses the config options and initializes the Bee.
func (mod *mastodonBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
options.Bind("server", &mod.server)
options.Bind("client_id", &mod.clientID)
options.Bind("client_secret", &mod.clientSecret)
options.Bind("email", &mod.email)
options.Bind("password", &mod.password)
}