Skip to content

Commit

Permalink
facebook: switch users' primary URL from web site to facebook profile…
Browse files Browse the repository at this point in the history
… URL

for #158
  • Loading branch information
snarfed committed Dec 7, 2018
1 parent e5d0d8e commit 1e97810
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ On the open source side, there are many related projects. [php-mf2-shim](https:/
Changelog
---
### 1.15 - unreleased
* Twitter:
* Twitter, Facebook:
* Switch users' primary URL from web site to Twitter profile URL ([snarfed/bridgy#158](https://github.com/snarfed/granary/issues/158)).
* GitHub:
* Don't enclose bare URLs in`<`/`>` ([snarfed/bridgy#850](https://github.com/snarfed/bridgy/issues/850)).
Expand Down
20 changes: 8 additions & 12 deletions granary/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,11 @@ def user_to_actor(self, user):
if not handle:
return {}

# facebook implements this as a 302 redirect
# extract web site links. extract_links uniquifies and preserves order
urls = (util.extract_links(user.get('link')) or [self.user_url(handle)]) + sum(
(util.extract_links(user.get(field)) for field in
('website', 'about', 'description')), [])

actor = {
# FB only returns the type field if you fetch the object with ?metadata=1
# https://developers.facebook.com/docs/graph-api/using-graph-api#introspection
Expand All @@ -1378,7 +1382,9 @@ def user_to_actor(self, user):
'username': username,
'description': user.get('description') or user.get('about'),
'summary': user.get('about'),
}
'url': urls[0],
'urls': [{'value': u} for u in urls] if len(urls) > 1 else None,
}

# numeric_id is our own custom field that always has the source's numeric
# user id, if available.
Expand All @@ -1390,16 +1396,6 @@ def user_to_actor(self, user):
},
})

# extract web site links. extract_links uniquifies and preserves order
urls = (sum((util.extract_links(user.get(field)) for field in
('website', 'about', 'description')), []) or
util.extract_links(user.get('link')) or
[self.user_url(handle)])
if urls:
actor['url'] = urls[0]
if len(urls) > 1:
actor['urls'] = [{'value': u} for u in urls]

location = user.get('location')
if location:
actor['location'] = {'id': location.get('id'),
Expand Down
35 changes: 21 additions & 14 deletions granary/tests/test_facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ def tag_uri(name):
'id': tag_uri('snarfed.org'),
'numeric_id': '212038',
'updated': '2012-01-06T02:11:04+00:00',
'url': 'https://snarfed.org/',
'urls': [{'value': 'https://snarfed.org/'},
{'value': 'http://in.description.com'},
],
'url': 'http://www.facebook.com/snarfed.org',
'urls': [
{'value': 'http://www.facebook.com/snarfed.org'},
{'value': 'https://snarfed.org/'},
{'value': 'http://in.description.com'},
],
'username': 'snarfed.org',
'description': 'something about me http://in.description.com',
'summary': 'something about me http://in.description.com',
Expand Down Expand Up @@ -102,11 +104,13 @@ def tag_uri(name):
'username': 'CivicHallNYC',
'numeric_id': '946432998716566',
'displayName': 'Civic Hall',
'url': 'http://www.civichall.org',
'urls': [{'value': 'http://www.civichall.org'},
{'value': 'https://in.about.net'},
{'value': 'http://in.description.gov'},
],
'url': 'https://www.facebook.com/CivicHallNYC',
'urls': [
{'value': 'https://www.facebook.com/CivicHallNYC'},
{'value': 'http://www.civichall.org'},
{'value': 'https://in.about.net'},
{'value': 'http://in.description.gov'},
],
'image': {'url': 'https://graph.facebook.com/v2.10/946432998716566/picture?type=large'},
'summary': 'Introducing Civic Hall, a new home for civic technology and innovation, launching soon in New York City. https://in.about.net',
'description': 'Civic Hall, a project of Personal Democracy Media, is a vibrant, collaborative, year-round community center and beautiful http://in.description.gov event space...',
Expand Down Expand Up @@ -1944,12 +1948,15 @@ def test_user_to_actor_multiple_urls(self):
http://a
y.com
http://b http://c""",
'link': 'http://x', # website overrides link
'link': 'http://x',
})
self.assertEqual('http://a', actor['url'])
self.assertEqual(
[{'value': 'http://a'}, {'value': 'http://b'}, {'value': 'http://c'}],
actor['urls'])
self.assertEqual('http://x', actor['url'])
self.assertEqual([
{'value': 'http://x'},
{'value': 'http://a'},
{'value': 'http://b'},
{'value': 'http://c'},
], actor['urls'])

actor = self.fb.user_to_actor({
'id': '123',
Expand Down

0 comments on commit 1e97810

Please sign in to comment.