Skip to content

Commit

Permalink
Merge pull request #374 from Aymanhki/obaid
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
Aymanhki authored Apr 9, 2024
2 parents ee54189 + e65f86d commit 89214da
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
6 changes: 3 additions & 3 deletions epoch_backend/persistence/epoch/epoch_post_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
32 changes: 31 additions & 1 deletion epoch_frontend/src/modules/NotificationItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}) {
Expand Down Expand Up @@ -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 (
<div className={`notification-item-container ${notificationRead ? 'notification-read' : 'notification-unread'}`} onClick={() => {
<div className={`notification-item-container ${notificationRead ? 'notification-read' : 'notification-unread'}`} style={{display: ( isNotificationVisible() ) ? 'block' : 'none'}}
onClick={() => {
navigate(notificationActionLink);
setNotificationRead(true);
onRead(notification.notif_id);
Expand Down
1 change: 1 addition & 0 deletions epoch_frontend/src/modules/NotificationsPopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function NotificationsPopup ({showNotifications, setShowNotifications, newUnread
}



return (
<animated.div
style={{
Expand Down
60 changes: 57 additions & 3 deletions epoch_frontend/src/pages/Comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {getUserInfo} from "../services/user";
import CommentPopup from "../modules/CommentPopup";
import {useNavigate} from "react-router-dom";
import NoSessionNavBar from "../modules/NoSessionNavBar";
import ForumOutlinedIcon from '@mui/icons-material/ForumOutlined';
import Feed from "../modules/Feed";


Expand All @@ -27,6 +28,9 @@ function Comments() {
const [showNewPostPopup, setShowNewPostPopup] = useState(false);
const [showNewCommentPopup, setShowNewCommentPopup] = useState(false);
const [refreshComments, setRefreshComments] = useState(false);
const [mentionedUsers, setMentionedUsers] = useState([]);
const [failedToFetchUser, setFailedToFetchUser] = useState(false);
const [fetchedPost, setFetchedPost] = useState(false);
const navigate = useNavigate();

useEffect(() => {
Expand All @@ -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')
}
});

}
Expand All @@ -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);
Expand All @@ -59,6 +88,7 @@ function Comments() {
console.log(error)
setRefreshComments(false);
setIsLoading(false);
setFetchedPost(false);
navigate('/epoch/login')
})
}
Expand All @@ -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')
})
}
Expand Down Expand Up @@ -115,7 +167,9 @@ function Comments() {
</div>

{user && (<button className={`new-comment-button ${showNewCommentPopup ? 'rotate' : ''}`}
onClick={() => setShowNewCommentPopup(!showNewCommentPopup)}>+</button>)}
onClick={() => setShowNewCommentPopup(!showNewCommentPopup)}>{
`${showNewCommentPopup ? '+' : <ForumOutlinedIcon></ForumOutlinedIcon>}`
}</button>)}

{comments && comments.length === 0 &&
<div className={"no-comments"}>No comments yet</div>}
Expand All @@ -140,7 +194,7 @@ function Comments() {
)}

{user && (<PostPopup showPopup={showNewPostPopup} setShowPopup={setShowNewPostPopup} username={user.username}
profilePic={user.profile_pic_data} refreshFeed={refreshFeed} setRefreshFeed={setRefreshFeed}/>)}
profilePic={user.profile_pic_data} refreshFeed={refreshComments} setRefreshFeed={setRefreshComments}/>)}

{user && (<CommentPopup showPopup={showNewCommentPopup} setShowPopup={setShowNewCommentPopup} postId={postId}
username={user.username} profilePic={user.profile_pic_data} refreshComments={refreshComments}
Expand Down

0 comments on commit 89214da

Please sign in to comment.