Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

added feature of terminating when the link is deleted issue number #352 issue has been solved #375

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MONGO_URI=mongodb+srv://<username>:<password>@cluster0-zbsik.mongodb.net
MONGO_DB_NAME=''
CHAT_LINK_DOMAIN=''
MONGO_URI=mongodb://localhost:27017
MONGO_DB_NAME=mydatabase
CHAT_LINK_DOMAIN='http://localhost:3000'
IMAGE_BB_API_KEY=''
IMGUR_API_KEY''
IMGUR_API_KEY=''
3 changes: 2 additions & 1 deletion backend/api/chatLink/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ router.get(
return res.send(link);
})
);
// this route is improved
router.get(
"/status/:channel",
asyncHandler(async (req, res) => {
Expand Down Expand Up @@ -74,4 +75,4 @@ router.delete(
})
);

export default router;
export default router;
2 changes: 1 addition & 1 deletion backend/api/chatLink/utils/link.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { v4 as uuidv4 } from 'uuid';
import { generatePIN } from './pin';

const { CHAT_LINK_DOMAIN } = process.env;
const CHAT_LINK_DOMAIN = process.env.CHAT_LINK_DOMAIN || 'http://localhost:3000';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this fallback.

const PIN_LENGTH = 4;

export type LinkType = {
Expand Down
11 changes: 9 additions & 2 deletions backend/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import {
findOneFromDB as _findOneFromDB, insertInDb as _insertInDb, updateOneFromDb as _updateOneFromDb
} from './inMemDB';

const uri = process.env.MONGO_URI;
const dbName = process.env.MONGO_DB_NAME;
// added local db address if you didnt create a .env file
const uri = process.env.MONGO_URI || 'mongodb://localhost:27017';
const dbName = process.env.MONGO_DB_NAME || 'realtimechatting';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the arbitrary defaults. Let it be set explicitly in .env


console.log('uri', uri);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary console.log

console.log('dbName', dbName);

const client = uri ? new MongoClient(uri) : null;

console.log('client', client);

let db: Db = null;
let inMem = uri ? false : true;

Expand Down
32 changes: 16 additions & 16 deletions client/src/components/DeleteChatLink/Style.module.css
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
.deleteButton {
height: 30px;
display: flex;
width: 80px;
font-family: inherit;
font-size: 14px;
align-items: center;
justify-content: center;
cursor: pointer;
background: #e91e63;
color: #fff;
transition: 180ms;
height: 30px;
display: flex;
width: 80px;
font-family: inherit;
font-size: 14px;
align-items: center;
justify-content: center;
cursor: pointer;
background: #e91e63;
color: #fff;
transition: 180ms;
}

.lightModeDelete {
background: #3cb043;
color: #fff;
background: #3cb043;
color: #fff;
}

.lightModeDelete:hover {
background: #028a0f !important;
background: #028a0f !important;
}

.deleteButton:hover {
background: #ab0b42;
}
background: #ab0b42;
}
83 changes: 80 additions & 3 deletions client/src/pages/messaging/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';


import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
import { useHistory, useParams } from 'react-router-dom';

import {
Expand Down Expand Up @@ -258,6 +260,69 @@ const Chat = () => {
timestamp
};
});

// added a useeffect to check whather the
useEffect(() => {
const validateLink = async () => {
try {
// FOR PRODUCTION MODE URL SHOULD BE https://chat-e2ee-2.azurewebsites.net/api/chat-link/status/${channelID}
// FOR RUNING LOCALLY URL SHOULD BE http://localhost:3001/api/chat-link/status/${channelID}
const URL = `http://localhost:3001/api/chat-link/status/${channelID}`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

urls should not be hard-coded. Also UI layer does not need to know anything about the server. That's why we have the service.
You can expose another method in service, and use that here.

const response = await fetch(URL);
if (response.status !== 200) {
// If the link is invalid, start a 2-second countdown before redirecting
setLinkActive(false);
const timer = setTimeout(() => {
history.push("/"); // Redirect after 2 seconds
}, 2000);

return () => clearTimeout(timer); // Cleanup the timer if component unmounts
} else {
// Link is still valid
setLinkActive(true);
}
} catch (error) {
console.error("Error validating chat link:", error);
setLinkActive(false);
const timer = setTimeout(() => {
history.push("/"); // Redirect after 2 seconds on error
}, 2000); // redireting aftrer 2 second and u can as much u want

return () => clearTimeout(timer); // Cleanup the timer if component unmounts
}
};

// Function to throttle link validation checks
const throttledValidateLink = throttle(validateLink, 1000); // Throttle the function to run every 5 seconds

// Validate the link immediately on component mount
validateLink();

// Periodically revalidate the link every 5 seconds
const intervalId = setInterval(() => {
throttledValidateLink();
}, 1000); // Check every 1 seconds (adjust this interval as needed)

return () => {
clearInterval(intervalId); // Cleanup the interval on component unmount
};
}, [channelID, history]);


// Helper function to throttle a function call
function throttle<T extends (...args: any[]) => void>(func: T, limit: number): (...args: Parameters<T>) => void {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be moved to ./utils.

let inThrottle: boolean;
return function (this: any, ...args: Parameters<T>) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => {
inThrottle = false;
}, limit);
}
};
}

if (linkActive) {
return (
<>
Expand Down Expand Up @@ -307,11 +372,23 @@ const Chat = () => {
return (
<div className={styles.messageContainer}>
<div className={`${styles.messageBlock} ${!darkMode && styles.lightModeContainer}`}>
<p>This link is no longer active</p>
<div style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100vh',
textAlign: 'center',
}}>
<div>
<p>This link has expired or been deleted!</p>
<p>Redirecting to home page in seconds...</p>
</div>
</div>

</div>
</div>
);
}
};

export default Chat;
export default Chat;
Loading