Skip to content

Commit

Permalink
Activity => Object: fully populate labels, source_protocol webmention
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Feb 1, 2023
1 parent 00d4dc9 commit ce2d009
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def inbox(domain=None):
key = Object(id=source, source_protocol='activitypub', domains=domains,
status='complete', as2=activity_as2_str, as1=activity_as1_str,
type=as1_type, object_ids=as1.get_ids(activity_as1, 'object'),
).put()
labels=['feed']).put()
logging.info(f'Wrote Object {key} with {len(domains)} follower domains')

return ''
Expand Down
4 changes: 3 additions & 1 deletion common.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ def send_webmentions(activity_wrapped, proxy=None, **object_props):
logger.info(f'Skipping same-domain webmention from {source} to {target}')
continue

obj = Object(id=source, domains=[domain], **object_props)
# TODO: unify across targets
obj = Object(id=source, domains=[domain], labels=['notification'],
**object_props)
obj.put()
wm_source = (obj.proxy_url()
if verb in ('follow', 'like', 'share') or proxy
Expand Down
6 changes: 3 additions & 3 deletions follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def finish(self, auth_entity, state=None):
follow_json = json_dumps(follow_as2, sort_keys=True)
Follower.get_or_create(dest=id, src=domain, status='active',
last_follow=follow_json)
Object(id=follow_id, domains=[domain], labels=['notification'],
Object(id=follow_id, domains=[domain], labels=['user'],
source_protocol='ui', status='complete', as2=follow_json,
as1=json_dumps(as2.to_as1(follow_as2), sort_keys=True),
).put()
Expand All @@ -191,7 +191,7 @@ def dispatch_request(self):


class UnfollowCallback(indieauth.Callback):
"""IndieAuth callback to add a follower to an existing user."""
"""IndieAuth callback to remove a follower."""
def finish(self, auth_entity, state=None):
if not auth_entity:
return
Expand Down Expand Up @@ -222,7 +222,7 @@ def finish(self, auth_entity, state=None):

follower.status = 'inactive'
follower.put()
Object(id=unfollow_id, domains=[domain], labels=['notification'],
Object(id=unfollow_id, domains=[domain], labels=['user'],
source_protocol='ui', status='complete',
as2=json_dumps(unfollow_as2, sort_keys=True),
as1=json_dumps(as2.to_as1(unfollow_as2), sort_keys=True),
Expand Down
6 changes: 3 additions & 3 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ class Object(StringIdModel):
Key name is the id. We synthesize ids if necessary.
"""
STATUSES = ('new', 'in progress', 'complete', 'failed', 'ignored')
PROTOCOLS = ('activitypub', 'bluesky', 'webmention', 'ui')
LABELS = ('feed', 'notification')
PROTOCOLS = ('activitypub', 'bluesky', 'ostatus', 'webmention', 'ui')
LABELS = ('feed', 'notification', 'user')

# domains of the Bridgy Fed users this activity is to or from
domains = ndb.StringProperty(repeated=True)
Expand All @@ -244,7 +244,7 @@ class Object(StringIdModel):
mf2 = ndb.TextProperty() # HTML microformats2

type = ndb.StringProperty() # AS1 objectType, or verb if it's an activity
deleted = ndb.BooleanProperty(default=False)
deleted = ndb.BooleanProperty()
object_ids = ndb.StringProperty(repeated=True) # id(s) of inner objects

# ActivityPub inbox delivery
Expand Down
8 changes: 7 additions & 1 deletion tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ def _test_inbox_reply(self, reply, expected_props, mock_head, mock_get, mock_pos
source_protocol='activitypub',
status='complete',
as1=as2.to_as1(expected_props['as2']),
labels=['notification'],
**expected_props)

def test_inbox_reply_to_self_domain(self, mock_head, mock_get, mock_post):
Expand Down Expand Up @@ -379,6 +380,7 @@ def test_inbox_create_obj(self, mock_head, mock_get, mock_post):
as1=as2.to_as1(expected_as2),
domains=['foo.com', 'baz.com'],
type='post',
labels=['feed'],
object_ids=[NOTE_OBJECT['id']])

def test_inbox_not_public(self, mock_head, mock_get, mock_post):
Expand Down Expand Up @@ -440,6 +442,7 @@ def _test_inbox_mention(self, mention, expected_props, mock_head, mock_get, mock
status='complete',
as2=expected_as2,
as1=as2.to_as1(expected_as2),
labels=['notification'],
**expected_props)

def test_inbox_like(self, mock_head, mock_get, mock_post):
Expand All @@ -464,7 +467,6 @@ def test_inbox_like(self, mock_head, mock_get, mock_post):
args, kwargs = mock_post.call_args
self.assertEqual(('http://or.ig/webmention',), args)
self.assertEqual({
# TODO
'source': 'http://localhost/render?id=http%3A%2F%2Fth.is%2Flike%23ok',
'target': 'http://or.ig/post',
}, kwargs['data'])
Expand All @@ -476,6 +478,7 @@ def test_inbox_like(self, mock_head, mock_get, mock_post):
as2=LIKE_WITH_ACTOR,
as1=as2.to_as1(LIKE_WITH_ACTOR),
type='like',
labels=['notification'],
object_ids=[LIKE['object']])

def test_inbox_follow_accept_with_id(self, mock_head, mock_get, mock_post):
Expand All @@ -492,6 +495,7 @@ def test_inbox_follow_accept_with_id(self, mock_head, mock_get, mock_post):
as2=follow,
as1=as2.to_as1(follow),
type='follow',
labels=['notification'],
object_ids=[FOLLOW['object']])

follower = Follower.query().get()
Expand Down Expand Up @@ -534,6 +538,7 @@ def test_inbox_follow_accept_with_object(self, mock_head, mock_get, mock_post):
as2=follow,
as1=as2.to_as1(follow),
type='follow',
labels=['notification'],
object_ids=[FOLLOW['object']])

def _test_inbox_follow_accept(self, follow_as2, accept_as2,
Expand Down Expand Up @@ -745,6 +750,7 @@ def test_inbox_no_webmention_endpoint(self, mock_head, mock_get, mock_post):
as2=LIKE_WITH_ACTOR,
as1=as2.to_as1(LIKE_WITH_ACTOR),
type='like',
labels=['notification'],
object_ids=[LIKE['object']])

def test_followers_collection_unknown_user(self, *args):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _test_callback(self, input, expected_follow, mock_get, mock_post):
objects = Object.query().fetch()
self.assert_entities_equal(
[Object(id=id, domains=['snarfed.org'], status='complete',
labels=['notification'], source_protocol='ui',
labels=['user'], source_protocol='ui',
as1=follow_as1, as2=follow_as2)],
objects,
ignore=['created', 'updated'])
Expand Down Expand Up @@ -261,7 +261,7 @@ def test_callback(self, mock_get, mock_post):
self.assert_entities_equal(
[Object(id='http://localhost/user/snarfed.org/following#undo-2022-01-02T03:04:05-https://bar/id',
domains=['snarfed.org'], status='complete',
source_protocol='ui', labels=['notification'],
source_protocol='ui', labels=['user'],
as2=json_dumps(UNDO_FOLLOW, sort_keys=True),
as1=json_dumps(as2.to_as1(UNDO_FOLLOW), sort_keys=True),
)],
Expand Down
27 changes: 18 additions & 9 deletions tests/test_webmention.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,13 @@ def test_create_reply(self, mock_get, mock_post):

self.assert_object('http://a/reply',
domains=['a'],
source_protocol='activitypub',
source_protocol='webmention',
status='complete',
ap_delivered=['https://foo.com/inbox'],
mf2=self.reply_mf2,
as1=self.reply_as1,
type='comment',
labels=['user'],
)

def test_update_reply(self, mock_get, mock_post):
Expand Down Expand Up @@ -616,13 +617,14 @@ def test_create_repost(self, mock_get, mock_post):

self.assert_object('http://a/repost',
domains=['a'],
source_protocol='activitypub',
source_protocol='webmention',
status='complete',
mf2=self.repost_mf2,
as1=self.repost_as1,
ap_delivered=['https://foo.com/inbox'],
type='share',
object_ids=['https://orig/post'],
labels=['user'],
)

def test_link_rel_alternate_as2(self, mock_get, mock_post):
Expand Down Expand Up @@ -768,12 +770,13 @@ def test_create_post_run_task_new(self, mock_get, mock_post):

self.assert_object(f'https://orig/post',
domains=['orig'],
source_protocol='activitypub',
source_protocol='webmention',
status='complete',
mf2=self.create_mf2,
as1=self.create_as1,
ap_delivered=inboxes,
type='note',
labels=['user'],
)

def test_create_post_run_task_resume(self, mock_get, mock_post):
Expand Down Expand Up @@ -809,12 +812,13 @@ def test_create_post_run_task_resume(self, mock_get, mock_post):

self.assert_object(f'https://orig/post',
domains=['orig'],
source_protocol='activitypub',
source_protocol='webmention',
status='complete',
mf2=self.create_mf2,
as1=self.create_as1,
ap_delivered=inboxes + ['https://skipped/inbox'],
type='note',
labels=['user'],
)

def test_create_post_run_task_update(self, mock_get, mock_post):
Expand Down Expand Up @@ -850,12 +854,13 @@ def test_create_post_run_task_update(self, mock_get, mock_post):

self.assert_object(f'https://orig/post',
domains=['orig'],
source_protocol='activitypub',
source_protocol='webmention',
status='complete',
mf2=self.create_mf2,
as1=self.create_as1,
ap_delivered=inboxes,
type='note',
labels=['user'],
)

def test_create_with_image(self, mock_get, mock_post):
Expand Down Expand Up @@ -912,13 +917,14 @@ def test_follow(self, mock_get, mock_post):

self.assert_object('http://a/follow',
domains=['a'],
source_protocol='activitypub',
source_protocol='webmention',
status='complete',
mf2=self.follow_mf2,
as1=self.follow_as1,
ap_delivered=['https://foo.com/inbox'],
type='follow',
object_ids=['http://followee'],
labels=['user'],
)

followers = Follower.query().fetch()
Expand Down Expand Up @@ -978,13 +984,14 @@ def test_follow_fragment(self, mock_get, mock_post):

self.assert_object('http://a/follow#2',
domains=['a'],
source_protocol='activitypub',
source_protocol='webmention',
status='complete',
mf2=self.follow_fragment_mf2,
as1=self.follow_fragment_as1,
ap_delivered=['https://foo.com/inbox'],
type='follow',
object_ids=['http://followee'],
labels=['user'],
)

followers = Follower.query().fetch()
Expand Down Expand Up @@ -1034,13 +1041,14 @@ def test_error(self, mock_get, mock_post):

self.assert_object('http://a/follow',
domains=['a'],
source_protocol='activitypub',
source_protocol='webmention',
status='failed',
mf2=self.follow_mf2,
as1=self.follow_as1,
ap_failed=['https://foo.com/inbox'],
type='follow',
object_ids=['http://followee'],
labels=['user'],
)

def test_repost_blocklisted_error(self, mock_get, mock_post):
Expand Down Expand Up @@ -1119,11 +1127,12 @@ def test_update_profile_run_task(self, mock_get, mock_post):
}
self.assert_object(f'https://orig/',
domains=['orig'],
source_protocol='activitypub',
source_protocol='webmention',
status='complete',
mf2=ACTOR_MF2,
as1=expected_as1,
ap_delivered=['https://inbox', 'https://shared/inbox'],
type='update',
object_ids=['https://orig'],
labels=['user'],
)
6 changes: 3 additions & 3 deletions webmention.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ def try_activitypub(self):
else:
obj = Object(id=self.source_url,
ap_undelivered=list(inboxes_to_targets.keys()),
ap_delivered=[],
ap_failed=[])
ap_delivered=[], ap_failed=[])
logging.info(f'Storing new {obj}')

obj.domains = [self.source_domain]
obj.source_protocol = 'activitypub'
obj.source_protocol = 'webmention'
obj.mf2 = json_dumps(self.source_mf2)
obj.as1 = json_dumps(self.source_as1)
obj.type = type
obj.object_ids = as1.get_ids(self.source_as1, 'object')
obj.labels = ['user']
obj.put()

# TODO: collect by inbox, add 'to' fields, de-dupe inboxes and recipients
Expand Down

0 comments on commit ce2d009

Please sign in to comment.