-
Notifications
You must be signed in to change notification settings - Fork 52
/
models.py
1221 lines (983 loc) · 41 KB
/
models.py
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Datastore model classes."""
from datetime import datetime, timedelta, timezone
import logging
import os
import re
from google.cloud import ndb
from granary import as1
from granary import microformats2
from granary import source as gr_source
from oauth_dropins.indieauth import IndieAuth
from oauth_dropins.instagram import INSTAGRAM_SESSIONID_COOKIE
from oauth_dropins.webutil import webmention
from oauth_dropins.webutil.flask_util import flash
from oauth_dropins.webutil.models import StringIdModel
from oauth_dropins.webutil.util import json_dumps, json_loads
import requests
import superfeedr
import util
logger = logging.getLogger(__name__)
VERB_TYPES = ('post', 'comment', 'like', 'react', 'repost', 'rsvp', 'tag')
PUBLISH_TYPES = VERB_TYPES + ('preview', 'delete')
MAX_AUTHOR_URLS = 5
REFETCH_HFEED_TRIGGER = datetime.fromtimestamp(-1, tz=timezone.utc)
# limit size of block lists stored in source entities to try to keep whole
# entity under 1MB datastore limit:
# https://cloud.google.com/datastore/docs/concepts/limits
BLOCKLIST_MAX_IDS = 20000
# maps string short name to Source subclass. populated by SourceMeta.
sources = {}
def get_type(obj):
"""Returns the :class:`Response` or :class:`Publish` type for an AS object."""
type = obj.get('objectType')
verb = obj.get('verb')
if type == 'activity' and verb == 'share':
return 'repost'
elif type == 'issue':
return 'post'
elif verb in as1.RSVP_VERB_TO_COLLECTION:
return 'rsvp'
elif (type == 'comment' or obj.get('inReplyTo') or
any(o.get('inReplyTo') for o in
(util.get_list(obj, 'object')) + util.get_list(obj, 'context'))):
return 'comment'
elif verb in VERB_TYPES:
return verb
else:
return 'post'
class DisableSource(Exception):
"""Raised when a user has deauthorized our app inside a given platform."""
class SourceMeta(ndb.MetaModel):
""":class:`Source` metaclass. Registers all subclasses in the ``sources`` global."""
def __new__(meta, name, bases, class_dict):
cls = ndb.MetaModel.__new__(meta, name, bases, class_dict)
if cls.SHORT_NAME:
sources[cls.SHORT_NAME] = cls
return cls
class Source(StringIdModel, metaclass=SourceMeta):
"""A silo account, e.g. a Facebook or Google+ account.
Each concrete silo class should subclass this class.
"""
STATUSES = ('enabled', 'disabled')
POLL_STATUSES = ('ok', 'error', 'polling')
# short name for this site type. used in URLs, etc.
SHORT_NAME = None
# the corresponding granary class
GR_CLASS = None
# oauth-dropins Start class
OAUTH_START = None
# oauth-dropins datastore model class
AUTH_MODEL = None
# whether Bridgy supports listen for this silo
CAN_LISTEN = True
# whether Bridgy supports publish for this silo
CAN_PUBLISH = None
# string name of oauth-dropins auth entity property to use as Micropub token
MICROPUB_TOKEN_PROPERTY = None
# whether this source should poll automatically, or only when triggered
# (eg Instagram)
AUTO_POLL = True
# how often to poll for responses
VOLUME_POLL = timedelta(minutes=5)
FAST_POLL = timedelta(minutes=30)
# how often to poll sources that have never sent a webmention
SLOW_POLL = timedelta(days=1)
# how often to poll sources that are currently rate limited by their silo
RATE_LIMITED_POLL = SLOW_POLL
# how long to wait after signup for a successful webmention before dropping to
# the lower frequency poll
FAST_POLL_GRACE_PERIOD = timedelta(days=7)
# how often refetch author url to look for updated syndication links
FAST_REFETCH = timedelta(hours=6)
# refetch less often (this often) if it's been >2w since the last synd link
SLOW_REFETCH = timedelta(days=2)
# rate limiting HTTP status codes returned by this silo. e.g. twitter returns
# 429, instagram 503, google+ 403.
RATE_LIMIT_HTTP_CODES = ('429',)
DISABLE_HTTP_CODES = ('401',)
TRANSIENT_ERROR_HTTP_CODES = ()
# whether granary supports fetching block lists
HAS_BLOCKS = False
# whether to require a u-syndication link for backfeed
BACKFEED_REQUIRES_SYNDICATION_LINK = False
# ignore fragments when comparing syndication links in OPD
IGNORE_SYNDICATION_LINK_FRAGMENTS = False
# convert username to all lower case to use as key name
USERNAME_KEY_ID = False
# Maps Publish.type (e.g. 'like') to source-specific human readable type label
# (e.g. 'favorite'). Subclasses should override this.
TYPE_LABELS = {}
# subclasses should override this
URL_CANONICALIZER = util.UrlCanonicalizer()
# Regexps for URL paths that don't accept incoming webmentions. Currently used
# by Blogger.
PATH_BLOCKLIST = ()
created = ndb.DateTimeProperty(auto_now_add=True, required=True, tzinfo=timezone.utc)
url = ndb.StringProperty()
username = ndb.StringProperty()
status = ndb.StringProperty(choices=STATUSES, default='enabled')
poll_status = ndb.StringProperty(choices=POLL_STATUSES, default='ok')
rate_limited = ndb.BooleanProperty(default=False)
name = ndb.StringProperty() # full human-readable name
picture = ndb.StringProperty()
domains = ndb.StringProperty(repeated=True)
domain_urls = ndb.StringProperty(repeated=True)
features = ndb.StringProperty(repeated=True, choices=util.FEATURES)
superfeedr_secret = ndb.StringProperty()
webmention_endpoint = ndb.StringProperty()
# points to an oauth-dropins auth entity. The model class should be a subclass
# of oauth_dropins.BaseAuth. the token should be generated with the
# offline_access scope so that it doesn't expire.
auth_entity = ndb.KeyProperty()
#
# listen-only properties
#
last_polled = ndb.DateTimeProperty(default=util.EPOCH, tzinfo=timezone.utc)
last_poll_attempt = ndb.DateTimeProperty(default=util.EPOCH, tzinfo=timezone.utc)
last_webmention_sent = ndb.DateTimeProperty(tzinfo=timezone.utc)
last_public_post = ndb.DateTimeProperty(tzinfo=timezone.utc)
recent_private_posts = ndb.IntegerProperty(default=0)
# the last time we re-fetched the author's url looking for updated
# syndication links
last_hfeed_refetch = ndb.DateTimeProperty(default=util.EPOCH, tzinfo=timezone.utc)
# the last time we've seen a rel=syndication link for this Source.
# we won't spend the time to re-fetch and look for updates if there's
# never been one
last_syndication_url = ndb.DateTimeProperty(tzinfo=timezone.utc)
# the last time we saw a syndication link in an h-feed, as opposed to just on
# permalinks. background: https://github.com/snarfed/bridgy/issues/624
last_feed_syndication_url = ndb.DateTimeProperty(tzinfo=timezone.utc)
last_activity_id = ndb.StringProperty()
last_activities_etag = ndb.StringProperty()
last_activities_cache_json = ndb.TextProperty()
seen_responses_cache_json = ndb.TextProperty(compressed=True)
# populated in Poll.poll(), used by handlers
blocked_ids = ndb.JsonProperty(compressed=True)
# maps updated property names to values that put_updates() writes back to the
# datastore transactionally. set this to {} before beginning.
updates = None
# gr_source is *not* set to None by default here, since it needs to be unset
# for __getattr__ to run when it's accessed.
def __init__(self, *args, id=None, **kwargs):
"""Constructor. Escapes the key string id if it starts with ``__``."""
username = kwargs.get('username')
if self.USERNAME_KEY_ID and username and not id:
id = username.lower()
if id and id.startswith('__'):
id = '\\' + id
super().__init__(*args, id=id, **kwargs)
def key_id(self):
"""Returns the key's unescaped string id."""
id = self.key.id()
return id[1:] if id[0] == '\\' else id
@classmethod
def new(cls, **kwargs):
"""Factory method. Creates and returns a new instance for the current user.
To be implemented by subclasses.
"""
raise NotImplementedError()
def __getattr__(self, name):
"""Lazily load the auth entity and instantiate :attr:`self.gr_source`.
Once :attr:`self.gr_source` is set, this method will *not* be called;
:attr:`gr_source` will be returned normally.
"""
if name != 'gr_source':
return getattr(super(), name)
super_attr = getattr(super(), name, None)
if super_attr:
return super_attr
elif not self.auth_entity:
return None
auth_entity = self.auth_entity.get()
try:
refresh_token = auth_entity.refresh_token
self.gr_source = self.GR_CLASS(refresh_token)
return self.gr_source
except AttributeError:
logger.info('no refresh_token')
args = auth_entity.access_token()
if not isinstance(args, tuple):
args = (args,)
kwargs = {}
if self.key.kind() == 'FacebookPage' and auth_entity.type == 'user':
kwargs = {'user_id': self.key_id()}
elif self.key.kind() == 'Instagram':
kwargs = {'scrape': True, 'cookie': INSTAGRAM_SESSIONID_COOKIE}
elif self.key.kind() == 'Mastodon':
args = (auth_entity.instance(),) + args
inst = auth_entity.app.get().instance_info
if inst:
j = json_loads(inst)
truncate_text_length = j.get("configuration", {}).get('statuses', {}).get('max_characters', None) or j.get('max_toot_chars', None)
else:
truncate_text_length = None
kwargs = {
'user_id': json_loads(auth_entity.user_json).get('id'),
# https://docs-develop.pleroma.social/backend/API/differences_in_mastoapi_responses/#instance
'truncate_text_length': truncate_text_length,
}
elif self.key.kind() == 'Twitter':
kwargs = {'username': self.key_id()}
elif self.key.kind() == 'Bluesky':
def store_session(session):
logger.info(f'Storing Bluesky session for {auth_entity.key.id()}: {session}')
auth_entity.session = session
auth_entity.put()
args = []
kwargs = {
'handle': json_loads(auth_entity.user_json).get('handle'),
'did': auth_entity.key.id(),
'app_password': auth_entity.password,
'session_callback': store_session,
}
if auth_entity.session:
kwargs.update({
'access_token': auth_entity.session.get('accessJwt'),
'refresh_token': auth_entity.session.get('refreshJwt'),
})
self.gr_source = self.GR_CLASS(*args, **kwargs)
return self.gr_source
@classmethod
def lookup(cls, id):
"""Returns the entity with the given id.
By default, interprets id as just the key id. Subclasses may extend this to
support usernames, etc.
Ideally, if ``USERNAME_KEY_ID``, normalize to lower case before looking up.
We'd need to backfill all existing entities with upper case key ids, though,
which we're not planning to do. https://github.com/snarfed/bridgy/issues/884
"""
if id and id.startswith('__'):
id = '\\' + id
return ndb.Key(cls, id).get()
def user_tag_id(self):
"""Returns the tag URI for this source, e.g. ``tag:plus.google.com:123456``."""
return self.gr_source.tag_uri(self.key_id())
def bridgy_path(self):
"""Returns the Bridgy page URL path for this source."""
return f'/{self.SHORT_NAME}/{self.key_id()}'
def bridgy_url(self):
"""Returns the Bridgy page URL for this source."""
return util.host_url(self.bridgy_path())
def silo_url(self, handler):
"""Returns the silo account URL, e.g. https://twitter.com/foo."""
raise NotImplementedError()
def label(self):
"""Human-readable label for this source."""
return f'{self.label_name()} ({self.GR_CLASS.NAME})'
def label_name(self):
"""Human-readable name or username for this source, whichever is preferred."""
return self.name or self.key_id()
def post_id(self, url):
"""
Resolve the ID of a post from a URL.
By default calls out to Granary's classmethod but can be
overridden if a URL needs user-specific treatment.
"""
return self.gr_source.post_id(url)
@classmethod
@ndb.transactional()
def put_updates(cls, source):
"""Writes ``source.updates`` to the datastore transactionally.
Returns:
source (Source)
Returns:
``source``, updated
"""
if not source.updates:
return source
to_log = {k: v for k, v in source.updates.items() if not k.endswith('_json')}
logger.info(f'Updating {source.label()} {source.bridgy_path()} : {to_log!r}')
updates = source.updates
source = source.key.get()
source.updates = updates
for name, val in updates.items():
setattr(source, name, val)
source.put()
return source
def poll_period(self):
"""Returns the poll frequency for this source, as a :class:`datetime.timedelta`.
Defaults to ~30m, depending on silo. If we've never sent a webmention for
this source, or the last one we sent was over a month ago, we drop them down
to ~1d after a week long grace period.
"""
now = util.now()
if self.rate_limited:
return self.RATE_LIMITED_POLL
elif now < self.created + self.FAST_POLL_GRACE_PERIOD:
return self.FAST_POLL
elif not self.last_webmention_sent:
return self.SLOW_POLL
elif (self.is_volume_user()
and self.last_webmention_sent > now - timedelta(hours=1)):
return self.VOLUME_POLL
elif self.last_webmention_sent > now - timedelta(days=7):
return self.FAST_POLL
elif self.last_webmention_sent > now - timedelta(days=30):
return self.FAST_POLL * 10
else:
return self.SLOW_POLL
def should_refetch(self):
"""Returns True if we should run OPD refetch on this source now."""
now = util.now()
if self.last_hfeed_refetch == REFETCH_HFEED_TRIGGER:
return True
elif not self.last_syndication_url:
return False
period = (self.FAST_REFETCH
if self.last_syndication_url > now - timedelta(days=14)
else self.SLOW_REFETCH)
return self.last_poll_attempt >= self.last_hfeed_refetch + period
@classmethod
def bridgy_webmention_endpoint(cls, domain='brid.gy'):
"""Returns the Bridgy webmention endpoint for this source type."""
return f'https://{domain}/webmention/{cls.SHORT_NAME}'
def has_bridgy_webmention_endpoint(self):
"""Returns True if this source uses Bridgy's webmention endpoint."""
return self.webmention_endpoint in (
self.bridgy_webmention_endpoint(),
self.bridgy_webmention_endpoint(domain='www.brid.gy'))
def get_author_urls(self):
"""Determine the author urls for a particular source.
In debug mode, replace test domains with localhost.
Return:
list of str: URLs, possibly empty
"""
return [util.replace_test_domains_with_localhost(u) for u in self.domain_urls]
def search_for_links(self):
"""Searches for activities with links to any of this source's web sites.
* https://github.com/snarfed/bridgy/issues/456
* https://github.com/snarfed/bridgy/issues/565
Returns:
list of dict: ActivityStreams activities
"""
return []
def get_activities_response(self, **kwargs):
"""Returns recent posts and embedded comments for this source.
May be overridden by subclasses.
"""
kwargs.setdefault('group_id', gr_source.SELF)
resp = self.gr_source.get_activities_response(**kwargs)
for activity in resp['items']:
self._inject_user_urls(activity)
return resp
def get_activities(self, **kwargs):
return self.get_activities_response(**kwargs)['items']
def get_comment(self, comment_id, **kwargs):
"""Returns a comment from this source.
Passes through to granary by default. May be overridden by subclasses.
Args:
comment_id (str): site-specific comment id
kwargs: passed to :meth:`granary.source.Source.get_comment`
Returns:
dict: decoded ActivityStreams comment object, or None
"""
comment = self.gr_source.get_comment(comment_id, **kwargs)
if comment:
self._inject_user_urls(comment)
return comment
def get_like(self, activity_user_id, activity_id, like_user_id, **kwargs):
"""Returns an ActivityStreams ``like`` activity object.
Passes through to granary by default. May be overridden by subclasses.
Args:
activity_user_id (str): id of the user who posted the original activity
activity_id (str): activity id
like_user_id (str): id of the user who liked the activity
kwargs: passed to :meth:`granary.source.Source.get_comment`
"""
return self.gr_source.get_like(activity_user_id, activity_id, like_user_id,
**kwargs)
def _inject_user_urls(self, activity):
"""Adds this user's web site URLs to their user mentions (in tags), in place."""
obj = activity.get('object') or activity
user_tag_id = self.user_tag_id()
for tag in obj.get('tags', []):
if tag.get('id') == user_tag_id:
tag.setdefault('urls', []).extend([{'value': u} for u in self.domain_urls])
def create_comment(self, post_url, author_name, author_url, content):
"""Creates a new comment in the source silo.
Must be implemented by subclasses.
Args:
post_url (str)
author_name (str)
author_url (str)
content (str)
Returns:
dict: response with at least ``id`` field
"""
raise NotImplementedError()
def feed_url(self):
"""Returns the RSS or Atom (or similar) feed URL for this source.
Must be implemented by subclasses. Currently only implemented by
:mod:`blogger`, :mod:`medium`, :mod:`tumblr`, and :mod:`wordpress_rest`.
Returns:
str: URL
"""
raise NotImplementedError()
def edit_template_url(self):
"""Returns the URL for editing this blog's template HTML.
Must be implemented by subclasses. Currently only implemented by
:mod:`blogger`, :mod:`medium`, :mod:`tumblr`, and :mod:`wordpress_rest`.
Returns:
str: URL
"""
raise NotImplementedError()
def format_for_source_url(self, id):
"""Returns the given id formatted for a URL if necessary.
Some silos use keys containing slashes.
By default this is a no-op - can be overridden by subclasses.
Args:
id: The id to format
Returns:
string formatted id
"""
return id
@classmethod
def button_html(cls, feature, **kwargs):
"""Returns an HTML string with a login form and button for this site.
Mostly just passes through to
:meth:`oauth_dropins.handlers.Start.button_html`.
Returns:
str: HTML
"""
assert set(feature.split(',')) <= set(util.FEATURES)
form_extra = (kwargs.pop('form_extra', '') +
f'<input name="feature" type="hidden" value="{feature}" />')
source = kwargs.pop('source', None)
if source:
form_extra += f'\n<input name="id" type="hidden" value="{source.key_id()}" />'
if cls.OAUTH_START:
return cls.OAUTH_START.button_html(
f'/{cls.SHORT_NAME}/start',
form_extra=form_extra,
image_prefix='/oauth_dropins_static/',
**kwargs)
return ''
@classmethod
@ndb.transactional()
def create_new(cls, user_url=None, **kwargs):
"""Creates and saves a new :class:`Source` and adds a poll task for it.
Args:
user_url (str): if provided, supersedes other urls when determining the
``author_url``
kwargs: passed to :meth:`new()`
Returns:
Source: newly created entity
"""
source = cls.new(**kwargs)
if source is None:
return None
auth_entity = kwargs.get('auth_entity')
# defer to the source if it already set domain_urls
if not source.domain_urls and auth_entity and hasattr(auth_entity, 'user_json'):
source.domain_urls, source.domains = source.urls_and_domains(
auth_entity, user_url)
logger.debug(f'URLs/domains: {source.domain_urls} {source.domains}')
# check if this source already exists
existing = source.key.get()
if existing:
# merge some fields
if not auth_entity or not auth_entity.SCOPES_RESET:
source.features = set(source.features + existing.features)
source.populate(**existing.to_dict(include=(
'created', 'last_hfeed_refetch', 'last_poll_attempt', 'last_polled',
'last_syndication_url', 'last_webmention_sent', 'superfeedr_secret',
'webmention_endpoint')))
verb = 'Updated'
else:
verb = 'Added'
author_urls = source.get_author_urls()
link = ('http://indiewebify.me/send-webmentions/?url=' + author_urls[0]
if author_urls else 'http://indiewebify.me/#send-webmentions')
feature = source.features[0] if source.features else 'listen'
blurb = '%s %s. %s' % (
verb, source.label(),
'Try previewing a post from your web site!' if feature == 'publish'
else '<a href="%s">Try a webmention!</a>' % link if feature == 'webmention'
else "Refresh in a minute to see what we've found!")
logger.info(f'{blurb} {source.bridgy_url()}')
source.verify()
if source.verified():
flash(blurb)
source.put()
if 'webmention' in source.features:
try:
superfeedr.subscribe(source)
except BaseException as e:
code, _ = util.interpret_http_exception(e)
if (code in superfeedr.TRANSIENT_ERROR_HTTP_CODES or
util.is_connection_failure(e)):
flash('Apologies, <a href="https://superfeedr.com/">Superfeedr</a> is having technical difficulties. Please try again later!')
return None
raise
if 'listen' in source.features and source.AUTO_POLL:
util.add_poll_task(source, now=True)
util.add_poll_task(source)
return source
def verified(self):
"""Returns True if this source is ready to be used, False otherwise.
See :meth:`verify()` for details. May be overridden by subclasses, e.g.
:class:`tumblr.Tumblr`.
"""
if not self.domains or not self.domain_urls:
return False
if 'webmention' in self.features and not self.webmention_endpoint:
return False
if ('listen' in self.features and
not (self.webmention_endpoint or self.last_webmention_sent)):
return False
return True
def verify(self, force=False):
"""Checks that this source is ready to be used.
For blog and listen sources, this fetches their front page HTML and
discovers their webmention endpoint. For publish sources, this checks that
they have a domain.
May be overridden by subclasses, e.g. :class:`tumblr.Tumblr`.
Args:
force (bool): if True, fully verifies (e.g. re-fetches the blog's HTML and
performs webmention discovery) even we already think this source is
verified.
"""
author_urls = [u for u, d in zip(self.get_author_urls(), self.domains)
if not util.in_webmention_blocklist(d)]
if ((self.verified() and not force) or self.status == 'disabled' or
not self.features or not author_urls):
return
author_url = author_urls[0]
try:
got = webmention.discover(author_url, timeout=util.HTTP_TIMEOUT)
self.webmention_endpoint = got.endpoint
self._fetched_html = got.response.text
except BaseException as e:
logger.info('Error discovering webmention endpoint', exc_info=e)
self.webmention_endpoint = None
self.put()
def urls_and_domains(self, auth_entity, user_url, actor=None,
resolve_source_domain=True):
"""Returns this user's valid (not webmention-blocklisted) URLs and domains.
Converts the auth entity's ``user_json`` to an ActivityStreams actor and
uses its ``urls`` and ``url`` fields. May be overridden by subclasses.
Args:
auth_entity (oauth_dropins.models.BaseAuth)
user_url (str): optional URL passed in when authorizing
actor (dict): optional AS actor for the user. If provided, overrides
auth_entity
resolve_source_domain (bool): whether to follow redirects on URLs on
this source's domain
Returns:
([str url, ...], [str domain, ...]) tuple:
"""
if not actor:
actor = self.gr_source.user_to_actor(json_loads(auth_entity.user_json))
logger.debug(f'Extracting URLs and domains from actor: {json_dumps(actor, indent=2)}')
candidates = util.trim_nulls(util.uniquify(
[user_url] + as1.object_urls(actor)))
if len(candidates) > MAX_AUTHOR_URLS:
logger.info(f'Too many profile links! Only resolving the first {MAX_AUTHOR_URLS}: {candidates}')
urls = []
for i, url in enumerate(candidates):
on_source_domain = util.domain_from_link(url) == self.gr_source.DOMAIN
resolve = ((resolve_source_domain or not on_source_domain) and
i < MAX_AUTHOR_URLS)
resolved = self.resolve_profile_url(url, resolve=resolve)
if resolved:
urls.append(resolved)
final_urls = []
domains = []
for url in util.dedupe_urls(urls): # normalizes domains to lower case
# skip links on this source's domain itself. only currently needed for
# Mastodon; the other silo domains are in the webmention blocklist.
domain = util.domain_from_link(url)
if domain != self.gr_source.DOMAIN:
final_urls.append(url)
domains.append(domain)
return final_urls, domains
@staticmethod
def resolve_profile_url(url, resolve=True):
"""Resolves a profile URL to be added to a source.
Args:
url (str)
resolve (bool): whether to make HTTP requests to follow redirects, etc.
Returns:
str: resolved URL, or None
"""
final, _, ok = util.get_webmention_target(url, resolve=resolve)
if not ok:
return None
final = final.lower()
if util.schemeless(final).startswith(util.schemeless(url.lower())):
# redirected to a deeper path. use the original higher level URL. #652
final = url
# If final has a path segment check if root has a matching rel=me.
match = re.match(r'^(https?://[^/]+)/.+', final)
if match and resolve:
root = match.group(1)
try:
mf2 = util.fetch_mf2(root)
me_urls = mf2['rels'].get('me', [])
if final in me_urls:
final = root
except requests.RequestException:
logger.warning(f"Couldn't fetch {root}, preserving path in {final}", exc_info=True)
return final
def canonicalize_url(self, url, activity=None, **kwargs):
"""Canonicalizes a post or object URL.
Wraps :class:`oauth_dropins.webutil.util.UrlCanonicalizer`.
"""
return self.URL_CANONICALIZER(url, **kwargs) if self.URL_CANONICALIZER else url
def infer_profile_url(self, url):
"""Given a silo profile, tries to find the matching Bridgy user URL.
Queries Bridgy's registered accounts for users with a particular
domain in their silo profile.
Args:
url (str): a person's URL
Return:
str: URL for their profile on this service, or None
"""
domain = util.domain_from_link(url)
if domain == self.gr_source.DOMAIN:
return url
user = self.__class__.query(self.__class__.domains == domain).get()
if user:
return self.gr_source.user_url(user.key_id())
def preprocess_for_publish(self, obj):
"""Preprocess an object before trying to publish it.
By default this tries to massage person tags so that the tag's
``url`` points to the person's profile on this service (as opposed
to a person's homepage).
The object is modified in place.
Args:
obj (dict): ActivityStreams activity or object
"""
if isinstance(obj, str):
return obj
for tag in obj.get('tags', []):
if tag.get('objectType') == 'person':
silo_url = None
for url in as1.object_urls(tag):
silo_url = url and self.infer_profile_url(url)
if silo_url:
break
if silo_url:
tag['url'] = silo_url
# recurse on contained object(s)
for obj in util.get_list(obj, 'object'):
self.preprocess_for_publish(obj)
def on_new_syndicated_post(self, syndpost):
"""Called when a new :class:`SyndicatedPost` is stored for this source.
Args:
syndpost (SyndicatedPost)
"""
pass
def is_private(self):
"""Returns True if this source is private aka protected.
...ie their posts are not public.
"""
return False
def is_beta_user(self):
"""Returns True if this is a "beta" user opted into new features.
Beta users come from ``beta_users.txt``.
"""
return self.bridgy_path() in util.BETA_USER_PATHS
def is_volume_user(self):
"""Returns True if this is a "volume" user special cased to poll faster.
Volume users come from ``volume_users.txt``.
"""
return self.bridgy_path() in util.VOLUME_USER_PATHS
def load_blocklist(self):
"""Fetches this user's blocklist, if supported, and stores it in the entity."""
if not self.HAS_BLOCKS:
return
try:
ids = self.gr_source.get_blocklist_ids()
except gr_source.RateLimited as e:
ids = e.partial or []
self.blocked_ids = ids[:BLOCKLIST_MAX_IDS]
self.put()
def is_blocked(self, obj):
"""Returns True if an object's author is being blocked.
...ie they're in this user's block list.
Note that this method is tested in test_twitter.py, not test_models.py, for
historical reasons.
"""
if not self.blocked_ids:
return False
for o in [obj] + util.get_list(obj, 'object'):
for field in 'author', 'actor':
if o.get(field, {}).get('numeric_id') in self.blocked_ids:
return True
class Webmentions(StringIdModel):
"""A bundle of links to send webmentions for.
Use the :class:`Response` and :class:`BlogPost` concrete subclasses below.
"""
STATUSES = ('new', 'processing', 'complete', 'error')
source = ndb.KeyProperty()
status = ndb.StringProperty(choices=STATUSES, default='new')
leased_until = ndb.DateTimeProperty(tzinfo=timezone.utc)
created = ndb.DateTimeProperty(auto_now_add=True, tzinfo=timezone.utc)
updated = ndb.DateTimeProperty(auto_now=True, tzinfo=timezone.utc)
# Original post links, ie webmention targets
sent = ndb.StringProperty(repeated=True)
unsent = ndb.StringProperty(repeated=True)
error = ndb.StringProperty(repeated=True)
failed = ndb.StringProperty(repeated=True)
skipped = ndb.StringProperty(repeated=True)
def label(self):
"""Returns a human-readable string description for use in log messages.
To be implemented by subclasses.
"""
raise NotImplementedError()
def add_task(self):
"""Adds a propagate task for this entity.
To be implemented by subclasses.
"""
raise NotImplementedError()
@ndb.transactional()
def get_or_save(self):
entity = existing = self.key.get()
propagate = False
if entity:
# merge targets
urls = set(entity.sent + entity.unsent + entity.error +
entity.failed + entity.skipped)
for field in ('sent', 'unsent', 'error', 'failed', 'skipped'):
entity_urls = getattr(entity, field)
new_urls = set(getattr(self, field)) - urls
entity_urls += new_urls
if new_urls and field in ('unsent', 'error'):
propagate = True
else:
entity = self
propagate = self.unsent or self.error
if propagate:
logger.debug(f'New webmentions to propagate! {entity.label()}')
entity.add_task()
elif not existing:
entity.status = 'complete'
entity.put()
return entity
def restart(self):
"""Moves status and targets to 'new' and adds a propagate task."""
self.status = 'new'
self.unsent = util.dedupe_urls(self.unsent + self.sent + self.error +
self.failed + self.skipped)
self.sent = self.error = self.failed = self.skipped = []
# clear any cached webmention endpoints
with util.webmention_endpoint_cache_lock:
for url in self.unsent:
util.webmention_endpoint_cache.pop(util.webmention_endpoint_cache_key(url), None)
# this datastore put and task add should be transactional, but Cloud Tasks
# doesn't support that :(
# https://cloud.google.com/appengine/docs/standard/python/taskqueue/push/migrating-push-queues#features-not-available
# https://github.com/googleapis/python-tasks/issues/26
#
# The new "bundled services" bridge for the old App Engine APIs still
# supports them, but only because that's literally on the old backends,
# which seems like a dead end.
# https://groups.google.com/g/google-appengine/c/22BKInlWty0/m/05ObNEdsAgAJ
self.put()
self.add_task()
class Response(Webmentions):
"""A comment, like, or repost to be propagated.
The key name is the comment object id as a tag URI.
"""
# ActivityStreams JSON activity and comment, like, or repost
type = ndb.StringProperty(choices=VERB_TYPES, default='comment')
# These are TextProperty, and not JsonProperty, so that their plain text is
# visible in the App Engine admin console. (JsonProperty uses a blob. :/)
activities_json = ndb.TextProperty(repeated=True)
response_json = ndb.TextProperty()
# Old values for response_json. Populated when the silo reports that the
# response has changed, e.g. the user edited a comment or changed their RSVP
# to an event. Currently unused, kept for historical records only.
old_response_jsons = ndb.TextProperty(repeated=True)
# JSON dict mapping original post url to activity index in activities_json.
# only set when there's more than one activity.
urls_to_activity = ndb.TextProperty()
# Original post links found by original post discovery
original_posts = ndb.StringProperty(repeated=True)
def label(self):
return ' '.join((self.key.kind(), self.type, self.key.id(),
json_loads(self.response_json).get('url', '[no url]')))
def add_task(self):
util.add_propagate_task(self)
@staticmethod
def get_type(obj):
type = get_type(obj)
return type if type in VERB_TYPES else 'comment'
def get_or_save(self, source, restart=False):
resp = super().get_or_save()
if (self.type != resp.type or
as1.activity_changed(json_loads(resp.response_json),
json_loads(self.response_json),
log=True)):
logger.info(f'Response changed! Re-propagating. Original: {resp}')
resp.old_response_jsons = [resp.response_json] + resp.old_response_jsons[:10]