Skip to content

Commit

Permalink
microformats2: handle mismatch between mf2 and AS 'photo' types
Browse files Browse the repository at this point in the history
mf2 'photo' type is a note or article *with* a photo, but AS 'photo' type *is* a photo. so, special case photo type to fall through to underlying mf2 type without photo.

fixes #702
  • Loading branch information
snarfed committed Oct 7, 2016
1 parent db6041b commit 675eb17
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ Changelog
* Render full original quoted tweet in retweets of quote tweets.
* microformats2 HTML:
* Optionally follow and fetch rel="author" links.
* Improve mapping between microformats2 and ActivityStreams 'photo' types. (mf2 'photo' type is a note or article *with* a photo, but AS 'photo' type *is* a photo. So, map mf2 photos to underlying type without photo.)

#### 1.5 - 2016-08-25
* REST API:
Expand Down
14 changes: 11 additions & 3 deletions granary/microformats2.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,21 @@ def json_to_object(mf2, actor=None):
'person': ('person', None),
'location': ('place', None),
'note': ('note', None),
'photo': ('note', None),
'article': ('article', None),
}

mf2_types = mf2.get('type') or []
mf2_type = ('location' if 'h-geo' in mf2_types or 'p-location' in mf2_types
else mf2util.post_type_discovery(mf2))
if 'h-geo' in mf2_types or 'p-location' in mf2_types:
mf2_type = 'location'
else:
# mf2 'photo' type is a note or article *with* a photo, but AS 'photo' type
# *is* a photo. so, special case photo type to fall through to underlying
# mf2 type without photo.
# https://github.com/snarfed/bridgy/issues/702
without_photo = copy.deepcopy(mf2)
without_photo.get('properties', {}).pop('photo', None)
mf2_type = mf2util.post_type_discovery(without_photo)

as_type, as_verb = mf2_type_to_as_type.get(mf2_type, (None, None))

def absolute_urls(prop):
Expand Down
2 changes: 1 addition & 1 deletion granary/test/testdata/article_with_comments.as.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "article abc",
"content": "foo bar",
"url": "http://example.com/article-abc",

"replies": {
"items": [{
"objectType": "comment",
Expand Down
6 changes: 6 additions & 0 deletions granary/test/testdata/article_with_photo.as.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"objectType": "article",
"displayName": "article abc",
"content": "foo bar",
"image": [{"url": "http://pic/ture.jpg"}]
}
12 changes: 12 additions & 0 deletions granary/test/testdata/article_with_photo.mf2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<article class="h-entry">
<span class="p-uid"></span>

<span class="p-name">article abc</span>
<div class="e-content">

foo bar
</div>

<img class="u-photo" src="http://pic/ture.jpg" alt="attachment" />

</article>
11 changes: 11 additions & 0 deletions granary/test/testdata/article_with_photo.mf2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": ["h-entry"],
"properties": {
"name": ["article abc"],
"content": [{
"value": "foo bar",
"html": "foo bar"
}],
"photo": ["http://pic/ture.jpg"]
}
}
3 changes: 1 addition & 2 deletions granary/test/testdata/note.as-from-mf2.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"objectType": "note",
"displayName": "blog post 123",
"summary": "too cool to summarize",
"content": "A blog post. <a href=\"http://my/link\">link</a> too",
"content": "A note. <a href=\"http://my/link\">link</a> too",
"id": "tag:example.com,2011:blog-post-123",
"published": "2012-02-22T20:26:41",
"updated": "2013-10-25T10:31:30+00:00",
Expand Down
5 changes: 2 additions & 3 deletions granary/test/testdata/note.as.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"objectType": "note",
"displayName": "blog post 123",
"summary": "too cool to summarize",
"content": "A blog post. link too",
"content": "A note. link too",
"id": "tag:example.com,2011:blog-post-123",
"published": "2012-02-22T20:26:41",
"updated": "2013-10-25T10:31:30+00:00",
Expand All @@ -20,7 +19,7 @@
},
"tags": [{
"url": "http://my/link",
"startIndex": 13,
"startIndex": 8,
"length": 4
}],
"upstreamDuplicates": ["http://orig/1", "http://orig/2"]
Expand Down
6 changes: 3 additions & 3 deletions granary/test/testdata/note.mf2.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<img class="u-photo" src="http://example.com/ryan/image" alt="" />
</span>

<a class="p-name u-url" href="http://example.com/blog-post-123">blog post 123</a>
<a class="u-url" href="http://example.com/blog-post-123">http://example.com/blog-post-123</a>
<a class="u-url" href="http://orig/1"></a>
<a class="u-url" href="http://orig/2"></a>
<div class="e-content">
<div class="e-content p-name">

A blog post. <a href="http://my/link">link</a> too
A note. <a href="http://my/link">link</a> too
</div>

<img class="u-photo" src="http://example.com/blog-post-123/image" alt="attachment" />
Expand Down
5 changes: 2 additions & 3 deletions granary/test/testdata/note.mf2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
"type": ["h-entry"],
"properties": {
"uid": ["tag:example.com,2011:blog-post-123"],
"name": ["blog post 123"],
"summary": ["too cool to summarize"],
"url": ["http://example.com/blog-post-123",
"http://orig/1",
"http://orig/2"],
"published": ["2012-02-22T20:26:41"],
"updated": ["2013-10-25T10:31:30+00:00"],
"content": [{
"value": "A blog post. link too",
"html": "A blog post. <a href=\"http://my/link\">link</a> too"
"value": "A note. link too",
"html": "A note. <a href=\"http://my/link\">link</a> too"
}],
"photo": ["http://example.com/blog-post-123/image"],

Expand Down

0 comments on commit 675eb17

Please sign in to comment.