def get_comment_by_thread(self, thread_id): log.debug(f"Fetching comment for thread: {thread_id}") c = self.dbConn.cursor() c.execute(''' SELECT ID, ThreadID, CommentID, ReminderId, CurrentCount, User, Source FROM comments WHERE ThreadID = ? ''', (thread_id,)) result = c.fetchone() if result is None or len(result) == 0: log.debug("No comment found") return None db_comment = DbComment( thread_id=result[1], comment_id=result[2], reminder_id=result[3], user=result[5], source=result[6], current_count=result[4], db_id=result[0] ) return db_comment
def test_delete_comment(database, reddit): db_comment = DbComment(thread_id="XXXXX", comment_id="ZZZZZ", reminder_id="YYYYY", user="******", source="www.reddit.com/r/test/comments/XXXXX", current_count=1) database.save_comment(db_comment) comment = reddit_test.RedditObject(body="Click here for a reminder!", author=static.ACCOUNT_NAME, id="ZZZZZ") reddit.add_comment(comment, True) message = reddit_test.RedditObject(body=f"Delete! SSSSSS", author="Watchful1") messages.process_message(message, reddit, database) assert "This comment doesn't exist or was already deleted." in message.get_first_child( ).body message = reddit_test.RedditObject(body=f"Delete! XXXXX", author="Watchful2") messages.process_message(message, reddit, database) assert "It looks like the bot wasn't replying to you." in message.get_first_child( ).body message = reddit_test.RedditObject(body=f"Delete! XXXXX", author="Watchful1") messages.process_message(message, reddit, database) assert "Comment deleted." in message.get_first_child().body assert comment.id not in reddit.all_comments
def process_comment(comment, reddit, database, count_string=""): reminder, result_message = parse_comment(comment, database, count_string, reddit) if reminder is None: counters.replies.labels(source='comment', type='other').inc() log.debug("Not replying") return commented = False thread_id = utils.id_from_fullname(comment['link_id']) comment_result = None if database.get_comment_by_thread(thread_id) is not None: comment_result = ReturnType.THREAD_REPLIED if comment_result is None and database.get_subreddit_banned( comment['subreddit']): comment_result = ReturnType.FORBIDDEN if comment_result is None: reminder.thread_id = thread_id reddit_comment = reddit.get_comment(comment['id']) bldr = utils.get_footer( reminder.render_comment_confirmation( thread_id, pushshift_minutes=reddit.get_effective_pushshift_lag())) result_id, comment_result = reddit.reply_comment( reddit_comment, ''.join(bldr)) if comment_result in (ReturnType.INVALID_USER, ReturnType.USER_DOESNT_EXIST, ReturnType.THREAD_LOCKED, ReturnType.DELETED_COMMENT, ReturnType.RATELIMIT): log.info(f"Unable to reply as comment: {comment_result.name}") elif comment_result == ReturnType.FORBIDDEN: log.info(f"Banned in subreddit, saving: {comment['subreddit']}") database.ban_subreddit(comment['subreddit']) else: if comment_result == ReturnType.NOTHING_RETURNED: result_id = "QUARANTINED" log.warning( f"Opting in to quarantined subreddit: {comment['subreddit']}" ) reddit.quarantine_opt_in(comment['subreddit']) if result_id is None: log.warning( f"Got comment ID of None when replying to {comment['id']}") comment_result = ReturnType.FORBIDDEN else: log.info( f"Reminder created: {reminder.id} : {utils.get_datetime_string(reminder.target_date)}, " f"replied as comment: {result_id}") if comment_result != ReturnType.QUARANTINED and comment[ 'subreddit'] != "RemindMeBot": db_comment = DbComment(thread_id=thread_id, comment_id=result_id, reminder_id=reminder.id, user=reminder.user.name, source=reminder.source) database.save_comment(db_comment) commented = True if not commented: log.info( f"Reminder created: {reminder.id} : {utils.get_datetime_string(reminder.target_date)}, " f"replying as message: {comment_result.name}") bldr = utils.get_footer( reminder.render_message_confirmation( result_message, comment_result, pushshift_minutes=reddit.get_effective_pushshift_lag())) result = reddit.send_message(comment['author'], "RemindMeBot Confirmation", ''.join(bldr)) if result != ReturnType.SUCCESS: log.info(f"Unable to send message: {result.name}")
def get_incorrect_comments(self, count): log.debug(f"Fetching incorrect comments") c = self.dbConn.cursor() results = [] for row in c.execute(''' SELECT cm.ID, cm.ThreadID, cm.CommentID, cm.ReminderId, cm.CurrentCount, cm.User, cm.Source, rm.ID, rm.Source, rm.RequestedDate, rm.TargetDate, rm.Message, rm.User, rm.Defaulted, rm.TimeZone, rm.NewCount FROM comments cm LEFT JOIN ( SELECT rm1.ID, rm1.Source, rm1.RequestedDate, rm1.TargetDate, rm1.Message, rm1.User, rm1.Defaulted, us.TimeZone, count(*) as NewCount FROM reminders rm1 INNER JOIN reminders rm2 ON rm1.Source = rm2.Message LEFT JOIN user_settings us ON us.User = rm1.User GROUP BY rm1.ID ) AS rm ON cm.ReminderId = rm.ID WHERE rm.NewCount != cm.CurrentCount LIMIT ? ''', (count,)): db_comment = DbComment( thread_id=row[1], comment_id=row[2], reminder_id=row[3], user=row[5], source=row[6], current_count=row[4], db_id=row[0] ) reminder = Reminder( source=row[8], target_date=utils.parse_datetime_string(row[10]), message=row[11], user=row[12], db_id=row[7], requested_date=utils.parse_datetime_string(row[9]), count_duplicates=row[15], thread_id=row[1], timezone=row[14], defaulted=row[13] == 1 ) results.append((db_comment, reminder)) log.debug(f"Found incorrect comments: {len(results)}") return results