Skip to content

Commit c4e42dd

Browse files
committed
Add process to limit the number of characters for tweet.
1 parent c528d4e commit c4e42dd

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/handlers/me/articles/like/create/me_articles_like_create.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,24 @@ def __get_article_likes_count(self):
147147

148148
def __post_tweet(self, user_id, title, tags):
149149
# tweet 文の作成
150-
# hash tag 文
150+
# title 文(50文字以上は省略)
151+
if len(title) > 50:
152+
show_title = title[:50] + '...'
153+
else:
154+
show_title = title
155+
# hash tag 文(最大55文字)
151156
hash_tags_str = ''
152157
if tags:
153-
hash_tags_str += '\n#' + ' #'.join(tags)
158+
for tag in tags:
159+
if hash_tags_str == '':
160+
hash_tags_str += '\n#' + tag
161+
elif len(hash_tags_str + tag) < 55:
162+
hash_tags_str += ' #' + tag
163+
else:
164+
break
154165
# tweet 文
155166
payload = {
156-
"text": f"{title}\n"
167+
"text": f"{show_title}\n"
157168
f"https://{os.environ['DOMAIN']}/{user_id}/articles/{self.event['pathParameters']['article_id']}" +
158169
hash_tags_str
159170
}

tests/handlers/me/articles/like/create/test_me_articles_like_create.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ def setUp(self):
7373
'status': 'public',
7474
'user_id': 'article_user_id_02',
7575
'sort_key': 1520150272000002
76+
},
77+
{
78+
'article_id': 'testid000003',
79+
'title': '123456789012345678901234567890123456789012345678901234567890',
80+
'status': 'public',
81+
'user_id': 'article_user_id_03',
82+
'tags': ['a1234567890', 'b1234567890', 'c1234567890', 'd1234567890', 'e1234567890'],
83+
'sort_key': 1520150272000003
7684
}
7785
]
7886
TestsUtil.create_table(
@@ -292,14 +300,15 @@ def test_main_with_updating_notification(self):
292300

293301
@patch('time.time', MagicMock(return_value=1520150272.000015))
294302
def test_main_ok_with_self_liked_user(self):
303+
settings.LIKED_TWEET_COUNT = 1
295304
params = {
296305
'pathParameters': {
297-
'article_id': self.article_info_table_items[2]['article_id']
306+
'article_id': self.article_info_table_items[3]['article_id']
298307
},
299308
'requestContext': {
300309
'authorizer': {
301310
'claims': {
302-
'cognito:username': self.article_info_table_items[2]['user_id'],
311+
'cognito:username': self.article_info_table_items[3]['user_id'],
303312
'custom:private_eth_address': '0x1234567890123456789012345678901234567890',
304313
'phone_number_verified': 'true',
305314
'email_verified': 'true'
@@ -315,7 +324,19 @@ def test_main_ok_with_self_liked_user(self):
315324
notification_before = notification_table.scan()['Items']
316325
unread_notification_manager_before = unread_notification_manager_table.scan()['Items']
317326

318-
response = MeArticlesLikeCreate(event=params, context={}, dynamodb=self.dynamodb).main()
327+
mock_lib = MagicMock()
328+
with patch('me_articles_like_create.TwitterUtil', mock_lib):
329+
response = MeArticlesLikeCreate(event=params, context={}, dynamodb=self.dynamodb).main()
330+
args, _ = mock_lib.return_value.post_tweet.call_args
331+
self.assertTrue(mock_lib.return_value.post_tweet.called)
332+
self.assertEqual(
333+
args[0],
334+
{
335+
'text': '12345678901234567890123456789012345678901234567890...\n'
336+
'https://dummy/article_user_id_03/articles/testid000003\n'
337+
'#a1234567890 #b1234567890 #c1234567890 #d1234567890'
338+
}
339+
)
319340

320341
notification_after = notification_table.scan()['Items']
321342
unread_notification_manager_after = unread_notification_manager_table.scan()['Items']

0 commit comments

Comments
 (0)