Skip to content

Commit

Permalink
twitter publish: explicit preview support for quote tweets
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jan 26, 2017
1 parent cc218de commit 7a18c9d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
14 changes: 12 additions & 2 deletions granary/test/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,10 @@ def test_create_favorite(self):
self.twitter.create(LIKES_FROM_HTML[0]).content)

preview = self.twitter.preview_create(LIKES_FROM_HTML[0])
self.assertIn('<span class="verb">favorite</span> <a href="https://twitter.com/snarfed_org/status/100">this tweet</a>:', preview.description)
self.assertIn("""\
<span class="verb">favorite</span>
<a href="https://twitter.com/snarfed_org/status/100">this tweet</a>:""",
preview.description)

def test_create_retweet(self):
self.expect_urlopen(twitter.API_POST_RETWEET % 333, TWEET, params={'id': 333})
Expand All @@ -1952,7 +1955,10 @@ def test_create_retweet(self):
self.assert_equals(tweet, self.twitter.create(SHARES[0]).content)

preview = self.twitter.preview_create(SHARES[0])
self.assertIn('<span class="verb">retweet</span> <a href="https://twitter.com/foo/status/333">this tweet</a>:', preview.description)
self.assertIn("""\
<span class="verb">retweet</span>
<a href="https://twitter.com/foo/status/333">this tweet</a>:""",
preview.description)

def test_create_quote_tweet(self):
self.expect_urlopen(
Expand All @@ -1968,6 +1974,10 @@ def test_create_quote_tweet(self):

preview = self.twitter.preview_create(QUOTE_ACTIVITY['object'])
self.assertEquals('I agree with this <a href="https://twitter.com/snarfed_org/status/100">twitter.com/snarfed_org/st...</a>', preview.content)
self.assertIn("""\
<span class="verb">quote</span>
<a href="https://twitter.com/snarfed_org/status/100">this tweet</a>:""",
preview.description)

def test_create_unsupported_type(self):
for fn in self.twitter.create, self.twitter.preview_create:
Expand Down
35 changes: 24 additions & 11 deletions granary/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,18 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
content = self._content_for_create(obj, ignore_formatting=ignore_formatting,
prefer_name=not prefer_content,
strip_first_video_tag=bool(video_url))

preview_description = ''
for att in obj.get('attachments', []):
url = self.URL_CANONICALIZER(att.get('url', ''))
if url and TWEET_URL_RE.match(url):
content += ' ' + url
preview_description += """\
<span class="verb">quote</span>
<a href="%s">this tweet</a>:
%s
and """ % (url, self.embed_post(att))
break

if not content:
if type == 'activity':
Expand Down Expand Up @@ -687,9 +695,11 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
'<a href="http://indiewebcamp.com/rel-syndication">rel-syndication</a> link to Twitter.')

if preview:
return source.creation_result(
description='<span class="verb">favorite</span> <a href="%s">'
'this tweet</a>:\n%s' % (base_url, self.embed_post(base_obj)))
preview_description += """\
<span class="verb">favorite</span>
<a href="%s">this tweet</a>:
%s""" % (base_url, self.embed_post(base_obj))
return source.creation_result(description=preview_description)
else:
data = urllib.urlencode({'id': base_id})
self.urlopen(API_POST_FAVORITE, data=data)
Expand All @@ -705,9 +715,11 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
'<a href="http://indiewebcamp.com/rel-syndication">rel-syndication</a> link to Twitter.')

if preview:
return source.creation_result(
description='<span class="verb">retweet</span> <a href="%s">'
'this tweet</a>:\n%s' % (base_url, self.embed_post(base_obj)))
preview_description += """\
<span class="verb">retweet</span>
<a href="%s">this tweet</a>:
%s""" % (base_url, self.embed_post(base_obj))
return source.creation_result(description=preview_description)
else:
data = urllib.urlencode({'id': base_id})
resp = self.urlopen(API_POST_RETWEET % base_id, data=data)
Expand All @@ -718,15 +730,15 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
data = {'status': content}

if is_reply:
description = \
'<span class="verb">@-reply</span> to <a href="%s">this tweet</a>:\n%s' % (
base_url, self.embed_post(base_obj))
preview_description += """\
<span class="verb">@-reply</span> to <a href="%s">this tweet</a>:
%s""" % (base_url, self.embed_post(base_obj))
data.update({
'in_reply_to_status_id': base_id,
'auto_populate_reply_metadata': 'true',
})
else:
description = '<span class="verb">tweet</span>:'
preview_description += '<span class="verb">tweet</span>:'

if video_url:
preview_content += ('<br /><br /><video controls src="%s"><a href="%s">'
Expand Down Expand Up @@ -759,7 +771,8 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
data['long'] = lng

if preview:
return source.creation_result(content=preview_content, description=description)
return source.creation_result(content=preview_content,
description=preview_description)
else:
resp = self.urlopen(API_POST_TWEET, data=urllib.urlencode(data))
resp['type'] = 'comment' if is_reply else 'post'
Expand Down

0 comments on commit 7a18c9d

Please sign in to comment.