Skip to content

Commit

Permalink
as2: add new is_server_actor function
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Aug 4, 2024
1 parent 237aa50 commit c5f7bf5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ Changelog
* Add new `is_dm` function.
* `as2`:
* Add [`sensitive`](https://swicg.github.io/miscellany/#sensitive) support.
* Add new `is_server_actor` function ([FEP-d556](https://codeberg.org/fediverse/fep/src/branch/main/fep/d556/fep-d556.md), [discussion](https://socialhub.activitypub.rocks/t/fep-d556-server-level-actor-discovery-using-webfinger/3861)).
* `atom`:
* `atom_to_activity/ies`: Get URL from `link` for activities as well as objects. ([Thanks @imax9000!](https://github.com/snarfed/granary/issues/752))
* `bluesky`:
Expand Down
38 changes: 38 additions & 0 deletions granary/as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ def _invert(d):
# https://codeberg.org/fediverse/fep/src/branch/main/fep/e232/fep-e232.md#user-content-examples
QUOTE_RE_SUFFIX = re.compile(r'\s+RE: <?[^\s]+>?\s?$')

# https://socialhub.activitypub.rocks/t/fep-d556-server-level-actor-discovery-using-webfinger/3861/3
# https://codeberg.org/fediverse/fep/src/branch/main/fep/d556/fep-d556.md
SERVER_ACTOR_PREFIXES = (
'/',
'/accounts/peertube',
'/actor',
'/i/actor',
'/internal/fetch',
'/wp-json/activitypub/1.0/application',
)


def get_urls(obj, key='url'):
"""Returns ``link['href']`` or ``link``, for each ``link`` in ``obj[key]``."""
Expand Down Expand Up @@ -720,3 +731,30 @@ def link_tags(obj):
del tag['length']

obj['content'] = linked + orig[last_end:]


def is_server_actor(actor):
"""Returns True if this is the instance's server actor, False otherwise.
Server actors are non-user actors used for fetching remote objects, sending
``Flag`` activities, etc. Background:
* https://seb.jambor.dev/posts/understanding-activitypub-part-4-threads/#the-instance-actor
* https://codeberg.org/fediverse/fep/src/branch/main/fep/d556/fep-d556.md
Right now, this just uses a well-known set of paths as a heuristic.
TODO: actually do a WebFinger lookup of the root path, eg
``webfinger?resource=https://example.com/``, and use FEP-d556 to determine the
server actor.
Args:
actor (dict): AS2 actor
Returns:
bool:
"""
id = actor.get('id')
if not id:
return False

return urlparse(id).path in SERVER_ACTOR_PREFIXES
14 changes: 14 additions & 0 deletions granary/tests/test_as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,17 @@ def test_link_tags(self):
{'url': 'http://baz'},
],
}, obj)

def test_is_server_actor(self):
self.assertFalse(as2.is_server_actor({}))

for expected, id in (
(True, 'http://a/'),
(True, 'http://a/actor'),
(True, 'http://a/internal/fetch'),
(False, None),
(False, ''),
(False, '/me'),
(False, '/users/me'),
):
self.assertEqual(expected, as2.is_server_actor({'id': id}))

0 comments on commit c5f7bf5

Please sign in to comment.