Skip to content

Commit

Permalink
FS: added two-way-match
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinav Bhattarai committed Jan 17, 2021
1 parent 21b7d57 commit 3280db7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
div{
display: flex;
flex-direction: column;
margin-left: 4%;

header{
font-weight: 500;
Expand Down
44 changes: 38 additions & 6 deletions client/src/Containers/MainPage/mainPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,43 @@ const MainPage = ({ authenticate, history }) => {
SetJoinedRoom( null );
}

const SendMatchRequest = (friend_name)=>{
const TwoWayMatchHandler = (context)=>{
RemoveRequestData(context.FriendName);
AddToMatchesBackend(context.FriendName, context.FriendProfile);
RemoveRequestSectionBackend(context.FriendName);
}

const OneWayMatchHandler = (context)=>{
axios.put('http://localhost:8000/friend-requests', context);
}

const MatchTypeCheck = (username)=>{
const dummy = [...requests];
if(dummy.length >= 1){
const index = dummy.findIndex((element)=>{
return element.sender === username;
})
if(index !== -1){
return true;
}
return false;
}
return false;
}

const SendMatchRequest = (friend_name, friend_profile)=>{
const context = {
YourName: localStorage.getItem('Username'),
YourProfile: my_profile_pic,
FriendName: friend_name,
FriendProfile: friend_profile
};
axios.put('http://localhost:8000/friend-requests', context);

if( MatchTypeCheck(friend_name) ){
TwoWayMatchHandler(context);
}else{
OneWayMatchHandler(context);
}
};

const FetchNewPost = ()=>{
Expand Down Expand Up @@ -244,6 +274,7 @@ const MainPage = ({ authenticate, history }) => {
const dummy = [...post_list];
// realtime request
const FriendName = dummy[0].Username;
const FriendProfile = dummy[0].ProfilePicture;
const MyName = localStorage.getItem('Username');
socket.emit('Send-Friend-Request', FriendName, MyName, my_profile_pic);

Expand All @@ -254,7 +285,7 @@ const MainPage = ({ authenticate, history }) => {
}
dummy.splice(0, 1);
SetPostList(dummy);
SendMatchRequest(FriendName);
SendMatchRequest(FriendName, FriendProfile);
SetReactionBackend(FriendName);
};

Expand All @@ -263,6 +294,7 @@ const MainPage = ({ authenticate, history }) => {
const dummy = [...post_list];
// realtime request
const FriendName = dummy[0].Username;
const FriendProfile = dummy[0].ProfilePicture;
const MyName = localStorage.getItem('Username');
socket.emit('Send-Friend-Request', FriendName, MyName, my_profile_pic);

Expand All @@ -274,7 +306,7 @@ const MainPage = ({ authenticate, history }) => {

dummy.splice(0, 1);
SetPostList(dummy);
SendMatchRequest(FriendName);
SendMatchRequest(FriendName, FriendProfile);
SetReactionBackend(FriendName);

};
Expand Down Expand Up @@ -399,10 +431,10 @@ const MainPage = ({ authenticate, history }) => {
const AddNotification = (sender, profile)=>{
if(notification_list){
const dummy = [...notification_list];
dummy.push({sender, profile});
dummy.push({sender, ProfilePicture: profile});
SetNotificationList(dummy);
}else{
SetNotificationList([{sender, profile}]);
SetNotificationList([{sender, ProfilePicture: cprofile}]);
}
if(current_request_bar_value === 0){
SetNotificationAlert(true);
Expand Down
15 changes: 15 additions & 0 deletions server/Middleware/redis-notification-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import redis from 'redis';

const cache = redis.createClient();

const NotificationCache = (req, res, next)=>{
cache.get(`notification/${req.params.Username}`, (err, reply)=>{
if(!err && reply){
const data = JSON.parse(reply);
return res.json(data);
}
next();
});
}

export default NotificationCache;
13 changes: 10 additions & 3 deletions server/Routes/notifications.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import express from 'express';
import RegistrationModel from '../Models/register-model.js';
import redis from 'redis';
import NotificationCache from '../Middleware/redis-notification-cache.js';

const cache = redis.createClient();
const router = express.Router();

router.get('/:Username', (req, res)=>{
router.get('/:Username', NotificationCache, (req, res)=>{
const Username = req.params.Username;
RegistrationModel.findOne({Username}).exec().then((response)=>{
if(response){
return res.json(response.Notification);
cache.set(`notification/${Username}`, JSON.stringify(response.Notification), ()=>{
return res.json(response.Notification);
})
}
})
})
Expand All @@ -22,7 +27,9 @@ router.put('/', (req, res)=>{
dummy.push({sender: Sender, ProfilePicture});
response.Notification = dummy;
response.save().then(()=>{
return res.json({ notification_added: true });
cache.set(`notification/${Username}`, JSON.stringify(dummy), ()=>{
return res.json({ notification_added: true });
})
})
}else{
return res.json({ no_username: true });
Expand Down

0 comments on commit 3280db7

Please sign in to comment.