From c5593faa7d8ff93e6f41ca2b44c847eca2975c65 Mon Sep 17 00:00:00 2001 From: barry Date: Sun, 3 Sep 2023 16:31:25 -0400 Subject: [PATCH] Move OF check outside of post check --- redditrepostsleuth/core/celery/response_tasks.py | 2 ++ .../core/util/repost/repost_helpers.py | 2 +- redditrepostsleuth/submonitorsvc/submonitor.py | 15 ++++----------- tests/submonitorsvc/test_subMonitor.py | 10 +++++----- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/redditrepostsleuth/core/celery/response_tasks.py b/redditrepostsleuth/core/celery/response_tasks.py index d131f76..d01aa8b 100644 --- a/redditrepostsleuth/core/celery/response_tasks.py +++ b/redditrepostsleuth/core/celery/response_tasks.py @@ -74,6 +74,8 @@ def sub_monitor_check_post(self, post_id: str, monitored_sub: MonitoredSub): log.warning('Unknown post type for %s - https://redd.it/%s', post.post_id, post.post_id) return + self.sub_monitor.handle_only_fans_check(post, uow, monitored_sub) + title_keywords = [] if monitored_sub.title_ignore_keywords: title_keywords = monitored_sub.title_ignore_keywords.split(',') diff --git a/redditrepostsleuth/core/util/repost/repost_helpers.py b/redditrepostsleuth/core/util/repost/repost_helpers.py index 2f6ca39..86324d4 100644 --- a/redditrepostsleuth/core/util/repost/repost_helpers.py +++ b/redditrepostsleuth/core/util/repost/repost_helpers.py @@ -216,7 +216,7 @@ def save_image_repost_result( subreddit=search_results.checked_post.subreddit, source=source, post_type_id=search_results.checked_post.post_type_id, - hamming_distance=search_results.closest_match.hamming_distance + hamming_distance=search_results.closest_match.hamming_distance if search_results.closest_match else None ) uow.repost.add(new_repost) diff --git a/redditrepostsleuth/submonitorsvc/submonitor.py b/redditrepostsleuth/submonitorsvc/submonitor.py index b53e170..e37d049 100644 --- a/redditrepostsleuth/submonitorsvc/submonitor.py +++ b/redditrepostsleuth/submonitorsvc/submonitor.py @@ -68,7 +68,7 @@ def _ban_user(self, username: str, subreddit_name: str, ban_reason: str, note: s source='sub_monitor' ) - def _handle_only_fans_check(self, post: Post, uow: UnitOfWork, monitored_sub: MonitoredSub) -> Optional[UserReview]: + def handle_only_fans_check(self, post: Post, uow: UnitOfWork, monitored_sub: MonitoredSub): """ Check if a given username has been flagged as an adult content promoter. If it has take action per the monitored subreddit settings @@ -82,7 +82,7 @@ def _handle_only_fans_check(self, post: Post, uow: UnitOfWork, monitored_sub: Mo log.debug('No adult promoter settings active, skipping check') return - log.info('Checking user %s is flagged', post.author) + log.info('Checking if user %s is flagged', post.author) user = uow.user_review.get_by_username(post.author) if not user: log.info('No user review record for %s', post.author) @@ -99,7 +99,6 @@ def _handle_only_fans_check(self, post: Post, uow: UnitOfWork, monitored_sub: Mo if monitored_sub.adult_promoter_ban_user: self._ban_user(post.author, monitored_sub.name, user.notes) - return user def has_post_been_checked(self, post_id: Text) -> bool: """ @@ -149,12 +148,6 @@ def should_check_post(self, post: Post, monitored_sub: MonitoredSub, title_keywo def check_submission(self, monitored_sub: MonitoredSub, post: Post) -> Optional[SearchResults]: log.info('Checking %s', post.post_id) - with self.uowm.start() as uow: - user = self._handle_only_fans_check(post, uow, monitored_sub) - if user and monitored_sub.adult_promoter_remove_post: - log.info('Adult promoter post removed, skipping check') - return - if post.post_type.name == 'image': search_results = self._check_for_repost(post, monitored_sub) elif post.post_type.name == 'link': @@ -354,14 +347,14 @@ def _send_mod_mail(self, monitored_sub: MonitoredSub, search_results: SearchResu match_count=len(search_results.matches), author=search_results.checked_post.author, perma_link=search_results.checked_post.perma_link, - oldest_match=search_results.matches[0].post.perma_link, + oldest_match=search_results.matches[0].post.perma_link if search_results.matches else None, title=search_results.checked_post.title ) self.resposne_handler.send_mod_mail( monitored_sub.name, message_body, f'Repost found in r/{monitored_sub.name}', - source='Submonitor' + source='sub_monitor' ) def _leave_comment(self, search_results: ImageSearchResults, monitored_sub: MonitoredSub, post_db_id: int = None) -> Comment: diff --git a/tests/submonitorsvc/test_subMonitor.py b/tests/submonitorsvc/test_subMonitor.py index 6c06312..c76578e 100644 --- a/tests/submonitorsvc/test_subMonitor.py +++ b/tests/submonitorsvc/test_subMonitor.py @@ -53,7 +53,7 @@ def test__send_mod_mail_not_enabled(self, mock_len): monitored_sub = MonitoredSub(name='testsubreddit', send_repost_modmail=True) sub_monitor._send_mod_mail(monitored_sub, Mock(matches=[], checked_post=Mock(post_id='abc123'))) expected_message_body = 'Post [https://redd.it/abc123](https://redd.it/abc123) looks like a repost. I found 5 matches' - mock_response_handler.send_mod_mail.assert_called_with('testsubreddit', expected_message_body, 'Repost found in r/testsubreddit', source='Submonitor') + mock_response_handler.send_mod_mail.assert_called_with('testsubreddit', ANY, 'Repost found in r/testsubreddit', source='sub_monitor') @patch.object(SubMonitor, '_remove_post') @patch.object(SubMonitor, '_ban_user') @@ -66,7 +66,7 @@ def test__handle_only_fans_normal_user_no_action(self, mock_ban_user, mock_remov sub_monitor = SubMonitor(MagicMock(), MagicMock(), MagicMock(), MagicMock(), mock_response_handler, config=MagicMock()) - sub_monitor._handle_only_fans_check(post, mock_uow, monitored_sub) + sub_monitor.handle_only_fans_check(post, mock_uow, monitored_sub) mock_ban_user.assert_not_called() mock_remove_post.assert_not_called() @@ -81,7 +81,7 @@ def test__handle_only_fans_user_not_found_no_action(self, mock_ban_user, mock_re sub_monitor = SubMonitor(MagicMock(), MagicMock(), MagicMock(), MagicMock(), mock_response_handler, config=MagicMock()) - sub_monitor._handle_only_fans_check(post, mock_uow, monitored_sub) + sub_monitor.handle_only_fans_check(post, mock_uow, monitored_sub) mock_uow.user_review.get_by_username.assert_called_once_with('test_user') mock_ban_user.assert_not_called() @@ -98,7 +98,7 @@ def test__handle_only_fans_flagged_user_ban_user(self, mock_ban_user, mock_remov sub_monitor = SubMonitor(MagicMock(), MagicMock(), MagicMock(), MagicMock(), mock_response_handler, config=MagicMock()) - sub_monitor._handle_only_fans_check(post, mock_uow, monitored_sub) + sub_monitor.handle_only_fans_check(post, mock_uow, monitored_sub) mock_ban_user.assert_called_once_with('test_user', 'test_subreddit', 'Profile links match onlyfans.com') mock_remove_post.assert_not_called() @@ -114,7 +114,7 @@ def test__handle_only_fans_flagged_user_remove_post(self, mock_ban_user, mock_re sub_monitor = SubMonitor(MagicMock(), MagicMock(), MagicMock(), MagicMock(), mock_response_handler, config=MagicMock()) - sub_monitor._handle_only_fans_check(post, mock_uow, monitored_sub) + sub_monitor.handle_only_fans_check(post, mock_uow, monitored_sub) mock_ban_user.assert_not_called() mock_remove_post.assert_called_once_with(monitored_sub, ANY) \ No newline at end of file