Skip to content

Commit

Permalink
you can now delete messages yay
Browse files Browse the repository at this point in the history
  • Loading branch information
marceldobehere committed Aug 18, 2024
1 parent 9df62b8 commit a3aef61
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 14 deletions.
11 changes: 10 additions & 1 deletion client/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,18 @@ body {
text-wrap: wrap;
}

.chat-entry-reply {
.chat-entry-reply, .chat-entry-delete, .chat-entry-edit {
float: right;
}
.chat-entry-reply {
color: #79c0ff;
}
.chat-entry-delete {
color: red;
}
.chat-entry-edit {
color: yellow;
}

.chat-entry img {
max-height: 15rem;
Expand Down
45 changes: 37 additions & 8 deletions client/js/lists/chatList.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ function getReplyStr(message)
}


function createChatEntry(username, time, message)
function createChatEntry(username, time, message, mine, messageId)
{
let li = document.createElement("li");
li.id = `chat-msg-${messageId}`;
let div = document.createElement("div");
div.className = "chat-entry";
let span1 = document.createElement("span");
Expand All @@ -60,6 +61,26 @@ function createChatEntry(username, time, message)
docChatInputElement.focus();
};

let deleteButton = document.createElement("button");
deleteButton.className = "chat-entry-delete";
deleteButton.textContent = "X";
deleteButton.onclick = async () => {
if (!confirm("Do you really want to delete this message?"))
return;

logInfo("DELETE MSG: " + messageId);
await doMsgSendThingy("delete-msg", {messageId: messageId}, true);
await internalRemoveUserMessage(currentUser['mainAccount'], getCurrentChatUserId(), messageId);
await messageDeletedUI(currentUser['mainAccount'], getCurrentChatUserId(), messageId);
};

let editButton = document.createElement("button");
editButton.className = "chat-entry-edit";
editButton.textContent = "E";
editButton.onclick = async () => {
alert("Editing messages not implemented yet!")
};

let br = document.createElement("br");
let p = document.createElement("p");
p.className = "chat-entry-message";
Expand All @@ -79,6 +100,10 @@ function createChatEntry(username, time, message)
div.appendChild(span1);
div.appendChild(document.createTextNode(" - "));
div.appendChild(span2);
if (mine) {
div.appendChild(deleteButton);
div.appendChild(editButton);
}
div.appendChild(replyButton);
div.appendChild(br);
div.appendChild(p);
Expand Down Expand Up @@ -169,7 +194,7 @@ async function createChatList(serverId, channelId, scrollDown) {
{
let msg = messages[i];
let username = userGetInfoDisplayUsername(currentUser['mainAccount'], msg["from"]);
createChatEntry(username, msg["date"], msg["data"]);
createChatEntry(username, msg["date"], msg["data"], currentUser['mainAccount']['userId'] == msg["from"], msg["messageId"]);
}
}
else
Expand All @@ -196,11 +221,8 @@ async function createChatList(serverId, channelId, scrollDown) {
{
let msg = messages[i];
let username = userGetInfoDisplayUsername(currentUser['mainAccount'], msg["from"]);
createChatEntry(username, msg["date"], msg["data"]);
createChatEntry(username, msg["date"], msg["data"], currentUser['mainAccount']['userId'] == msg["from"], msg["messageId"]);
}

// for (let i = 0; i < 30; i++)
// createChatEntry(`User ${i}`, "2024-04-27T17:43:03.164Z", `Test Message ${i} for chat ${channelId}`);
}


Expand Down Expand Up @@ -245,6 +267,13 @@ async function channelClicked(element, channelId, serverId) {
docChatInputElement.focus();
}

async function messageDeletedUI(account, chatUserId, messageId)
{
let li = document.getElementById(`chat-msg-${messageId}`);
if (li)
li.remove();
}

async function messageReceivedUI(account, chatUserId, message)
{
/* console.log("MESSAGE RECEIVED")
Expand All @@ -262,7 +291,7 @@ async function messageReceivedUI(account, chatUserId, message)

//await createChatList(groupInfo["groupId"], groupInfo["channelId"], true);
await refreshChatListArea(groupInfo["groupId"], groupInfo["channelId"]);
await createChatEntry(userGetInfoDisplayUsername(currentUser['mainAccount'], message["from"]), message["date"], message["data"]);
createChatEntry(userGetInfoDisplayUsername(currentUser['mainAccount'], message["from"]), message["date"], message["data"], currentUser['mainAccount']['userId'] == message["from"], message["messageId"]);
docChatUlDiv.scrollTop = docChatUlDiv.scrollHeight;
}

Expand All @@ -278,7 +307,7 @@ async function messageReceivedUI(account, chatUserId, message)

//await createChatList(DMsId, chatUserId, true);
await refreshChatListArea(DMsId, chatUserId);
await createChatEntry(userGetInfoDisplayUsername(currentUser['mainAccount'], message["from"]), message["date"], message["data"]);
createChatEntry(userGetInfoDisplayUsername(currentUser['mainAccount'], message["from"]), message["date"], message["data"], currentUser['mainAccount']['userId'] == message["from"], message["messageId"]);
docChatUlDiv.scrollTop = docChatUlDiv.scrollHeight;
}

Expand Down
2 changes: 1 addition & 1 deletion client/lib/group/groupHandlerFuncs.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function handleGroupMessage(account, msgObj, dontAdd)
if (!dontAdd)
await addMessageToUser(account, from, getChannelStrFromGroup(groupId, channelId), sendMsgObj, date);

logWarn("Group messages not fully implemented yet");
//logWarn("Group messages not fully implemented yet");
}

async function handleGroupInvite(account, msgObj)
Expand Down
9 changes: 9 additions & 0 deletions client/lib/msg/localMsgInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ async function internalGetUserMessages(account, userId)
);
}

async function internalGetUserMessage(account, userId, messageId)
{
return await _intLockMethod(
_lMsgDxGetUserMsg,
undefined,
[account, userId, messageId]
);
}

async function internalAddUserMessageSorted(account, userId, message)
{
return await _intLockMethod(
Expand Down
8 changes: 8 additions & 0 deletions client/lib/msg/localMsgInterfaces/localDexieInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ async function _lMsgDxGetUserMsgs(account, userId) // sorted
return messages;
}

async function _lMsgDxGetUserMsg(account, userId, messageId)
{
let temp = await db.messages.where({accountUserId: account["userId"], userId: userId, messageId: messageId}).toArray();
if (temp.length < 1)
return undefined;
return temp[0];
}

async function _lMsgDxAddMsg(account, userId, msg)
{
return await db.messages.add({accountUserId: account["userId"], userId: userId, messageId: msg["messageId"], message: msg});
Expand Down
13 changes: 10 additions & 3 deletions client/lib/other/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,19 @@ function logInfo(...arr)
function logWarn(...arr)
{
if (logWarnEnabled)
_logExtra("WARN", arr);
{
//_logExtra("WARN", arr);
console.warn(...arr);
console.trace();
}
}

function logError(...arr)
{
if (logErrorEnabled)
{
_logExtra("ERROR", arr);
//_logExtra("ERROR", arr);
console.error(...arr);
console.trace(arr);
}
}
Expand All @@ -80,7 +85,9 @@ function logFatalErrorAndCrash(...arr)
{
if (logErrorEnabled)
{
_logExtra("FATAL ERROR", arr);
//_logExtra("FATAL ERROR", arr);
console.error("FATAL ERROR");
console.error(...arr);
console.trace(arr);
}

Expand Down
17 changes: 16 additions & 1 deletion client/lib/user/userMsgSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ async function addMessageToUser(account, userIdFrom, chatUserId, message, date)

logInfo(`Adding User ${newUserId} to redirect list`);
}
else if (type == "delete-msg")
{
let deleteMsgId = message["data"]["messageId"];
let msg = await internalGetUserMessage(account, chatUserId, deleteMsgId);
if (msg == undefined)
return logWarn("Message not found:", deleteMsgId);

logInfo(`Trying to delete message ${deleteMsgId}:`, msg);
if (msg["message"]["from"] == userIdFrom) {
await internalRemoveUserMessage(account, chatUserId, deleteMsgId);
await messageDeletedUI(account, chatUserId, deleteMsgId);
}
else
logWarn("User not allowed to delete message:", msg);
}
else if (type == "call-start")
{
tryExtFn(extFnVcOnReceiveCallOffer, account, chatUserId, message["data"]);
Expand Down Expand Up @@ -360,7 +375,7 @@ async function addNormalMessageToUser(account, chatUserId, message)
{
logInfo(`New message from user ${chatUserId}:`, message);
if (isStrChannelFromGroup(chatUserId))
logWarn("Group messages not implemented yet");
;//logWarn("Group messages not implemented yet");
else
addUserIdIfNotExists(chatUserId);

Expand Down

0 comments on commit a3aef61

Please sign in to comment.