Skip to content

Commit

Permalink
drop support for h-as-* classes, both incoming and outgoing
Browse files Browse the repository at this point in the history
they're deprecated in favor of https://indiewebcamp.com/post-type-discovery
  • Loading branch information
snarfed committed Mar 18, 2016
1 parent d9ffef6 commit 63ab9b3
Show file tree
Hide file tree
Showing 44 changed files with 91 additions and 111 deletions.
40 changes: 10 additions & 30 deletions granary/microformats2.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,6 @@ def object_to_json(obj, trim_nulls=True, entry_class='h-entry',
if not obj:
return {}

types_map = {'article': [entry_class, 'h-as-article'],
'comment': [entry_class, 'h-as-comment'],
'like': [entry_class, 'h-as-like'],
'note': [entry_class, 'h-as-note'],
'person': ['h-card'],
'place': ['h-card', 'h-as-location'],
'share': [entry_class, 'h-as-share'],
'rsvp-yes': [entry_class, 'h-as-rsvp'],
'rsvp-no': [entry_class, 'h-as-rsvp'],
'rsvp-maybe': [entry_class, 'h-as-rsvp'],
'invite': [entry_class],
}
obj_type = source.object_type(obj) or default_object_type
# if the activity type is a post, then it's really just a conduit
# for the object. for other verbs, the activity itself is the
Expand All @@ -131,21 +119,22 @@ def object_to_json(obj, trim_nulls=True, entry_class='h-entry',
else:
primary = obj

types = types_map.get(obj_type, [entry_class])

# TODO: extract snippet
name = primary.get('displayName', primary.get('title'))
summary = primary.get('summary')
author = obj.get('author', obj.get('actor', {}))

in_reply_tos = obj.get(
'inReplyTo', obj.get('context', {}).get('inReplyTo', []))
if 'h-as-rsvp' in types and 'object' in obj:
is_rsvp = obj_type in ('rsvp-yes', 'rsvp-no', 'rsvp-maybe')
if is_rsvp and obj.get('object'):
in_reply_tos.append(obj['object'])

# TODO: more tags. most will be p-category?
ret = {
'type': types,
'type': (['h-card'] if obj_type == 'person'
else ['h-card', 'h-geo'] if obj_type == 'place'
else [entry_class]),
'properties': {
'uid': [obj.get('id', '')],
'name': [name],
Expand Down Expand Up @@ -191,7 +180,7 @@ def object_to_json(obj, trim_nulls=True, entry_class='h-entry',
ret['properties']['category'].append(object_to_json(tag, entry_class=cls))

# rsvp
if 'h-as-rsvp' in types:
if is_rsvp:
ret['properties']['rsvp'] = [obj_type[len('rsvp-'):]]
elif obj_type == 'invite':
invitee = object_to_json(obj.get('object'), trim_nulls=False,
Expand Down Expand Up @@ -239,13 +228,6 @@ def json_to_object(mf2):
rsvp_verb = 'rsvp-%s' % rsvp if rsvp else None
author = json_to_object(prop.get('author'))

# maps mf2 class to a mf2 type. ordered by priority. these explicit
# h-* types can override implicit post type discovery.
h_class_overrides = [
('h-geo', 'location'),
('h-card', 'person'),
]

# maps mf2 type to ActivityStreams objectType and optional verb.
mf2_type_to_as_type = {
'rsvp': ('activity', rsvp_verb),
Expand All @@ -260,12 +242,10 @@ def json_to_object(mf2):
'article': ('article', None),
}

for h_class, mf2_type in h_class_overrides:
if h_class in mf2.get('type', []):
break # found
else:
mf2_type = mf2util.post_type_discovery(mf2)

mf2_types = mf2.get('type') or []
mf2_type = ('location' if 'h-geo' in mf2_types
else 'person' if 'h-card' in mf2_types
else mf2util.post_type_discovery(mf2))
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/test_facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ def tag_uri(name):
</a>
<span class="summary">my link caption</span>
</p>
<span class="p-location h-card h-as-location">
<span class="p-location h-card h-geo">
<a class="p-name u-url" href="https://www.facebook.com/113785468632283">Lake Merced</a>
</span>
Expand Down
2 changes: 1 addition & 1 deletion granary/test/test_instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def tag_uri(name):
<img class="thumbnail" src="http://attach/image/big" alt="" />
</a>
</p>
<span class="p-location h-card h-as-location">
<span class="p-location h-card h-geo">
<a class="p-name u-url" href="https://instagram.com/explore/locations/520640/">Le Truc</a>
</span>
Expand Down
17 changes: 8 additions & 9 deletions granary/test/test_microformats2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ def test_post_type_discovery(self):
def test_verb_require_of_suffix(self):
for prop in 'like', 'repost':
obj = microformats2.json_to_object(
{'type': ['h-entry', 'h-as-note'],
{'type': ['h-entry'],
'properties': {prop: ['http://foo/bar']}})
self.assertNotIn('verb', obj)

def test_ignore_h_as(self):
"""https://github.com/snarfed/bridgy/issues/635"""
obj = microformats2.json_to_object({'type': ['h-entry', 'h-as-article']})
obj = microformats2.json_to_object({'type': ['h-entry']})
self.assertEquals('note', obj['objectType'])

def test_html_content_and_summary(self):
Expand Down Expand Up @@ -74,8 +74,7 @@ def test_video_stream(self):
self.assertEquals([{'url': 'http://example.com/video.mp4'}], obj['stream'])

def test_nested_compound_url_object(self):
mf2 = {'type': ['h-as-share'],
'properties': {
mf2 = {'properties': {
'repost-of': [{
'type': ['h-outer'],
'properties': {
Expand Down Expand Up @@ -222,7 +221,7 @@ def test_render_content_omits_tags_without_urls(self):
def test_render_content_location(self):
self.assert_equals("""\
foo
<span class="p-location h-card h-as-location">
<span class="p-location h-card h-geo">
<a class="p-name u-url" href="http://my/place">My place</a>
</span>
Expand Down Expand Up @@ -336,16 +335,16 @@ def test_attachments_to_children(self):
]}

self.assert_equals([{
'type': ['h-cite', 'h-as-note'],
'type': ['h-cite'],
'properties': {'url': ['http://p'], 'name': ['p']},
}, {
'type': ['h-cite', 'h-as-article'],
'type': ['h-cite'],
'properties': {'url': ['http://a']},
}], microformats2.object_to_json(obj)['children'])

html = microformats2.object_to_html(obj)
self.assert_multiline_in("""\
<article class="h-cite h-as-note">
<article class="h-cite">
<span class="p-uid"></span>
<a class="p-name u-url" href="http://p">p</a>
Expand All @@ -355,7 +354,7 @@ def test_attachments_to_children(self):
</article>
<article class="h-cite h-as-article">
<article class="h-cite">
<span class="p-uid"></span>
<a class="u-url" href="http://a">http://a</a>
Expand Down
10 changes: 4 additions & 6 deletions granary/test/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def tag_uri(name):
<img class="thumbnail" src="http://p.twimg.com/picture3" alt="" />
</a>
</p>
<span class="p-location h-card h-as-location">
<span class="p-location h-card h-geo">
<a class="p-name u-url" href="https://maps.google.com/maps?q=32.4004416,-98.9852672">Carcassonne, Aude</a>
</span>
Expand Down Expand Up @@ -1469,12 +1469,10 @@ def test_create_tweet_include_link(self):
self.assertIn(u'too long… (<a href="http://obj.ca">obj.ca</a>)',result.content)

def test_create_recognize_note(self):
"""Use post-type-discovery to recognize a note with non-trivial html
content (and no explicit h-as-* type). We'll know it was
successful if it respects the rich content and includes newlines
in the output.
"""Use post-type-discovery to recognize a note with non-trivial html content.
We'll know it was successful if it respects the rich content and includes
newlines in the output.
"""

obj = microformats2.json_to_object({
"type": ["h-entry"],
"properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"objectType": "article",
"objectType": "note",
"displayName": "article abc",
"url": "http://example.com/article-abc",

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
@@ -1,5 +1,5 @@
{
"objectType": "article",
"objectType": "note",
"displayName": "article abc",
"url": "http://example.com/article-abc",

Expand Down
6 changes: 3 additions & 3 deletions granary/test/testdata/article_with_comments.mf2.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<article class="h-entry h-as-article">
<article class="h-entry">
<span class="p-uid"></span>

<a class="p-name u-url" href="http://example.com/article-abc">article abc</a>
<div class="">

</div>

<article class="p-comment h-cite h-as-comment">
<article class="p-comment h-cite">
<span class="p-uid"></span>

<span class="p-author h-card">
Expand All @@ -23,7 +23,7 @@

</article>

<article class="p-comment h-cite h-as-comment">
<article class="p-comment h-cite">
<span class="p-uid"></span>

<a class="p-name u-url" href="http://example.com/comment/2">second comment name</a>
Expand Down
6 changes: 3 additions & 3 deletions granary/test/testdata/article_with_comments.mf2.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"type": ["h-entry", "h-as-article"],
"type": ["h-entry"],
"properties": {
"name": ["article abc"],
"url": ["http://example.com/article-abc"],

"comment": [{
"type": ["h-cite", "h-as-comment"],
"type": ["h-cite"],
"properties": {
"author": [{
"type": ["h-card"],
Expand All @@ -15,7 +15,7 @@
"in-reply-to": ["http://example.com/article-abc"]
}
}, {
"type": ["h-cite", "h-as-comment"],
"type": ["h-cite"],
"properties": {
"url": ["http://example.com/comment/2"],
"name": ["second comment name"],
Expand Down
2 changes: 1 addition & 1 deletion granary/test/testdata/article_with_likes.as-from-mf2.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"objectType": "article",
"objectType": "note",
"url": "http://example.com/abc"
}
2 changes: 1 addition & 1 deletion granary/test/testdata/article_with_likes.as.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"objectType": "article",
"objectType": "note",
"url": "http://example.com/abc",

"tags": [{
Expand Down
6 changes: 3 additions & 3 deletions granary/test/testdata/article_with_likes.mf2.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<article class="h-entry h-as-article">
<article class="h-entry">
<span class="p-uid"></span>

<a class="u-url" href="http://example.com/abc">http://example.com/abc</a>
<div class="">

</div>

<article class="u-like h-cite h-as-like">
<article class="u-like h-cite">
<span class="p-uid"></span>

<span class="p-author h-card">
Expand All @@ -23,7 +23,7 @@

</article>

<article class="u-like h-cite h-as-like">
<article class="u-like h-cite">
<span class="p-uid"></span>

<a class="u-url" href="http://example.com/anonymous/like">http://example.com/anonymous/like</a>
Expand Down
6 changes: 3 additions & 3 deletions granary/test/testdata/article_with_likes.mf2.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"type": ["h-entry", "h-as-article"],
"type": ["h-entry"],
"properties": {
"url": ["http://example.com/abc"],
"like": [{
"type": ["h-cite", "h-as-like"],
"type": ["h-cite"],
"properties": {
"author": [{
"type": ["h-card"],
Expand All @@ -18,7 +18,7 @@
"like-of": ["http://example.com/abc"]
}
}, {
"type": ["h-cite", "h-as-like"],
"type": ["h-cite"],
"properties": {
"url": ["http://example.com/anonymous/like"],
"content": [{"html": "<a href=\"http://example.com/abc\">likes this.</a>"}],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"objectType": "article",
"objectType": "note",
"url": "http://example.com/abc"
}
2 changes: 1 addition & 1 deletion granary/test/testdata/article_with_reposts.as.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"objectType": "article",
"objectType": "note",
"url": "http://example.com/abc",

"tags": [{
Expand Down
6 changes: 3 additions & 3 deletions granary/test/testdata/article_with_reposts.mf2.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<article class="h-entry h-as-article">
<article class="h-entry">
<span class="p-uid"></span>

<a class="u-url" href="http://example.com/abc">http://example.com/abc</a>
<div class="">

</div>

<article class="u-repost h-cite h-as-share">
<article class="u-repost h-cite">
<span class="p-uid"></span>

<span class="p-author h-card">
Expand All @@ -24,7 +24,7 @@

</article>

<article class="u-repost h-cite h-as-share">
<article class="u-repost h-cite">
<span class="p-uid"></span>

<a class="u-url" href="http://example.com/anonymous/repost">http://example.com/anonymous/repost</a>
Expand Down
6 changes: 3 additions & 3 deletions granary/test/testdata/article_with_reposts.mf2.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"type": ["h-entry", "h-as-article"],
"type": ["h-entry"],
"properties": {
"url": ["http://example.com/abc"],
"repost": [{
"type": ["h-cite", "h-as-share"],
"type": ["h-cite"],
"properties": {
"author": [{
"type": ["h-card"],
Expand All @@ -18,7 +18,7 @@
"repost-of": ["http://example.com/abc"]
}
}, {
"type": ["h-cite", "h-as-share"],
"type": ["h-cite"],
"properties": {
"url": ["http://example.com/anonymous/repost"],
"content": [{"html": "<a href=\"http://example.com/abc\">shared this.</a>"}],
Expand Down
4 changes: 2 additions & 2 deletions granary/test/testdata/comment.mf2.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<article class="h-entry h-as-comment">
<article class="h-entry">
<span class="p-uid">tag:example.com,2001:547822715231468_6796480</span>

<time class="dt-published" datetime="2012-12-05T00:58:26+00:00">2012-12-05T00:58:26+00:00</time>
Expand All @@ -9,7 +9,7 @@
foo bar baz
</div>

<span class="p-location h-card h-as-location">
<span class="p-location h-card h-geo">
<a class="p-name u-url" href="https://maps.google.com/maps?q=32.4004416,-98.9852672">Carcassonne, Aude</a>

</span>
Expand Down
Loading

0 comments on commit 63ab9b3

Please sign in to comment.