Merge lp:~robru/friends/tweet-reply into lp:~super-friends/friends/raring

Proposed by Robert Bruce Park
Status: Merged
Approved by: Ken VanDine
Approved revision: 176
Merged at revision: 178
Proposed branch: lp:~robru/friends/tweet-reply
Merge into: lp:~super-friends/friends/raring
Diff against target: 94 lines (+54/-1)
3 files modified
friends/protocols/twitter.py (+6/-1)
friends/tests/test_twitter.py (+38/-0)
friends/utils/base.py (+10/-0)
To merge this branch: bzr merge lp:~robru/friends/tweet-reply
Reviewer Review Type Date Requested Status
Ken VanDine Approve
Review via email: mp+153983@code.launchpad.net

Commit message

Make Twitter.send_thread prepend the necessary @mention if it is missing from the tweet. (LP: #1156829)

To post a comment you must log in.
Revision history for this message
Ken VanDine (ken-vandine) wrote :

Thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'friends/protocols/twitter.py'
2--- friends/protocols/twitter.py 2013-03-13 18:05:13 +0000
3+++ friends/protocols/twitter.py 2013-03-19 03:16:24 +0000
4@@ -31,7 +31,6 @@
5 from friends.utils.avatar import Avatar
6 from friends.utils.base import Base, feature
7 from friends.utils.cache import JsonCache
8-from friends.utils.model import Model
9 from friends.utils.http import BaseRateLimiter, Downloader
10 from friends.utils.time import parsetime, iso8601utc
11 from friends.errors import FriendsError
12@@ -254,6 +253,12 @@
13 order for Twitter to actually accept this as a reply. Otherwise it
14 will just be an ordinary tweet.
15 """
16+ try:
17+ sender = '@{}'.format(self._fetch_cell(message_id, 'sender_nick'))
18+ if message.find(sender) < 0:
19+ message = sender + ' ' + message
20+ except FriendsError:
21+ pass
22 url = self._api_base.format(endpoint='statuses/update')
23 tweet = self._get_url(url, dict(in_reply_to_status_id=message_id,
24 status=message))
25
26=== modified file 'friends/tests/test_twitter.py'
27--- friends/tests/test_twitter.py 2013-03-14 19:14:03 +0000
28+++ friends/tests/test_twitter.py 2013-03-19 03:16:24 +0000
29@@ -389,6 +389,44 @@
30 'tweet @pumpichank!',
31 in_reply_to_status_id='1234'))
32
33+ @mock.patch('friends.utils.base.Model', TestModel)
34+ @mock.patch('friends.utils.http.Soup.Message',
35+ FakeSoupMessage('friends.tests.data', 'twitter-home.dat'))
36+ @mock.patch('friends.protocols.twitter.Twitter._login',
37+ return_value=True)
38+ @mock.patch('friends.utils.base._seen_ids', {})
39+ def test_send_thread_prepend_nick(self, *mocks):
40+ self.account.access_token = 'access'
41+ self.account.secret_token = 'secret'
42+ self.account.auth.parameters = dict(
43+ ConsumerKey='key',
44+ ConsumerSecret='secret')
45+ self.assertEqual(0, TestModel.get_n_rows())
46+ self.assertEqual(self.protocol.home(), 3)
47+ self.assertEqual(3, TestModel.get_n_rows())
48+
49+ # If you forgot to @mention in your reply, we add it for you.
50+ get = self.protocol._get_url = mock.Mock()
51+ self.protocol._publish_tweet = mock.Mock()
52+ self.protocol.send_thread(
53+ '240556426106372096',
54+ 'Exciting and original response!')
55+ get.assert_called_once_with(
56+ 'https://api.twitter.com/1.1/statuses/update.json',
57+ dict(status='@raffi Exciting and original response!',
58+ in_reply_to_status_id='240556426106372096'))
59+
60+ # If you remembered the @mention, we won't duplicate it.
61+ get.reset_mock()
62+ self.protocol.send_thread(
63+ '240556426106372096',
64+ 'You are the greatest, @raffi!')
65+ get.assert_called_once_with(
66+ 'https://api.twitter.com/1.1/statuses/update.json',
67+ dict(status='You are the greatest, @raffi!',
68+ in_reply_to_status_id='240556426106372096'))
69+
70+
71 def test_delete(self):
72 get_url = self.protocol._get_url = mock.Mock(return_value='tweet')
73 publish = self.protocol._unpublish = mock.Mock()
74
75=== modified file 'friends/utils/base.py'
76--- friends/utils/base.py 2013-03-14 13:29:53 +0000
77+++ friends/utils/base.py 2013-03-19 03:16:24 +0000
78@@ -478,6 +478,16 @@
79 message = None
80 raise FriendsError(message or str(error))
81
82+ def _fetch_cell(self, message_id, column_name):
83+ """Find a column value associated with a specific message_id."""
84+ row_id = _seen_ids.get(message_id)
85+ col_idx = COLUMN_INDICES.get(column_name)
86+ if None not in (row_id, col_idx):
87+ row = Model.get_row(row_id)
88+ return row[col_idx]
89+ else:
90+ raise FriendsError('Value could not be found.')
91+
92 def _new_book_client(self, source):
93 client = EBook.BookClient.new(source)
94 client.open_sync(False, None)

Subscribers

People subscribed via source and target branches