Skip to content

Commit

Permalink
added server invite button and fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ION606 committed Mar 26, 2024
1 parent 64e85ec commit 40d3a97
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
33 changes: 33 additions & 0 deletions client/scripts/chatServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,45 @@ function createChannelLink(channelName, serverId, cid, hasPerms) {
}


const showInvite = (data) => {
const popup = document.createElement('div');
popup.setAttribute("class", "modal");
const contentContainer = document.createElement('p');

const link = document.createElement('span');
link.innerText = data;
link.addEventListener('click', (e) => {
navigator.clipboard.writeText(data);
alert("content copied!");
});

link.style = 'cursor: pointer; padding: 10px;';
contentContainer.appendChild(link);

contentContainer.innerHTML += '<br><br><i>(click outside the container to close)</i>';
contentContainer.setAttribute("class", "modal-content");
contentContainer.style.textAlign = 'center';

popup.appendChild(contentContainer);
const remf = (e) => {
if (contentContainer.matches(":hover")) return;
popup.remove();
document.removeEventListener('click', remf);
}

setTimeout(() => document.addEventListener('click', remf), 1000);
document.getElementById('maincontent').appendChild(popup);
}


function createServerSideBar(data) {
const info = data.serverInfo;
const sidebar = document.getElementById('channels');
const serverId = data.serverInfo.configs.serverId;

const isOwner = JSON.parse(localStorage.getItem('user')).uid == data.serverInfo.configs.owner;
sidebar.appendChild(createServerConfBtn(showInvite, "INVITE", `${window.location.origin}/invite/${serverId}`));

if (isOwner) {
// admin stuff
sidebar.appendChild(createServerConfBtn(showNewChannelPopup, 'NEW CHANNEL'));
Expand Down
3 changes: 3 additions & 0 deletions client/scripts/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function createImage(buf) {
const img = document.createElement('img');
img.src = URL.createObjectURL(new Blob([buf]));
img.width = '250';
img.style.cursor = 'pointer';

img.onload = () => {
const msgs = document.getElementById('messages');
Expand All @@ -11,5 +12,7 @@ function createImage(buf) {
imgs[imgs.length - 1].scrollIntoView();
}

img.onclick = (e) => window.open(e.target.src, 'Image', 'resizable=1');

return img;
}
22 changes: 17 additions & 5 deletions server/database/newMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ export async function broadcastToSessions(client, connectionMap, others, toSend,
}


const splitByID = (channelId, client) => {
return channelId.split("|").filter((o) => (o && o.length > 0));
// get the users in the DM
const splitByID = async (channelId, client) => {
try {
if (!channelId || !client) return null;
const dbo = client.db('dms').collection(channelId);
const users = (await dbo.findOne({_id: 'configs'}))?.users;
return (users) ? users.split("|").filter((o) => (o && o.length > 0)) : null;
}
catch(err) {
console.error("SPLIT ERROR", err);
return null;
}
}


Expand Down Expand Up @@ -82,7 +92,7 @@ async function deleteMessage(mongoconnection, connectionMap, data) {

// get the key
var doc = await client.db(data.user.uid).collection('dm_keys').findOne({dmid: data.chatid});
const others = (doc.isGroupDM) ? splitByID(doc.uid) : [data.user.uid, doc.uid];
const others = (doc.isGroupDM) ? await splitByID(doc.uid, client) : [data.user.uid, doc.uid];

const mbo = client.db((doc.isGroupDM) ? 'gdms' : 'dms').collection(data.chatid);
doc = await mbo.findOne({id: data.id});
Expand Down Expand Up @@ -113,7 +123,7 @@ async function editMessage(mongoconnection, connectionMap, data) {
const client = await mongoconnection;

var doc = await client.db(data.user.uid).collection('dm_keys').findOne({dmid: data.chatid});
const others = (doc.isGroupDM) ? splitByID(doc.uid) : [data.user.uid, doc.uid];
const others = (doc.isGroupDM) ? await splitByID(doc.uid, client) : [data.user.uid, doc.uid];

const mbo = client.db((doc.isGroupDM) ? 'gdms' : 'dms').collection(data.chatid);
doc = await mbo.findOne({id: data.id});
Expand Down Expand Up @@ -145,7 +155,9 @@ export async function markDMAsRead(mongoconnection, connectionMap, data) {
const client = await mongoconnection;
const dbo = client.db(uid).collection('dm_keys');

const ids = splitByID(data.data.dmid);
const ids = await splitByID(data.data.dmid, client);
if (!ids) return null;

for (const oid of ids) {
if (oid == uid) continue;
dbo.updateOne({uid: oid}, {$set: {unread: false}});
Expand Down
1 change: 0 additions & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ app.get('/msgImg', async (req, res) => {
if (!fname) return res.sendStatus(404);

const channelIDFull = (serverid) ? `${serverid}/${channelid}` : channelid;
console.log(channelIDFull);

const file = await CDNManager.getFile(channelIDFull, fname);
return res.send(file);
Expand Down

0 comments on commit 40d3a97

Please sign in to comment.