Skip to content

Commit

Permalink
merge is_privacy_known into is_public, return None for unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Mar 21, 2016
1 parent 622c2ab commit 7ff376e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 49 deletions.
21 changes: 8 additions & 13 deletions granary/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def actor_name(actor):

@staticmethod
def is_public(obj):
"""Returns True if the activity or object is public or unspecified.
"""Returns True if the object is public, False if private, None if unknown.
...according to the Audience Targeting extension
https://developers.google.com/+/api/latest/activities/list#collection
Expand All @@ -543,18 +543,13 @@ def is_public(obj):
bridgy/util.prune_activity()). If the default here ever changes, be sure to
update Bridgy's code.
"""
to = obj.get('to')
if not to:
to = obj.get('object', {}).get('to')
if not to:
return True # unset
return '@public' in set(t.get('alias') for t in to)

@staticmethod
def is_privacy_known(obj):
"""Returns True if the object's privacy (ie 'to') is known, False otherwise."""
return any(t.get('alias') and t['alias'] != 'unknown'
for t in obj.get('to') or obj.get('object', {}).get('to') or [])
to = obj.get('to') or obj.get('object', {}).get('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
else None if 'unknown' in object_types
else False if aliases
else True)

@staticmethod
def add_rsvps_to_event(event, rsvps):
Expand Down
57 changes: 21 additions & 36 deletions granary/test/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,51 +372,36 @@ def test_strip_html_tags(self):

def test_is_public(self):
for obj in ({'to': [{'objectType': 'unknown'}]},
{'to': [{'objectType': 'group', 'alias': '@private'}]},
{'to': [{'objectType': 'group', 'alias': 'xyz'}]},
{'to': [{'objectType': 'group', 'alias': 'xyz'},
{'objectType': 'group', 'alias': '@private'}]},
{'to': [{'objectType': 'unknown'},
{'objectType': 'unknown'}]},
{'to': [{'alias': 'xyz'},
{'objectType': 'unknown'}]},
):
self.assertFalse(Source.is_public(obj), obj)
self.assertFalse(Source.is_public({'object': obj}), obj)
self.assertIsNone(Source.is_public(obj), `obj`)
self.assertIsNone(Source.is_public({'object': obj}), `obj`)

for obj in ({},
{'privacy': 'xyz'},
{'to': []},
{'to': [{}]},
{'to': [{'objectType': 'group'}]},
{'to': [{'objectType': 'group', 'alias': '@public'}]},
{'to': [{'objectType': 'group', 'alias': '@private'},
{'objectType': 'group', 'alias': '@public'}]},
{'to': [{'objectType': 'group', 'alias': '@public'},
{'objectType': 'group', 'alias': '@private'}]},
{'to': [{'alias': '@public'},
{'alias': '@private'}]},
):
self.assertTrue(Source.is_public(obj), obj)
self.assertTrue(Source.is_public({'object': obj}), obj)
self.assertTrue(Source.is_public(obj), `obj`)
self.assertTrue(Source.is_public({'object': obj}), `obj`)

def test_is_privacy_known(self):
for obj in ({},
{'privacy': 'xyz'},
{'to': []},
{'to': [{'objectType': 'unknown'}]},
{'to': [{'objectType': 'unknown'},
{'objectType': 'unknown'}]},
):
self.assertFalse(Source.is_privacy_known(obj), obj)
self.assertFalse(Source.is_privacy_known({'object': obj}), obj)

for obj in ({'to': [{'objectType': 'group', 'alias': '@public'}]},
{'to': [{'objectType': 'group', 'alias': '@private'}]},
for obj in ({'to': [{'objectType': 'group', 'alias': '@private'}]},
{'to': [{'objectType': 'group', 'alias': 'xyz'}]},
{'to': [{'objectType': 'group', 'alias': '@private'},
{'objectType': 'group', 'alias': '@public'}]},
{'to': [{'objectType': 'unknown'},
{'objectType': 'group', 'alias': '@public'},
{'objectType': 'unknown'},
]},
{'to': [{'objectType': 'group', 'alias': '@public'},
{'objectType': 'group', 'alias': '@private'}]},
{'to': [{'objectType': 'group', 'alias': 'xyz'},
{'objectType': 'group', 'alias': '@private'}]},
{'to': [{'alias': 'xyz'}]},
{'to': [{'alias': 'xyz'},
{'alias': '@private'}]},
{'to': [{'objectType': 'group'},
{'alias': 'xyz'},
{'alias': '@private'}]},
):
self.assertTrue(Source.is_privacy_known(obj), obj)
self.assertTrue(Source.is_privacy_known({'object': obj}), obj)

self.assertFalse(Source.is_public(obj), `obj`)
self.assertFalse(Source.is_public({'object': obj}), `obj`)

0 comments on commit 7ff376e

Please sign in to comment.