Skip to content

Commit

Permalink
as1.is_public: return False if the object contains to that's empty or…
Browse files Browse the repository at this point in the history
… has only unknown aliases

for snarfed/bridgy-fed#1022
  • Loading branch information
snarfed committed May 6, 2024
1 parent 788815b commit d802de2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 40 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ Changelog

* `as1`:
* `activity_changed`: add `displayName`, `summary` fields.
* `is_public`: return `False` if the object/activity contains `to` that's empty or has only unknown aliases.
* `as2`:
* Add support for the `Application`, `Block`, `Flag`, and `Link` types.
* Generalize actor logic in `to/from_as1` across all actor types, not just `Person`.
Expand Down
19 changes: 13 additions & 6 deletions granary/as1.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,21 @@ def is_public(obj, unlisted=True):
obj (dict): AS2 activity or object
unlisted (bool): whether `@unlisted` counts as public or not
"""
to = obj.get('to') or get_object(obj).get('to') or []
inner_obj = get_object(obj)
to = get_objects(obj, 'to') or get_objects(inner_obj, 'to') or []
aliases = util.trim_nulls([t.get('alias') for t in to])
object_types = util.trim_nulls([t.get('objectType') for t in to])
return (True if ('@public' in aliases
or ('@unlisted' in aliases and unlisted))
else None if 'unknown' in object_types
else False if aliases
else True)
if '@public' in aliases or ('@unlisted' in aliases and unlisted):
return True
elif 'unknown' in object_types:
return None
elif aliases:
return False
elif 'to' in obj or 'to' in inner_obj:
# it does at least have some audience that doesn't include public
return False

return True


def add_rsvps_to_event(event, rsvps):
Expand Down
76 changes: 42 additions & 34 deletions granary/tests/test_as1.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,46 +155,54 @@
class As1Test(testutil.TestCase):

def test_is_public(self):
for obj in ({'to': [{'objectType': 'unknown'}]},
{'to': [{'objectType': 'unknown'},
{'objectType': 'unknown'}]},
{'to': [{'alias': 'xyz'},
{'objectType': 'unknown'}]},
):
self.assertIsNone(as1.is_public(obj), repr(obj))
self.assertIsNone(as1.is_public({'object': obj}), repr(obj))

for obj in ({},
{'privacy': 'xyz'},
{'to': []},
{'to': [{}]},
{'to': [{'objectType': 'group'}]},
{'to': [{'objectType': 'group', 'alias': '@unlisted'}]},
{'to': [{'objectType': 'group', 'alias': '@public'}]},
{'to': [{'objectType': 'group', 'alias': '@private'},
{'objectType': 'group', 'alias': '@public'}]},
{'to': [{'alias': '@public'},
{'alias': '@private'}]},
):
self.assertTrue(as1.is_public(obj), repr(obj))
self.assertTrue(as1.is_public({'object': obj}), repr(obj))
for obj in (
{'to': [{'objectType': 'unknown'}]},
{'to': [{'objectType': 'unknown'},
{'objectType': 'unknown'}]},
{'to': [{'alias': 'xyz'},
{'objectType': 'unknown'}]},
):
with self.subTest(obj=obj):
self.assertIsNone(as1.is_public(obj))
self.assertIsNone(as1.is_public({'object': obj}))

for obj in (
{},
{'privacy': 'xyz'},
{'to': [{'objectType': 'group', 'alias': '@unlisted'}]},
{'to': [{'objectType': 'group', 'alias': '@public'}]},
{'to': [{'objectType': 'group', 'alias': '@private'},
{'objectType': 'group', 'alias': '@public'}]},
{'to': [{'alias': '@public'},
{'alias': '@private'}]},
):
with self.subTest(obj=obj):
self.assertTrue(as1.is_public(obj))
self.assertTrue(as1.is_public({'object': obj}))


self.assertFalse(as1.is_public({
'to': [{'objectType': 'group', 'alias': '@unlisted'}],
}, unlisted=False))


for obj in ({'to': [{'objectType': 'group', 'alias': '@private'}]},
{'to': [{'objectType': 'group', 'alias': 'xyz'}]},
{'to': [{'alias': 'xyz'}]},
{'to': [{'alias': 'xyz'},
{'alias': '@private'}]},
{'to': [{'objectType': 'group'},
{'alias': 'xyz'},
{'alias': '@private'}]},
):
self.assertFalse(as1.is_public(obj), repr(obj))
self.assertFalse(as1.is_public({'object': obj}), repr(obj))
for obj in (
{'to': []},
{'to': [{}]},
{'to': ['someone']},
{'to': [{'objectType': 'group'}]},
{'to': [{'objectType': 'group', 'alias': '@private'}]},
{'to': [{'objectType': 'group', 'alias': 'xyz'}]},
{'to': [{'alias': 'xyz'}]},
{'to': [{'alias': 'xyz'},
{'alias': '@private'}]},
{'to': [{'objectType': 'group'},
{'alias': 'xyz'},
{'alias': '@private'}]},
):
with self.subTest(obj=obj):
self.assertFalse(as1.is_public(obj))
self.assertFalse(as1.is_public({'object': obj}))

def test_activity_changed(self):
fb_post = copy.deepcopy(ACTIVITY)
Expand Down

0 comments on commit d802de2

Please sign in to comment.