Skip to content

Commit

Permalink
notifications yes
Browse files Browse the repository at this point in the history
  • Loading branch information
marceldobehere committed Aug 18, 2024
1 parent 477d5ad commit e0759b3
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 56 deletions.
Binary file added client/assets/audio/not.wav
Binary file not shown.
5 changes: 5 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ <h3>Main Settings</h3>
<label>Auto Toggle Chat</label><input type="checkbox" id="settings-main-input-auto-show-chat" onclick="setSetting(['chat', 'auto-show-chat'], this.checked)"><br>
<label>Allow Embedding External Sources</label><input type="checkbox" id="settings-main-input-allow-external-sources-global" onclick="setSetting(['chat', 'allow-external-sources-global'], this.checked)"><br>
<label>Add Ping Reply</label><input type="checkbox" id="settings-main-input-add-ping-reply" onclick="setSetting(['chat', 'add-ping-reply'], this.checked)"><br>
<label>Allow Notifications</label><input type="checkbox" id="settings-allow-notifications" onclick="setSetting(['notification', 'allow-notifications'], this.checked)"><br>
<label>Allow Notification Sound</label><input type="checkbox" id="settings-allow-notification-sounds" onclick="setSetting(['notification', 'allow-sound'], this.checked)"><br>


<br>
<button onclick="exportProfile()">Export Profile</button>
<button onclick="importProfile()">Import Profile</button>
Expand Down Expand Up @@ -177,6 +181,7 @@ <h3>Info and Disclaimers</h3>
<script src="./js/groups.js" defer></script>
<script src="./js/hover.js" defer></script>
<script src="./js/settings.js" defer></script>
<script src="./js/notifications.js" defer></script>

<script src="./js/indexConn.js" defer></script>
<script src="./js/index.js" defer></script>
Expand Down
1 change: 1 addition & 0 deletions client/js/lists/channelList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let docLastChannelServerId = NoId;

async function createChannelEntry(channelId, channelName, serverId, shouldHighlight) {
let li = document.createElement("li");
li.id = `chat-selector-entry-${serverId}-${channelId}`;
let span = document.createElement("span");
span.className = "chat-selector-entry";
if (channelId != NoId)
Expand Down
62 changes: 57 additions & 5 deletions client/js/lists/chatList.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,14 @@ async function createChatList(serverId, channelId, scrollDown) {
async function channelClicked(element, channelId, serverId) {
console.log(`Channel ${channelId} clicked`);

try {
if (docLastChannelEntry)
docLastChannelEntry.classList.remove("chat-selector-entry-active");
element.classList.add("chat-selector-entry-active");
} catch (e) {
console.warn("ERROR ADDING CLASS: ", e);
}

if (docLastChannelEntry)
docLastChannelEntry.classList.remove("chat-selector-entry-active");
element.classList.add("chat-selector-entry-active");
docLastChannelEntry = element;
docLastChannelId = channelId;
await updateChatInfo(serverId, channelId);
Expand Down Expand Up @@ -332,48 +336,96 @@ async function messageEditedUI(account, chatUserId, messageId, newMessage)
}
}

async function openChat(serverId, channelId) {
logInfo(`> Opening chat ${serverId} - ${channelId}`);

docLastServerId = serverId;
docLastChannelId = channelId;
createServerList(docLastServerId);
await createChannelList(docLastServerId, docLastChannelId, true);
await createChatList(docLastServerId, docLastChannelId);

let channelIdStr = `chat-selector-entry-${serverId}-${channelId}`;
let element = document.getElementById(channelIdStr);
if (element)
element = element.getElementsByTagName("span")[0];
await channelClicked(element, channelId, serverId);
}

async function messageReceivedUI(account, chatUserId, message)
{
/* console.log("MESSAGE RECEIVED")
console.log(chatUserId);
console.log(message);*/


let shouldScroll = docChatUlDiv.scrollHeight - docChatUlDiv.scrollTop - docChatUlDiv.clientHeight < 140;
if (isStrChannelFromGroup(chatUserId))
{
let groupInfo = getGroupAndChannelFromChannelStr(chatUserId);
if (groupInfo == null)
return;

let username = userGetInfoDisplayUsername(currentUser['mainAccount'], message["from"]);
if (docChatLastServerId == groupInfo["groupId"] && docChatLastChannelId == groupInfo["channelId"])
{
await userMarkGroupMessagesAsRead(groupInfo["groupId"], groupInfo["channelId"]);

//await createChatList(groupInfo["groupId"], groupInfo["channelId"], true);
await refreshChatListArea(groupInfo["groupId"], groupInfo["channelId"]);

createChatEntry(userGetInfoDisplayUsername(currentUser['mainAccount'], message["from"]), message);
createChatEntry(username, message);
if (shouldScroll)
docChatUlDiv.scrollTop = docChatUlDiv.scrollHeight;
}

if (!windowVisible() ||
!(docChatLastServerId == groupInfo["groupId"] && docChatLastChannelId == groupInfo["channelId"]))
playNotificationSound();

{
if (hasGroupChatInfo(currentUser["mainAccount"], groupInfo["groupId"]))
{
let info = getGroupChatInfo(currentUser['mainAccount'], groupInfo["groupId"]);
let channel = info["channels"].find(x => x["id"] == groupInfo["channelId"]);

showNotification(`${username} in ${info["groupName"]} - ${channel["name"]}`, message["data"], () => {
openChat(groupInfo["groupId"], groupInfo["channelId"]);
});
}
else
logError("Group not found")
}




if (docLastServerId == groupInfo["groupId"])
await createChannelList(groupInfo["groupId"], docChatLastChannelId, true);
}
else
{
let username = userGetInfoDisplayUsername(currentUser['mainAccount'], message["from"]);
if (chatUserId == docChatLastChannelId && docChatLastServerId == DMsId)
{
await userMarkMessagesAsRead(chatUserId);

//await createChatList(DMsId, chatUserId, true);
await refreshChatListArea(DMsId, chatUserId);
createChatEntry(userGetInfoDisplayUsername(currentUser['mainAccount'], message["from"]), message);
createChatEntry(username, message);
if (shouldScroll)
docChatUlDiv.scrollTop = docChatUlDiv.scrollHeight;
}

if (!windowVisible() ||
!(chatUserId == docChatLastChannelId && docChatLastServerId == DMsId))
playNotificationSound();

showNotification(username, message["data"], () => {
openChat(DMsId, chatUserId);
});


if (docLastServerId == DMsId)// && (await userGetMessages(chatUserId)).length == 1)
await createChannelList(DMsId, docLastChannelId, true);
}
Expand Down
113 changes: 113 additions & 0 deletions client/js/notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
let notifications = [];
let notificationsWork = false;
async function checkNotifications()
{
notificationsWork = false;
//console.log(window.location.protocol);
if (window.location.protocol != "https:" && window.location.protocol != "http:"
&& window.location.protocol != "localhost:")
return;

if (!settingsObj["notification"]["allow-notifications"])
return;

if (!("Notification" in window))
return;

if (Notification.permission === "granted")
{
notificationsWork = true;
return;
}

if (Notification.permission !== "denied")
{
if (await Notification.requestPermission())
notificationsWork = true;
}
}

const windowHasFocus = function () {
if (document.hasFocus()) return true
let hasFocus = false

window.addEventListener('focus', function () {
hasFocus = true
})
window.focus()

return hasFocus
}

function clearNotifications()
{
if (!notificationsWork)
return;

//console.log(notifications)
for (let not of notifications) {
//console.log(`> Closing notification: ${not.body}`);
not.close();
}

notifications = [];
}

function windowVisible()
{
return (windowHasFocus() || document.visibilityState == "visible")
}
function canNotify()
{
if (!settingsObj["notification"]["allow-notifications"])
return false;
if (!notificationsWork)
return false;
if (windowVisible())
return false;

return true;
}

function showNotification(title, msg, callback)
{
if (!canNotify())
return;

//console.log(`> Showing notification: ${msg}`);

const notification = new Notification(title, {body: msg, silent: true});
notifications.push(notification);

notification.onclick = (ev) =>
{
clearNotifications();

if (typeof callback == "function")
callback(ev);
window.focus();
};
}

window.addEventListener('focus', () =>
{
clearNotifications();
});

window.addEventListener('visibilitychange', () =>
{
if (document.visibilityState == "visible")
clearNotifications();
});


let msgSound = new Audio("./assets/audio/not.wav");
function playNotificationSound()
{
if (!settingsObj["notification"]["allow-sound"])
return;

msgSound.play().then();
}

checkNotifications().then();
Loading

0 comments on commit e0759b3

Please sign in to comment.