summaryrefslogtreecommitdiff
path: root/postgresqleu/util/messaging/mastodon.py
blob: 25437a151c9e15eb60fb552b694ecfdae45e370c (plain)
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
from django.core.validators import ValidationError
from django import forms
from django.utils import timezone
from django.utils.html import strip_tags

import re
import requests_oauthlib
import requests
import dateutil.parser

from postgresqleu.util.widgets import StaticTextWidget
from postgresqleu.util.forms import LinkForCodeField
from postgresqleu.util.oauthapps import get_oauth_client, get_oauth_secret
from postgresqleu.util.models import OAuthApplication
from postgresqleu.util.messaging import re_token

from postgresqleu.confreg.backendforms import BackendSeriesMessagingForm
from postgresqleu.confreg.models import ConferenceRegistration, IncomingDirectMessage

from .util import send_reg_direct_message, ratelimiter
from .common import register_messaging_config


# We always ask for this scope
MASTODON_SCOPES = "read write:statuses write:media"


class MastodonBackendForm(BackendSeriesMessagingForm):
    initialconfig = LinkForCodeField(label='Get authorization code')
    mastodoninfo = forms.CharField(widget=StaticTextWidget, label="Account information", required=False)

    def __init__(self, *args, **kwargs):
        self.baseurl = None
        super().__init__(*args, **kwargs)

    def fix_fields(self):
        super().fix_fields()

        if self.baseurl:
            self.instance.config['baseurl'] = self.baseurl.rstrip('/')

        if self.instance.config.get('token', None):
            del self.fields['initialconfig']
            self.config_fields = ['mastodoninfo', ]
            self.config_fieldsets = [
                {'id': 'mastodon', 'legend': 'Mastodon', 'fields': ['mastodoninfo', ]},
            ]
            self.config_readonly_fields = ['mastodoninfo', ]

            try:
                if 'username' not in self.instance.config:
                    self.instance.config.update(Mastodon(self.instance.id, self.instance.config).get_account_info())
                    self.instance.save(update_fields=['config'])
                selfinfo = "Connected to mastodon account @{}.".format(self.instance.config['username'])
            except Exception as e:
                selfinfo = "ERROR verifying Mastodon access: {}".format(e)

            self.initial.update({
                'mastodoninfo': selfinfo,
            })
        else:
            # Not configured yet, so prepare for it!
            del self.fields['mastodoninfo']
            self.config_fields = ['initialconfig', ]
            self.config_fieldsets = [
                {'id': 'mastodon', 'legend': 'Mastodon', 'fields': ['initialconfig', ]},
            ]
            self.nosave_fields = ['initialconfig', ]

            # Ugly power-grab here, but let's see what's in our POST
            if self.request.POST.get('initialconfig', None):
                # Token is included, so don't try to get a new one
                self.fields['initialconfig'].widget.authurl = self.request.session['authurl']
            else:
                auth_url, state = self._get_oauth_session().authorization_url('{}/oauth/authorize'.format(self.instance.config['baseurl']))
                self.request.session['authurl'] = auth_url

                self.fields['initialconfig'].widget.authurl = auth_url

    def clean(self):
        d = super().clean()
        if d.get('initialconfig', None):
            # We have received an initial config, so try to attach ourselves to mastodon
            try:
                tokens = self._get_oauth_session().fetch_token(
                    '{}/oauth/token'.format(self.instance.config['baseurl']),
                    code=d.get('initialconfig'),
                    client_secret=get_oauth_secret(self.instance.config['baseurl']),
                    scopes=MASTODON_SCOPES
                )

                self.instance.config['token'] = tokens['access_token']
                del self.request.session['authurl']
                self.request.session.modified = True
            except Exception as e:
                self.add_error('initialconfig', 'Could not set up Mastodon: {}'.format(e))
                self.add_error('initialconfig', 'You probably have to restart the process')
        return d

    def _get_oauth_session(self):
        return requests_oauthlib.OAuth2Session(
            get_oauth_client(self.instance.config['baseurl']),
            redirect_uri='urn:ietf:wg:oauth:2.0:oob',
            scope=MASTODON_SCOPES
        )


class Mastodon(object):
    provider_form_class = MastodonBackendForm
    can_process_incoming = True
    can_broadcast = True
    can_notification = True
    direct_message_max_length = 450  # 500 is lenght, draw down some to handle username
    typename = 'Mastodon'
    max_post_length = 500

    handle_regexp = re.compile(r'^@([A-Z0-9._%+-]+)@([A-Z0-9.-]+\.[A-Z]{2,})$', re.I)

    @classmethod
    def can_track_users_for(self, whatfor):
        return True

    @classmethod
    def get_field_help(self, whatfor):
        return 'Enter Mastodon username in the format @user@site (e.g. @someone@mastodon.social).'

    @classmethod
    def validate_baseurl(self, baseurl):
        if not OAuthApplication.objects.filter(name='mastodon', baseurl=baseurl).exists():
            return 'Global OAuth credentials for {} missing'.format(baseurl)

    @classmethod
    def clean_identifier_form_value(self, whatfor, value):
        if not self.handle_regexp.fullmatch(value):
            raise ValidationError("Invalid format of Mastodon username. Must use format @name@site.")
        return value

    @classmethod
    def get_link_from_identifier(self, value):
        m = self.handle_regexp.fullmatch(value)
        if not m:
            return None
        return 'https://{}/@{}'.format(m.group(2), m.group(1))

    def __init__(self, providerid, config):
        self.providerid = providerid
        self.providerconfig = config

        self.authheaders = {
            'Authorization': 'Bearer {}'.format(self.providerconfig['token']),
        }

    def _api_url(self, url):
        return '{}{}'.format(self.providerconfig['baseurl'], url)

    def _get(self, url, *args, **kwargs):
        ratelimiter.limit(self.providerconfig['baseurl'])
        return requests.get(
            self._api_url(url),
            timeout=30,
            headers=self.authheaders,
            *args,
            **kwargs
        )

    def _post(self, url, *args, **kwargs):
        ratelimiter.limit(self.providerconfig['baseurl'])
        return requests.post(
            self._api_url(url),
            timeout=30,
            headers=self.authheaders,
            *args,
            **kwargs,
        )

    def get_account_info(self):
        r = self._get('/api/v1/accounts/verify_credentials')
        r.raise_for_status()
        j = r.json()
        return {
            'username': j['username'],
        }

    def post(self, toot, image=None, replytotweetid=None):
        d = {
            'status': toot,
            'visibility': 'public',
        }
        if replytotweetid:
            d['in_reply_to_id'] = replytotweetid

        if image:
            r = self._post('/api/v1/media', files={
                'file': bytearray(image),
            })
            if r.status_code != 200:
                return (None, 'Media upload: {}'.format(r.text))
            d['media_ids'] = [int(r.json()['id']), ]

        r = self._post('/api/v1/statuses', json=d)
        if r.status_code != 200:
            return (None, r.text)

        return (r.json()['id'], None)

    def repost(self, postid):
        r = self._post('/api/v1/statuses/{}/reblog'.format(postid))
        if r.status_code != 200:
            return (None, r.text)
        return (True, None)

    def send_direct_message(self, recipient_config, msg):
        d = {
            'status': '@{} {}'.format(recipient_config['username'], msg),
            'visibility': 'direct',
        }

        r = self._post('/api/v1/statuses', json=d)
        r.raise_for_status()

    def poll_public_posts(self, lastpoll, checkpoint):
        p = {
            'limit': 200,  # If it's this many, we should give up
            'exclude_types[]': ['follow', 'favourite', 'reblog', 'poll', 'follow_request'],
        }
        if checkpoint:
            p['since_id'] = checkpoint

        r = self._get('/api/v1/notifications', params=p)
        r.raise_for_status()

        for n in r.json():
            if n['type'] != 'mention':
                # Sometimes  Mastodon may include a type that we don't know about, since it hadn't yet
                # been added to the exclude_types. So ignore them if they show up.
                continue

            s = n['status']
            d = {
                'id': int(s['id']),
                'datetime': dateutil.parser.parse(s['created_at']),
                'text': strip_tags(s['content']),
                'replytoid': s['in_reply_to_id'] and int(s['in_reply_to_id']) or None,
                'author': {
                    'name': s['account']['display_name'] or s['account']['username'],
                    'username': s['account']['username'],
                    'id': s['account']['id'],
                    'imageurl': s['account']['avatar_static'],
                },
                'media': [m['url'] for m in s['media_attachments']],
            }
            # (mastodon doesn't have quoted status, so just leave that one non-existing)
            yield d

    def poll_incoming_private_messages(self, lastpoll, checkpoint):
        p = {
            'limit': 40,
        }
        if checkpoint:
            p['since_id'] = checkpoint

        r = self._get('/api/v1/conversations', params=p)
        r.raise_for_status()

        j = r.json()
        for c in j:
            if len(c['accounts']) > 1:
                # Can't handle group messages
                continue
            ls = c['last_status']
            self.process_incoming_dm_struct(ls)

        if len(j):
            # For some reason, it paginates by last_status->id, and not by id. Go figure.
            return timezone.now(), max((c['last_status']['id'] for c in j))
        else:
            return timezone.now(), checkpoint

    def process_incoming_dm_struct(self, s):
        if s['visibility'] != 'direct':
            # We're only supposed to collect direct messages. Which
            # isn't really direct messages when it comes to mastodon,
            # but they have a visibility of direct.
            return

        postid = int(s['id'])
        if IncomingDirectMessage.objects.filter(provider_id=self.providerid, postid=postid).exists():
            # Already seen this one, so ignore it
            return

        dm = IncomingDirectMessage(
            provider_id=self.providerid,
            postid=postid,
            time=dateutil.parser.parse(s['created_at']),
            sender={
                'name': s['account']['display_name'] or s['account']['username'],
                'username': s['account']['username'],
                'id': s['account']['id'],
                'imageurl': s['account']['avatar_static'],
            },
            txt=strip_tags(s['content']),
        )
        self.process_incoming_dm(dm)
        dm.save()

    def process_incoming_dm(self, msg):
        register_messaging_config(msg, self)

    def get_regconfig_from_dm(self, dm):
        # Return a structure to store in messaging_config corresponding to the dm
        return {
            'username': dm.sender['username'],
        }

    def get_regdisplayname_from_config(self, config):
        return config.get('username', '<unspecified>')

    def get_public_url(self, post):
        return '{}@{}/{}'.format(self.providerconfig['baseurl'], post.author_screenname, post.statusid)

    def get_attendee_string(self, token, messaging, attendeeconfig):
        if 'username' in attendeeconfig:
            return "Your notifications will be sent to @{}.".format(attendeeconfig['username']), None
        else:
            return 'mastodon_invite.html', {
                'mastodonname': self.providerconfig['username'],
                'token': token,
            }

    def check_messaging_config(self, state):
        # Check that we can get our own account info
        try:
            self.get_account_info()
        except Exception as e:
            return False, 'Could not get own account information: {}'.format(e)
        return True, ''

    def get_link(self, id):
        return 'mastodon', '{}/@{}/{}'.format(self.providerconfig['baseurl'].rstrip('/'), self.providerconfig['username'], id)