Skip to content

Commit

Permalink
Twitter: when replying to yourself, don't add a self @-mention
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed May 20, 2016
1 parent 5027586 commit e28f87e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ Changelog
* Switch create() to use brevity for counting characters. https://github.com/kylewm/brevity
* Fix bug in create() that occasionally incorrectly escaped ., +, and - characters.
* Fix text rendering bug when there are multipl photos/videos.
* When replying to yourself, don't add a self @-mention.
* Instagram:
* Fix bugs in scraping.
* Upgrade to requests 2.10.0 and requests-toolbelt 0.60, which support App Engine.
Expand Down
23 changes: 23 additions & 0 deletions granary/test/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,29 @@ def test_create_reply_objectType_comment(self):
self.assert_equals({'url': 'http://posted/tweet', 'type': 'comment'},
self.twitter.create(obj).content)

def test_create_reply_to_self_omits_mention(self):
tw = twitter.Twitter('key', 'secret', username='me')
obj = {
'objectType': 'comment',
'content': 'my content',
'inReplyTo': [{'url': 'http://twitter.com/me/status/100'}],
}

# test preview
preview = tw.preview_create(obj)
self.assertIn('<span class="verb">@-reply</span> to <a href="http://twitter.com/me/status/100">this tweet</a>:', preview.description)
self.assertEquals('my content', preview.content)

# test create
self.expect_urlopen(twitter.API_POST_TWEET, {'url': 'http://posted/tweet'},
params={
'status': 'my content',
'in_reply_to_status_id': '100',
})
self.mox.ReplayAll()
self.assert_equals({'url': 'http://posted/tweet', 'type': 'comment'},
tw.create(obj).content)

def test_create_favorite(self):
self.expect_urlopen(twitter.API_POST_FAVORITE, TWEET, params={'id': 100})
self.mox.ReplayAll()
Expand Down
11 changes: 7 additions & 4 deletions granary/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Twitter(source.Source):
</blockquote>
"""

def __init__(self, access_token_key, access_token_secret):
def __init__(self, access_token_key, access_token_secret, username=None):
"""Constructor.
Twitter now requires authentication in v1.1 of their API. You can get an
Expand All @@ -132,9 +132,11 @@ def __init__(self, access_token_key, access_token_secret):
Args:
access_token_key: string, OAuth access token key
access_token_secret: string, OAuth access token secret
username: string, optional, the current user. Used in e.g. preview/create.
"""
self.access_token_key = access_token_key
self.access_token_secret = access_token_secret
self.username = username

def get_actor(self, screen_name=None):
"""Returns a user as a JSON ActivityStreams actor dict.
Expand Down Expand Up @@ -599,9 +601,10 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
parts = parsed.path.split('/')
if len(parts) < 2 or not parts[1]:
raise ValueError('Could not determine author of in-reply-to URL %s' % base_url)
mention = '@' + parts[1]
if mention.lower() not in content.lower():
content = mention + ' ' + content
if parts[1] != self.username:

This comment has been minimized.

Copy link
@kylewm

kylewm May 20, 2016

Contributor

Maybe a couple .lower()s here too?

This comment has been minimized.

Copy link
@snarfed

snarfed May 20, 2016

Author Owner

yes! good catch. thanks!

mention = '@' + parts[1]
if mention.lower() not in content.lower():
content = mention + ' ' + content

# the embed URL in the preview can't start with mobile. or www., so just
# hard-code it to twitter.com. index #1 is netloc.
Expand Down

0 comments on commit e28f87e

Please sign in to comment.