Skip to content

Commit

Permalink
Merge pull request #376 from Aymanhki/ayman
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
Aymanhki authored Apr 9, 2024
2 parents 73fcd39 + a3568a6 commit 7b9279b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 86 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 ORDER BY created_at DESC LIMIT %s OFFSET %s", (user_id, limit, offset))
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))
notifs = curr.fetchall()

for i in range(len(notifs)):
Expand Down
4 changes: 2 additions & 2 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, target_username, target_name) VALUES (%s, %s, %s, %s, %s)", (mentioned_user[0], "mention", post_id, user_info[0], user_info[1]))
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]))
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, target_username, target_name) VALUES (%s, %s, %s, %s, %s)", (mentioned_user[0], "mention", post_id, user_info[0], user_info[1]))
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]))
connection.commit()

if old_post_caption:
Expand Down
4 changes: 2 additions & 2 deletions epoch_frontend/src/modules/EditProfilePopup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, {useState, useRef, useEffect} from 'react';
import React, {useState, useRef, useEffect, useContext} from 'react';
import {updateUser} from '../services/user';
import '../styles/EditProfilePopup.css';
import {useSpring, animated} from 'react-spring';


function EditProfilePopup({onClose, user, showEditProfilePopup, setShowEditProfilePopup, refreshProfile, setRefreshProfile, profilePicId, profilePicUrl, profilePicName, profilePicType, backgroundPicId, backgroundPicUrl, backgroundPicName, backgroundPicType}) {
const [formData, setFormData] = useState({
username: user.username,
Expand Down Expand Up @@ -45,7 +46,6 @@ function EditProfilePopup({onClose, user, showEditProfilePopup, setShowEditProfi
const maxImageBytes = 30000001;
const allowedFileTypes = ["jpg", "jpeg", "png", "gif", "HEIC", "JPG", "JPEG", "PNG", "GIF", "heic"];


const {transform: inTransform, opacity: inOpacity} = useSpring({
opacity: showEditProfilePopup ? 1 : 0,
transform: `translateY(${showEditProfilePopup ? 0 : 100}vh)`,
Expand Down
27 changes: 1 addition & 26 deletions epoch_frontend/src/modules/NotificationItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,7 @@ function NotificationItem({notification, setShowNotifications, onRead}) {


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 true;
}


Expand Down
6 changes: 6 additions & 0 deletions epoch_frontend/src/modules/ProtectedRoute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useContext } from 'react';
import { UserContext } from '../services/UserContext';

// Utility function to get the "epoch_session_id" cookie
export function getSessionCookie() {
Expand All @@ -14,9 +16,13 @@ export function isSessionActive() {
const ProtectedRoute = ({ children }) => {
const navigate = useNavigate();



useEffect(() => {
// Check if the session is active; if not, redirect to the login page
if (!isSessionActive()) {
const { updateUser } = useContext(UserContext);
updateUser(null);
navigate('/epoch/login');
}
}, [navigate]);
Expand Down
58 changes: 4 additions & 54 deletions epoch_frontend/src/pages/Comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {Spinner} from '../modules/Spinner';
import {getAllComments} from '../services/comments'
import NavBar from "../modules/NavBar";
import PostPopup from "../modules/PostPopup";
import Post from '../modules/Post'
import {useLocation} from 'react-router-dom';
import Comment from '../modules/Comment';
import '../styles/PostComments.css'
Expand All @@ -28,9 +27,6 @@ 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 @@ -39,14 +35,9 @@ 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 @@ -59,26 +50,6 @@ 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 @@ -88,7 +59,6 @@ function Comments() {
console.log(error)
setRefreshComments(false);
setIsLoading(false);
setFetchedPost(false);
navigate('/epoch/login')
})
}
Expand All @@ -103,35 +73,13 @@ 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);
setIsLoading(false);
setFetchedPost(false);
navigate('/epoch/login')
})
}
Expand All @@ -145,7 +93,7 @@ function Comments() {
return (
<>
{user ? (<NavBar profilePic={user.profile_pic_data} profilePicType={user.profile_pic_type}
showNewPostPopup={showNewPostPopup} setShowNewPostPopup={setShowNewPostPopup} userId={user.id}/>) : (
showNewPostPopup={showNewCommentPopup} setShowNewPostPopup={setShowNewCommentPopup} userId={user.id}/>) : (
<NoSessionNavBar></NoSessionNavBar>)}
{isLoading ? (
<Spinner/>
Expand All @@ -167,7 +115,9 @@ function Comments() {
</div>

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

{comments && comments.length === 0 &&
<div className={"no-comments"}>No comments yet</div>}
Expand Down
1 change: 0 additions & 1 deletion epoch_frontend/src/pages/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ function Profile() {
.catch(error => {
console.log("Error fetching user info:", error);
setIsLoading(false);

});
}
}, [refreshProfile, setRefreshProfile, updateUser]);
Expand Down

0 comments on commit 7b9279b

Please sign in to comment.