Skip to content

Commit

Permalink
Merge pull request #363 from Aymanhki/ayman
Browse files Browse the repository at this point in the history
Bug fix for dedicating a post
  • Loading branch information
Aymanhki authored Mar 30, 2024
2 parents b7684f9 + 8408ae8 commit 6b8fd80
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions epoch_backend/persistence/epoch/epoch_post_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def add_post(self, new_post: post):
connection.commit()

if new_post.caption:
username_reg = re.compile(r'@([a-zA-Z0-9_]+)')
username_reg = re.compile(r'(@[a-zA-Z0-9_]+)')
words = re.split(username_reg, new_post.caption)
mentioned = words

mentioned = [word for word in words if word.startswith('@')]
mentioned = [mention[1:] for mention in mentioned] # remove the '@' from the username

if len(mentioned) > 0:
cursor.execute("SELECT user_id, username, name FROM users WHERE username IN %s", (tuple(mentioned),))
Expand All @@ -45,7 +45,7 @@ def add_post(self, new_post: post):
cursor.execute("SELECT * FROM notifications WHERE user_id=%s AND target_id=%s AND type=%s", (mentioned_user[0], post_id, "mention"))
mentioned_notification = cursor.fetchone()

if not mentioned_notification:
if not mentioned_notification and mentioned_user[0] != new_post.user_id:
cursor.execute("INSERT INTO notifications (user_id, type, target_id, target_username, target_name) VALUES (%s, %s, %s, %s, %s)", (mentioned_user[0], "mention", post_id, user_info[0], user_info[1]))
connection.commit()

Expand Down Expand Up @@ -175,17 +175,13 @@ def update_post(self, post_id: int, new_post: post):
(new_post.caption, new_post.release, post_id))
connection.commit()

# if the old caption had a mention that was removed in the new caption, remove the notification
# if the new caption has a mention that was not in the old caption, add the notification


new_mentioned = []

if new_post.caption:
new_username_reg = re.compile(r'@([a-zA-Z0-9_]+)')
new_username_reg = re.compile(r'(@[a-zA-Z0-9_]+)')
new_words = re.split(new_username_reg, new_post.caption)
new_mentioned = new_words

new_mentioned = [word for word in new_words if word.startswith('@')]
new_mentioned = [mention[1:] for mention in new_mentioned] # remove the '@' from the username

if len(new_mentioned) > 0:
cursor.execute("SELECT user_id, username, name FROM users WHERE username IN %s", (tuple(new_mentioned),))
Expand All @@ -197,14 +193,15 @@ def update_post(self, post_id: int, new_post: post):
cursor.execute("SELECT * FROM notifications WHERE user_id=%s AND target_id=%s AND type=%s", (mentioned_user[0], post_id, "mention"))
mentioned_notification = cursor.fetchone()

if not mentioned_notification:
if not mentioned_notification and mentioned_user[0] != new_post.user_id:
cursor.execute("INSERT INTO notifications (user_id, type, target_id, target_username, target_name) VALUES (%s, %s, %s, %s, %s)", (mentioned_user[0], "mention", post_id, user_info[0], user_info[1]))
connection.commit()

if old_post_caption:
old_username_reg = re.compile(r'@([a-zA-Z0-9_]+)')
old_username_reg = re.compile(r'(@[a-zA-Z0-9_]+)')
old_words = re.split(old_username_reg, old_post_caption)
old_mentioned = old_words
old_mentioned = [word for word in old_words if word.startswith('@')]
old_mentioned = [mention[1:] for mention in old_mentioned] # remove the '@' from the username

for old_mention in old_mentioned:
if old_mention not in new_mentioned:
Expand Down

0 comments on commit 6b8fd80

Please sign in to comment.