Skip to content

Commit 92f625d

Browse files
committed
Don't create notifications when add comment on own articles.
1 parent a514e8d commit 92f625d

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/handlers/me/articles/comments/create/me_articles_comments_create.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ def exec_main_proc(self):
6060
article_info_table = self.dynamodb.Table(os.environ['ARTICLE_INFO_TABLE_NAME'])
6161
article_info = article_info_table.get_item(Key={'article_id': self.params['article_id']})['Item']
6262

63-
self.__create_comment_notification(article_info, comment_id, user_id)
64-
self.__update_unread_notification_manager(article_info)
63+
if self.__is_notifiable_comment(article_info, user_id):
64+
self.__create_comment_notification(article_info, comment_id, user_id)
65+
self.__update_unread_notification_manager(article_info)
6566

6667
except Exception as err:
6768
logging.fatal(err)
@@ -72,6 +73,9 @@ def exec_main_proc(self):
7273
'body': json.dumps({'comment_id': comment_id})
7374
}
7475

76+
def __is_notifiable_comment(self, article_info, user_id):
77+
return False if article_info['user_id'] == user_id else True
78+
7579
def __create_comment_notification(self, article_info, comment_id, user_id):
7680
notification_table = self.dynamodb.Table(os.environ['NOTIFICATION_TABLE_NAME'])
7781
notification_id = '-'.join(

tests/handlers/me/articles/comments/create/test_me_articles_comments_create.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,45 @@ def test_main_ok_already_commented_by_myself(self):
168168
self.assertEqual(len(comment_after) - len(comment_before), 1)
169169
self.assertIsNotNone(comment)
170170

171+
@patch('me_articles_comments_create.MeArticlesCommentsCreate._MeArticlesCommentsCreate__generate_comment_id',
172+
MagicMock(return_value='PIYOPIYO'))
173+
def test_main_ok_with_adding_comment_on_own_article(self):
174+
params = {
175+
'pathParameters': {
176+
'article_id': 'publicId0001'
177+
},
178+
'body': {
179+
'text': 'A' * 400,
180+
},
181+
'requestContext': {
182+
'authorizer': {
183+
'claims': {
184+
'cognito:username': 'article_user01'
185+
}
186+
}
187+
}
188+
}
189+
190+
params['body'] = json.dumps(params['body'])
191+
192+
comment_before = self.comment_table.scan()['Items']
193+
notification_before = self.notification_table.scan()['Items']
194+
unread_notification_manager_before = self.unread_notification_manager_table.scan()['Items']
195+
196+
response = MeArticlesCommentsCreate(params, {}, self.dynamodb).main()
197+
198+
comment_after = self.comment_table.scan()['Items']
199+
notification_after = self.notification_table.scan()['Items']
200+
unread_notification_manager_after = self.unread_notification_manager_table.scan()['Items']
201+
202+
comment = self.comment_table.get_item(Key={'comment_id': 'PIYOPIYO'}).get('Item')
203+
204+
self.assertEqual(response['statusCode'], 200)
205+
self.assertIsNotNone(comment)
206+
self.assertEqual(len(comment_after) - len(comment_before), 1)
207+
self.assertEqual(len(notification_after) - len(notification_before), 0)
208+
self.assertEqual(len(unread_notification_manager_after) - len(unread_notification_manager_before), 0)
209+
171210
def test_call_validate_comment_existence(self):
172211
params = {
173212
'pathParameters': {

0 commit comments

Comments
 (0)