From e65f86dc3c4a53c4e0695e71821d6d1d5a5fd257 Mon Sep 17 00:00:00 2001 From: castmallards Date: Tue, 9 Apr 2024 10:39:37 -0500 Subject: [PATCH] Bug fixes --- .../epoch/epoch_notification_persistence.py | 2 +- .../epoch/epoch_post_persistence.py | 6 +- .../src/modules/NotificationItem.js | 32 +++++++++- .../src/modules/NotificationsPopup.js | 1 + epoch_frontend/src/pages/Comments.js | 60 ++++++++++++++++++- 5 files changed, 93 insertions(+), 8 deletions(-) diff --git a/epoch_backend/persistence/epoch/epoch_notification_persistence.py b/epoch_backend/persistence/epoch/epoch_notification_persistence.py index d3a8915..9a64adb 100644 --- a/epoch_backend/persistence/epoch/epoch_notification_persistence.py +++ b/epoch_backend/persistence/epoch/epoch_notification_persistence.py @@ -11,7 +11,7 @@ def get_user_notifications(self, user_id: int, limit: int, offset: int): conn = get_db_connection() curr = conn.cursor() - curr.execute("SELECT * FROM notifications WHERE user_id = %s AND created_at > now() ORDER BY created_at DESC LIMIT %s OFFSET %s", (user_id, limit, offset)) + curr.execute("SELECT * FROM notifications WHERE user_id = %s ORDER BY created_at DESC LIMIT %s OFFSET %s", (user_id, limit, offset)) notifs = curr.fetchall() for i in range(len(notifs)): diff --git a/epoch_backend/persistence/epoch/epoch_post_persistence.py b/epoch_backend/persistence/epoch/epoch_post_persistence.py index fdda955..a5e0d1f 100644 --- a/epoch_backend/persistence/epoch/epoch_post_persistence.py +++ b/epoch_backend/persistence/epoch/epoch_post_persistence.py @@ -52,7 +52,7 @@ def add_post(self, new_post: post): mentioned_notification = cursor.fetchone() if not mentioned_notification and mentioned_user[0] != new_post.user_id: - cursor.execute("INSERT INTO notifications (user_id, type, target_id, created_at, target_username, target_name) VALUES (%s, %s, %s, %s, %s, %s)", (mentioned_user[0], "mention", post_id, new_post.release, user_info[0], user_info[1])) + 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() @@ -205,7 +205,7 @@ def update_post(self, post_id: int, new_post: post): mentioned_notification = cursor.fetchone() if not mentioned_notification and mentioned_user[0] != new_post.user_id: - cursor.execute("INSERT INTO notifications (user_id, type, target_id, created_at, target_username, target_name) VALUES (%s, %s, %s, %s, %s, %s)", (mentioned_user[0], "mention", post_id, new_post.release, user_info[0], user_info[1])) + 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: @@ -229,7 +229,7 @@ def update_post(self, post_id: int, new_post: post): def get_followed_users_posts(self, user_id: int, offset: int, limit: int): connection = get_db_connection() cursor = connection.cursor() - cursor.execute("SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM following WHERE user_id = %s) ORDER BY created_at DESC LIMIT %s OFFSET %s", (user_id, limit, offset)) + cursor.execute("SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM following WHERE user_id = %s) OR user_id = %s ORDER BY created_at DESC LIMIT %s OFFSET %s", (user_id, user_id, limit, offset)) posts = cursor.fetchall() posts_media = get_posts_media(posts) posts_users_info = get_posts_users_info(posts) diff --git a/epoch_frontend/src/modules/NotificationItem.js b/epoch_frontend/src/modules/NotificationItem.js index 2c6477f..090de70 100644 --- a/epoch_frontend/src/modules/NotificationItem.js +++ b/epoch_frontend/src/modules/NotificationItem.js @@ -8,6 +8,7 @@ import ConnectWithoutContactOutlinedIcon from '@mui/icons-material/ConnectWithou import React from "react"; import {useNavigate} from "react-router-dom"; import {useEffect, useState} from "react"; +import { getAllComments } from '../services/comments'; import '../styles/NotificationItem.css'; function NotificationItem({notification, setShowNotifications, onRead}) { @@ -67,10 +68,39 @@ function NotificationItem({notification, setShowNotifications, onRead}) { }, [notification]); + const isNotificationVisible = () => { + let toReturn = true; + + if(notification) { + if(notification.type === 'mention') { + getAllComments(notification.target_id) + .then(data => { + let thePost = data.post; + + const now = new Date(); + let postTime = new Date(thePost.release); + postTime = new Date(Date.UTC(postTime.getFullYear(), postTime.getMonth(), postTime.getDate(), postTime.getHours(), postTime.getMinutes(), postTime.getSeconds())); + let isInPast = now >= postTime; + + if (!isInPast) { + toReturn = false; + } + }) + .catch(error => { + toReturn = false; + }) + + + } + } + + return toReturn; + } return ( -
{ +
{ navigate(notificationActionLink); setNotificationRead(true); onRead(notification.notif_id); diff --git a/epoch_frontend/src/modules/NotificationsPopup.js b/epoch_frontend/src/modules/NotificationsPopup.js index 827040a..e00f42d 100644 --- a/epoch_frontend/src/modules/NotificationsPopup.js +++ b/epoch_frontend/src/modules/NotificationsPopup.js @@ -168,6 +168,7 @@ function NotificationsPopup ({showNotifications, setShowNotifications, newUnread } + return ( { @@ -35,9 +39,14 @@ function Comments() { getUserInfo() .then(data => { updateUser(data); + setFailedToFetchUser(false); }) .catch(error => { updateUser(null); + setFailedToFetchUser(true); + if(mentionedUsers.length > 0 && fetchedPost){ + navigate('/epoch/login') + } }); } @@ -50,6 +59,26 @@ function Comments() { setIsLoading(true); getAllComments(postId) .then(data => { + const usernames = []; + const regex = /@([a-zA-Z0-9_]+)/g; + + if (data.post.caption) { + const matches = data.post.caption.match(regex); + + if (matches) { + for (let i = 0; i < matches.length; i++) { + usernames.push(matches[i].substring(1)); + } + } + } + setMentionedUsers(usernames); + + if(usernames.length > 0 && user === null && failedToFetchUser){ + navigate('/epoch/login') + } + + setFetchedPost(true); + setCommentsPost(data.post); setComments(data.comments); setRefreshComments(false); @@ -59,6 +88,7 @@ function Comments() { console.log(error) setRefreshComments(false); setIsLoading(false); + setFetchedPost(false); navigate('/epoch/login') }) } @@ -73,13 +103,35 @@ function Comments() { if (postId !== -1) { getAllComments(postId) .then(data => { + + const usernames = []; + const regex = /@([a-zA-Z0-9_]+)/g; + + if (data.post.caption) { + const matches = data.post.caption.match(regex); + + if (matches) { + for (let i = 0; i < matches.length; i++) { + usernames.push(matches[i].substring(1)); + } + } + } + setMentionedUsers(usernames); + + if(usernames.length > 0 && user === null && failedToFetchUser){ + navigate('/epoch/login') + } + + setFetchedPost(true); + setCommentsPost(data.post); setComments(data.comments); setIsLoading(false); }) .catch(error => { - console.log(error) + console.log(error); setIsLoading(false); + setFetchedPost(false); navigate('/epoch/login') }) } @@ -115,7 +167,9 @@ function Comments() {
{user && ()} + onClick={() => setShowNewCommentPopup(!showNewCommentPopup)}>{ + `${showNewCommentPopup ? '+' : }` + })} {comments && comments.length === 0 &&
No comments yet
} @@ -140,7 +194,7 @@ function Comments() { )} {user && ()} + profilePic={user.profile_pic_data} refreshFeed={refreshComments} setRefreshFeed={setRefreshComments}/>)} {user && (