added onclick for notifications

This commit is contained in:
PhilReact 2024-10-31 17:34:29 +02:00
parent b14cc3b8d3
commit b52abbc007

View File

@ -89,15 +89,8 @@ import {
} from "./background-cases"; } from "./background-cases";
import { getData, removeKeysAndLogout, storeData } from "./utils/chromeStorage"; import { getData, removeKeysAndLogout, storeData } from "./utils/chromeStorage";
// import {BackgroundFetch} from '@transistorsoft/capacitor-background-fetch'; // import {BackgroundFetch} from '@transistorsoft/capacitor-background-fetch';
// import { LocalNotifications } from '@capacitor/local-notifications';
// LocalNotifications.requestPermissions().then(permission => {
// if (permission.display === 'granted') {
// console.log("Notifications enabled");
// }
// });
export function cleanUrl(url) { export function cleanUrl(url) {
return url?.replace(/^(https?:\/\/)?(www\.)?/, ""); return url?.replace(/^(https?:\/\/)?(www\.)?/, "");
@ -121,6 +114,50 @@ const timeDifferenceForNotificationChatsBackground = 600000;
const requestQueueAnnouncements = new RequestQueueWithPromise(1); const requestQueueAnnouncements = new RequestQueueWithPromise(1);
let isMobile = true; let isMobile = true;
function handleNotificationClick(notificationId) {
// Determine the type of notification by parsing notificationId
const isDirect = notificationId.includes("_type=direct_");
const isGroup = notificationId.includes("_type=group_");
const isGroupAnnouncement = notificationId.includes(
"_type=group-announcement_"
);
const isNewThreadPost = notificationId.includes("_type=thread-post_");
// Handle specific notification types and post the message accordingly
if (isDirect) {
const fromValue = notificationId.split("_from=")[1];
window.postMessage(
{ action: "NOTIFICATION_OPEN_DIRECT", payload: { from: fromValue } },
"*"
);
} else if (isGroup) {
const fromValue = notificationId.split("_from=")[1];
window.postMessage(
{ action: "NOTIFICATION_OPEN_GROUP", payload: { from: fromValue } },
"*"
);
} else if (isGroupAnnouncement) {
const fromValue = notificationId.split("_from=")[1];
window.postMessage(
{
action: "NOTIFICATION_OPEN_ANNOUNCEMENT_GROUP",
payload: { from: fromValue },
},
"*"
);
} else if (isNewThreadPost) {
const dataValue = notificationId.split("_data=")[1];
const dataParsed = JSON.parse(dataValue);
window.postMessage(
{
action: "NOTIFICATION_OPEN_THREAD_NEW_POST",
payload: { data: dataParsed },
},
"*"
);
}
}
const isMobileDevice = () => { const isMobileDevice = () => {
const userAgent = navigator.userAgent || navigator.vendor || window.opera; const userAgent = navigator.userAgent || navigator.vendor || window.opera;
@ -180,12 +217,10 @@ export const getApiKeyFromStorage = async (): Promise<string | null> => {
return getData<string>("apiKey").catch(() => null); return getData<string>("apiKey").catch(() => null);
}; };
export const getCustomNodesFromStorage = async (): Promise<any | null> => { export const getCustomNodesFromStorage = async (): Promise<any | null> => {
return getData<any>("customNodes").catch(() => null); return getData<any>("customNodes").catch(() => null);
}; };
const getArbitraryEndpoint = async () => { const getArbitraryEndpoint = async () => {
const apiKey = await getApiKeyFromStorage(); // Retrieve apiKey asynchronously const apiKey = await getApiKeyFromStorage(); // Retrieve apiKey asynchronously
if (apiKey) { if (apiKey) {
@ -341,7 +376,6 @@ async function checkWebviewFocus() {
}); });
} }
function playNotificationSound() { function playNotificationSound() {
// chrome.runtime.sendMessage({ action: "PLAY_NOTIFICATION_SOUND" }); // chrome.runtime.sendMessage({ action: "PLAY_NOTIFICATION_SOUND" });
} }
@ -394,21 +428,30 @@ const handleNotificationDirect = async (directs) => {
(newestLatestTimestamp && (newestLatestTimestamp &&
newestLatestTimestamp?.timestamp > oldestLatestTimestamp?.timestamp) newestLatestTimestamp?.timestamp > oldestLatestTimestamp?.timestamp)
) { ) {
// Create the notification and assign the onclick handler
const title = `New Direct message! ${
newestLatestTimestamp?.name ? `from ${newestLatestTimestamp.name}` : ""
}`;
const body = "You have received a new direct message";
const notificationId =
"chat_notification_" +
Date.now() +
"_type=direct" +
`_from=${newestLatestTimestamp.address}`;
const notification = new window.Notification(title, {
body,
data: { id: notificationId },
});
// LocalNotifications.schedule({ // Set up the onclick event to call the handler function
// notifications: [ notification.onclick = () => {
// { handleNotificationClick(notificationId);
// title: `New Direct message! ${ notification.close();
// newestLatestTimestamp?.name && `from ${newestLatestTimestamp.name}` };
// }`,
// body: "You have received a new direct message", setTimeout(() => {
// id: notificationId, notification.close();
// schedule: { at: new Date(Date.now() + 1000) }, // 1 second from now }, 5000);
// }
// ]
// });
} }
} catch (error) { } catch (error) {
if (!isFocused) { if (!isFocused) {
@ -426,22 +469,34 @@ const handleNotificationDirect = async (directs) => {
); );
}); });
const notificationId = "chat_notification_" + Date.now(); // Create a unique notification ID with type and sender information
const notificationId =
// LocalNotifications.schedule({ "chat_notification_" +
// notifications: [ Date.now() +
// { "_type=direct" +
// title: `New Direct message!`, `_from=${newestLatestTimestamp.address}`;
// body: "You have received a new direct message",
// id: notificationId, const title = "New Direct message!";
// schedule: { at: new Date(Date.now() + 1000) }, // 1 second from now const body = "You have received a new direct message";
// }
// ] const notification = new window.Notification(title, {
// }); body,
data: { id: notificationId },
});
// Handle notification click with specific actions based on `notificationId`
notification.onclick = () => {
handleNotificationClick(notificationId);
notification.close(); // Clean up the notification on click
};
// Automatically close the notification after 5 seconds if not clicked
setTimeout(() => {
notification.close();
}, 5000); // Close after 5 seconds
} }
} finally { } finally {
setChatHeadsDirect(dataDirects); setChatHeadsDirect(dataDirects);
} }
}; };
async function getThreadActivity(): Promise<any | null> { async function getThreadActivity(): Promise<any | null> {
@ -452,7 +507,6 @@ async function getThreadActivity(): Promise<any | null> {
return getData<any>(key).catch(() => null); return getData<any>(key).catch(() => null);
} }
export function updateThreadActivity({ export function updateThreadActivity({
threadId, threadId,
qortalName, qortalName,
@ -483,7 +537,7 @@ export function updateThreadActivity({
lastResetTime: 0, lastResetTime: 0,
}; };
} else { } else {
threads = storedData threads = storedData;
} }
let lastResetTime = threads.lastResetTime || 0; let lastResetTime = threads.lastResetTime || 0;
@ -533,7 +587,6 @@ export function updateThreadActivity({
}); });
} }
const handleNotification = async (groups) => { const handleNotification = async (groups) => {
const wallet = await getSaveWallet(); const wallet = await getSaveWallet();
const address = wallet.address0; const address = wallet.address0;
@ -604,27 +657,32 @@ const handleNotification = async (groups) => {
) )
return; return;
// Create a unique notification ID with type and group information
const notificationId = const notificationId =
"chat_notification_" + "chat_notification_" +
Date.now() + Date.now() +
"_type=group" + "_type=group" +
`_from=${newestLatestTimestamp.groupId}`; `_from=${newestLatestTimestamp.groupId}`;
// LocalNotifications.schedule({ const title = "New Group Message!";
// notifications: [ const body = `You have received a new message from ${newestLatestTimestamp?.groupName}`;
// {
// title: "New Group Message!", const notification = new window.Notification(title, {
// body: `You have received a new message from ${newestLatestTimestamp?.groupName}`, body,
// id: notificationId, data: { id: notificationId },
// schedule: { at: new Date(Date.now() + 1000) }, // 1 second from now });
// }
// ] // Handle notification click with specific actions based on `notificationId`
// }); notification.onclick = () => {
if (!isMobile) { handleNotificationClick(notificationId);
setTimeout(() => { notification.close(); // Clean up the notification on click
chrome.notifications.clear(notificationId); };
}, 7000);
} // Automatically close the notification after 5 seconds if not clicked
setTimeout(() => {
notification.close();
}, 5000); // Close after 5 seconds
lastGroupNotification = Date.now(); lastGroupNotification = Date.now();
} }
} }
@ -644,35 +702,37 @@ const handleNotification = async (groups) => {
); );
}); });
// Generate a unique notification ID
const notificationId = "chat_notification_" + Date.now(); const notificationId = "chat_notification_" + Date.now();
// LocalNotifications.schedule({
// notifications: [
// {
// title: "New Group Message!",
// body: "You have received a new message from one of your groups",
// id: notificationId,
// schedule: { at: new Date(Date.now() + 1000) }, // 1 second from now
// }
// ]
// });
lastGroupNotification = Date.now();
const title = "New Group Message!";
const body = "You have received a new message from one of your groups";
// Create and show the notification immediately
const notification = new window.Notification(title, {
body,
data: { id: notificationId },
});
// Handle notification click, allowing specific actions based on `notificationId`
notification.onclick = () => {
handleNotificationClick(notificationId);
notification.close(); // Clean up the notification on click
};
// Automatically close the notification after 5 seconds if its not clicked
setTimeout(() => {
notification.close();
}, 5000); // Close after 5 seconds
lastGroupNotification = Date.now();
} }
} finally { } finally {
if (!data || data?.length === 0) return; if (!data || data?.length === 0) return;
setChatHeads(dataWithUpdates); setChatHeads(dataWithUpdates);
} }
}; };
const forceCloseWebSocket = () => { const forceCloseWebSocket = () => {
if (socket) { if (socket) {
clearTimeout(timeoutId); clearTimeout(timeoutId);
@ -730,7 +790,6 @@ export async function getSaveWallet() {
} }
} }
export async function clearAllNotifications() { export async function clearAllNotifications() {
// const notifications = await chrome.notifications.getAll(); // const notifications = await chrome.notifications.getAll();
// for (const notificationId of Object.keys(notifications)) { // for (const notificationId of Object.keys(notifications)) {
@ -755,7 +814,6 @@ async function connection(hostname: string) {
return isConnected; return isConnected;
} }
async function getTradeInfo(qortalAtAddress) { async function getTradeInfo(qortalAtAddress) {
const response = await fetch( const response = await fetch(
buyTradeNodeBaseUrl + "/crosschain/trade/" + qortalAtAddress buyTradeNodeBaseUrl + "/crosschain/trade/" + qortalAtAddress
@ -786,7 +844,7 @@ export async function getLTCBalance() {
const wallet = await getSaveWallet(); const wallet = await getSaveWallet();
let _url = `${buyTradeNodeBaseUrl}/crosschain/ltc/walletbalance`; let _url = `${buyTradeNodeBaseUrl}/crosschain/ltc/walletbalance`;
const keyPair = await getKeyPair(); const keyPair = await getKeyPair();
const parsedKeyPair = keyPair const parsedKeyPair = keyPair;
let _body = parsedKeyPair.ltcPublicKey; let _body = parsedKeyPair.ltcPublicKey;
const response = await fetch(_url, { const response = await fetch(_url, {
method: "POST", method: "POST",
@ -977,18 +1035,17 @@ export async function getDataPublishes(groupId, type) {
return new Promise((resolve) => { return new Promise((resolve) => {
getData<any>(`${address}-publishData`) getData<any>(`${address}-publishData`)
.then((storedData) => { .then((storedData) => {
storedData = storedData || {}; // Initialize an empty object if no data storedData = storedData || {}; // Initialize an empty object if no data
const groupData = storedData[groupId] || {}; // Get data by groupId const groupData = storedData[groupId] || {}; // Get data by groupId
const typeData = groupData[type] || {}; // Get data by type const typeData = groupData[type] || {}; // Get data by type
resolve(typeData); // Resolve with the data inside the specific type
})
.catch((error) => {
console.error("Error retrieving data:", error);
resolve(null); // Return null in case of an error
});
resolve(typeData); // Resolve with the data inside the specific type
})
.catch((error) => {
console.error("Error retrieving data:", error);
resolve(null); // Return null in case of an error
});
}); });
} }
@ -1002,58 +1059,60 @@ export async function addDataPublishes(newData, groupId, type) {
return new Promise((res) => { return new Promise((res) => {
getData<any>(`${address}-publishData`) getData<any>(`${address}-publishData`)
.then((storedData) => { .then((storedData) => {
storedData = storedData || {}; // Initialize if no data found storedData = storedData || {}; // Initialize if no data found
let groupData = storedData[groupId] || {}; // Initialize group data if not found let groupData = storedData[groupId] || {}; // Initialize group data if not found
let typeData = groupData[type] || {}; // Initialize type data if not found let typeData = groupData[type] || {}; // Initialize type data if not found
let totalSize = 0; let totalSize = 0;
// Calculate total size of all stored data // Calculate total size of all stored data
Object.values(storedData).forEach((group) => { Object.values(storedData).forEach((group) => {
Object.values(group).forEach((type) => { Object.values(group).forEach((type) => {
Object.values(type).forEach((data) => { Object.values(type).forEach((data) => {
totalSize += data.size; // Accumulate data sizes totalSize += data.size; // Accumulate data sizes
});
}); });
}); });
});
// Check if adding new data exceeds 3MB limit
// Check if adding new data exceeds 3MB limit if (totalSize + newData.size > MAX_STORAGE_SIZE) {
if (totalSize + newData.size > MAX_STORAGE_SIZE) { let dataEntries = Object.entries(typeData);
let dataEntries = Object.entries(typeData); dataEntries.sort((a, b) => a[1].timestampSaved - b[1].timestampSaved);
dataEntries.sort((a, b) => a[1].timestampSaved - b[1].timestampSaved);
// Remove oldest entries until there's enough space
// Remove oldest entries until there's enough space while (
while (totalSize + newData.size > MAX_STORAGE_SIZE && dataEntries.length > 0) { totalSize + newData.size > MAX_STORAGE_SIZE &&
const removedEntry = dataEntries.shift(); dataEntries.length > 0
totalSize -= removedEntry[1].size; ) {
delete typeData[removedEntry[0]]; // Remove from typeData const removedEntry = dataEntries.shift();
totalSize -= removedEntry[1].size;
delete typeData[removedEntry[0]]; // Remove from typeData
}
} }
}
// Add or update the new data if there's space
// Add or update the new data if there's space if (totalSize + newData.size <= MAX_STORAGE_SIZE) {
if (totalSize + newData.size <= MAX_STORAGE_SIZE) { typeData[`${nameIdentifier}`] = newData;
typeData[`${nameIdentifier}`] = newData; groupData[type] = typeData;
groupData[type] = typeData; storedData[groupId] = groupData;
storedData[groupId] = groupData;
// Save updated structure back to localStorage
// Save updated structure back to localStorage storeData(`${address}-publishData`, storedData)
storeData(`${address}-publishData`, storedData) .then(() => res(true)) // Successfully added
.then(() => res(true)) // Successfully added .catch((error) => {
.catch((error) => { console.error("Error saving data:", error);
console.error("Error saving data:", error); res(false); // Save failed
res(false); // Save failed });
}); } else {
} else { console.error("Failed to add data, still exceeds storage limit.");
console.error("Failed to add data, still exceeds storage limit."); res(false); // Failure due to storage limit
res(false); // Failure due to storage limit }
} })
}) .catch((error) => {
.catch((error) => { console.error("Error retrieving data:", error);
console.error("Error retrieving data:", error); res(false); // Failure due to retrieval error
res(false); // Failure due to retrieval error });
});
}); });
} }
@ -1064,16 +1123,15 @@ export async function getUserSettings({ key }) {
return new Promise((resolve) => { return new Promise((resolve) => {
getData<any>(`${address}-userSettings`) getData<any>(`${address}-userSettings`)
.then((storedData) => { .then((storedData) => {
storedData = storedData || {}; // Initialize empty object if no data storedData = storedData || {}; // Initialize empty object if no data
const value = storedData[key] || null; // Get data by key const value = storedData[key] || null; // Get data by key
resolve(value); // Resolve with the data for the specific key
})
.catch((error) => {
resolve(null); // Return null in case of an error
});
resolve(value); // Resolve with the data for the specific key
})
.catch((error) => {
resolve(null); // Return null in case of an error
});
}); });
} }
@ -1087,24 +1145,23 @@ export async function addUserSettings({ keyValue }) {
return new Promise((res) => { return new Promise((res) => {
getData<any>(`${address}-userSettings`) getData<any>(`${address}-userSettings`)
.then((storedData) => { .then((storedData) => {
storedData = storedData || {}; // Initialize if no data found storedData = storedData || {}; // Initialize if no data found
storedData[key] = value; // Update the key-value pair within stored data storedData[key] = value; // Update the key-value pair within stored data
// Save updated structure back to localStorage // Save updated structure back to localStorage
storeData(`${address}-userSettings`, storedData) storeData(`${address}-userSettings`, storedData)
.then(() => res(true)) // Data successfully added .then(() => res(true)) // Data successfully added
.catch((error) => { .catch((error) => {
console.error("Error saving data:", error); console.error("Error saving data:", error);
res(false); // Save failed res(false); // Save failed
}); });
}) })
.catch((error) => { .catch((error) => {
console.error("Error retrieving data:", error); console.error("Error retrieving data:", error);
res(false); // Failure due to retrieval error res(false); // Failure due to retrieval error
}); });
}); });
} }
@ -1143,11 +1200,10 @@ export async function decryptWallet({ password, wallet, walletVersion }) {
}; };
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
storeData("keyPair", toSave) storeData("keyPair", toSave)
.then(() => resolve(true)) .then(() => resolve(true))
.catch((error) => { .catch((error) => {
reject(new Error(error.message || "Error saving data")); reject(new Error(error.message || "Error saving data"));
}); });
}); });
const newWallet = { const newWallet = {
...wallet, ...wallet,
@ -1156,11 +1212,10 @@ export async function decryptWallet({ password, wallet, walletVersion }) {
}; };
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
storeData("walletInfo", newWallet) storeData("walletInfo", newWallet)
.then(() => resolve(true)) .then(() => resolve(true))
.catch((error) => { .catch((error) => {
reject(new Error(error.message || "Error saving data")); reject(new Error(error.message || "Error saving data"));
}); });
}); });
return true; return true;
@ -1249,22 +1304,27 @@ export const computePow = async ({ chatBytes, path, difficulty }) => {
const getStoredData = async (key) => { const getStoredData = async (key) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
getData<any>(key) getData<any>(key)
.then((data) => resolve(data)) .then((data) => resolve(data))
.catch((error) => reject(error)); .catch((error) => reject(error));
}); });
}; };
export async function handleActiveGroupDataFromSocket({ groups, directs }) { export async function handleActiveGroupDataFromSocket({ groups, directs }) {
try { try {
window.postMessage({ window.postMessage(
action: "SET_GROUPS", {
payload: groups, action: "SET_GROUPS",
}, "*"); payload: groups,
window.postMessage({ },
action: "SET_DIRECTS", "*"
payload: directs, );
}, "*"); window.postMessage(
{
action: "SET_DIRECTS",
payload: directs,
},
"*"
);
groups = groups; groups = groups;
directs = directs; directs = directs;
@ -1274,11 +1334,10 @@ export async function handleActiveGroupDataFromSocket({ groups, directs }) {
}; };
// Save the active data to localStorage // Save the active data to localStorage
storeData("active-groups-directs", activeData) storeData("active-groups-directs", activeData).catch((error) => {
.catch((error) => {
console.error("Error saving data:", error); console.error("Error saving data:", error);
}); });
try { try {
handleNotification(groups); handleNotification(groups);
handleNotificationDirect(directs); handleNotificationDirect(directs);
@ -2311,7 +2370,6 @@ export function removeDuplicateWindow(popupUrl) {
// w.tabs && // w.tabs &&
// w.tabs.some((tab) => tab.url && tab.url.startsWith(popupUrl)) // w.tabs.some((tab) => tab.url && tab.url.startsWith(popupUrl))
// ); // );
// if (existingPopupsPending.length > 1) { // if (existingPopupsPending.length > 1) {
// chrome.windows.remove( // chrome.windows.remove(
// existingPopupsPending?.[0]?.tabs?.[0]?.windowId, // existingPopupsPending?.[0]?.tabs?.[0]?.windowId,
@ -2335,11 +2393,10 @@ export async function setChatHeads(data) {
const address = wallet.address0; const address = wallet.address0;
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
storeData(`chatheads-${address}`, data) storeData(`chatheads-${address}`, data)
.then(() => resolve(true)) .then(() => resolve(true))
.catch((error) => { .catch((error) => {
reject(new Error(error.message || "Error saving data")); reject(new Error(error.message || "Error saving data"));
}); });
}); });
} }
@ -2357,7 +2414,7 @@ export async function getTempPublish() {
const SIX_MINUTES = 6 * 60 * 1000; // 6 minutes in milliseconds const SIX_MINUTES = 6 * 60 * 1000; // 6 minutes in milliseconds
if (res) { if (res) {
const parsedData = res const parsedData = res;
const currentTime = Date.now(); const currentTime = Date.now();
// Filter through each top-level key (e.g., "announcement") and then through its nested entries // Filter through each top-level key (e.g., "announcement") and then through its nested entries
@ -2397,14 +2454,12 @@ export async function saveTempPublish({ data, key }) {
}, },
}; };
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
storeData(`tempPublish-${address}`, newTemp) storeData(`tempPublish-${address}`, newTemp)
.then(() => resolve(newTemp[key])) .then(() => resolve(newTemp[key]))
.catch((error) => { .catch((error) => {
reject(new Error(error.message || "Error saving data")); reject(new Error(error.message || "Error saving data"));
}); });
}); });
} }
@ -2413,11 +2468,10 @@ async function setChatHeadsDirect(data) {
const address = wallet.address0; const address = wallet.address0;
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
storeData(`chatheads-direct-${address}`, data) storeData(`chatheads-direct-${address}`, data)
.then(() => resolve(true)) .then(() => resolve(true))
.catch((error) => { .catch((error) => {
reject(new Error(error.message || "Error saving data")); reject(new Error(error.message || "Error saving data"));
}); });
}); });
} }
@ -2427,7 +2481,7 @@ export async function getTimestampEnterChat() {
const key = `enter-chat-timestamp-${address}`; const key = `enter-chat-timestamp-${address}`;
const res = await getData<any>(key).catch(() => null); const res = await getData<any>(key).catch(() => null);
if (res) { if (res) {
const parsedData = res const parsedData = res;
return parsedData; return parsedData;
} else { } else {
return {}; return {};
@ -2439,7 +2493,7 @@ export async function getTimestampGroupAnnouncement() {
const key = `group-announcement-${address}`; const key = `group-announcement-${address}`;
const res = await getData<any>(key).catch(() => null); const res = await getData<any>(key).catch(() => null);
if (res) { if (res) {
const parsedData = res const parsedData = res;
return parsedData; return parsedData;
} else { } else {
return {}; return {};
@ -2460,11 +2514,10 @@ export async function addTimestampGroupAnnouncement({
}; };
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
storeData(`group-announcement-${address}`, data) storeData(`group-announcement-${address}`, data)
.then(() => resolve(true)) .then(() => resolve(true))
.catch((error) => { .catch((error) => {
reject(new Error(error.message || "Error saving data")); reject(new Error(error.message || "Error saving data"));
}); });
}); });
} }
@ -2474,7 +2527,7 @@ async function getGroupData() {
const key = `group-data-${address}`; const key = `group-data-${address}`;
const res = await getData<any>(key).catch(() => null); const res = await getData<any>(key).catch(() => null);
if (res) { if (res) {
const parsedData = res const parsedData = res;
return parsedData; return parsedData;
} else { } else {
return {}; return {};
@ -2486,7 +2539,7 @@ export async function getGroupDataSingle(groupId) {
const key = `group-data-${address}`; const key = `group-data-${address}`;
const res = await getData<any>(key).catch(() => null); const res = await getData<any>(key).catch(() => null);
if (res) { if (res) {
const parsedData = res const parsedData = res;
return parsedData[groupId] || null; return parsedData[groupId] || null;
} else { } else {
return null; return null;
@ -2510,11 +2563,10 @@ export async function setGroupData({
}; };
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
storeData(`group-data-${address}`, data) storeData(`group-data-${address}`, data)
.then(() => resolve(true)) .then(() => resolve(true))
.catch((error) => { .catch((error) => {
reject(new Error(error.message || "Error saving data")); reject(new Error(error.message || "Error saving data"));
}); });
}); });
} }
@ -2525,11 +2577,10 @@ export async function addTimestampEnterChat({ groupId, timestamp }) {
data[groupId] = timestamp; data[groupId] = timestamp;
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
storeData(`enter-chat-timestamp-${address}`, data) storeData(`enter-chat-timestamp-${address}`, data)
.then(() => resolve(true)) .then(() => resolve(true))
.catch((error) => { .catch((error) => {
reject(new Error(error.message || "Error saving data")); reject(new Error(error.message || "Error saving data"));
}); });
}); });
} }
@ -2556,7 +2607,7 @@ async function getChatHeads() {
const key = `chatheads-${address}`; const key = `chatheads-${address}`;
const res = await getData<any>(key).catch(() => null); const res = await getData<any>(key).catch(() => null);
if (res) { if (res) {
const parsedData = res const parsedData = res;
return parsedData; return parsedData;
} else { } else {
throw new Error("No Chatheads saved"); throw new Error("No Chatheads saved");
@ -2569,7 +2620,7 @@ async function getChatHeadsDirect() {
const key = `chatheads-direct-${address}`; const key = `chatheads-direct-${address}`;
const res = await getData<any>(key).catch(() => null); const res = await getData<any>(key).catch(() => null);
if (res) { if (res) {
const parsedData = res const parsedData = res;
return parsedData; return parsedData;
} else { } else {
throw new Error("No Chatheads saved"); throw new Error("No Chatheads saved");
@ -2579,14 +2630,13 @@ async function getChatHeadsDirect() {
function setupMessageListener() { function setupMessageListener() {
window.addEventListener("message", async (event) => { window.addEventListener("message", async (event) => {
if (event.origin !== window.location.origin) { if (event.origin !== window.location.origin) {
return; return;
} }
const request = event.data; const request = event.data;
// Check if the message is intended for this listener // Check if the message is intended for this listener
if (request?.type !== "backgroundMessage") return; // Only process messages of type 'backgroundMessage' if (request?.type !== "backgroundMessage") return; // Only process messages of type 'backgroundMessage'
switch (request.action) { switch (request.action) {
case "version": case "version":
versionCase(request, event); versionCase(request, event);
@ -2786,9 +2836,8 @@ function setupMessageListener() {
key2, key2,
key3, key3,
]; ];
removeKeysAndLogout(keysToRemove, event, request); removeKeysAndLogout(keysToRemove, event, request);
}; };
logoutFunc(); logoutFunc();
} catch (error) {} } catch (error) {}
@ -2804,10 +2853,6 @@ function setupMessageListener() {
setupMessageListener(); setupMessageListener();
const checkGroupList = async () => { const checkGroupList = async () => {
try { try {
const wallet = await getSaveWallet(); const wallet = await getSaveWallet();
@ -2844,11 +2889,10 @@ const checkGroupList = async () => {
} }
}; };
export const checkNewMessages = async () => { export const checkNewMessages = async () => {
try { try {
let mutedGroups = await getUserSettings({key: 'mutedGroups'}) || [] let mutedGroups = (await getUserSettings({ key: "mutedGroups" })) || [];
if(!isArray(mutedGroups)) mutedGroups = [] if (!isArray(mutedGroups)) mutedGroups = [];
let myName = ""; let myName = "";
const userData = await getUserInfo(); const userData = await getUserInfo();
if (userData?.name) { if (userData?.name) {
@ -2907,32 +2951,49 @@ export const checkNewMessages = async () => {
} }
}) })
); );
let isDisableNotifications = await getUserSettings({key: 'disable-push-notifications'}) || false let isDisableNotifications =
(await getUserSettings({ key: "disable-push-notifications" })) || false;
if (newAnnouncements.length > 0 && !mutedGroups.includes(newAnnouncements[0]?.groupId) && !isDisableNotifications) { if (
newAnnouncements.length > 0 &&
!mutedGroups.includes(newAnnouncements[0]?.groupId) &&
!isDisableNotifications
) {
// Create a unique notification ID with type and group announcement details
const notificationId = const notificationId =
"chat_notification_" + "chat_notification_" +
Date.now() + Date.now() +
"_type=group-announcement" + "_type=group-announcement" +
`_from=${newAnnouncements[0]?.groupId}`; `_from=${newAnnouncements[0]?.groupId}`;
const title = "New group announcement!";
// LocalNotifications.schedule({ const body = `You have received a new announcement from ${newAnnouncements[0]?.groupName}`;
// notifications: [
// { // Create and show the notification
// title: "New group announcement!", const notification = new window.Notification(title, {
// body: `You have received a new announcement from ${newAnnouncements[0]?.groupName}`, body,
// id: notificationId, data: { id: notificationId },
// schedule: { at: new Date(Date.now() + 1000) }, // 1 second from now });
// }
// ] // Handle notification click with specific actions based on `notificationId`
// }); notification.onclick = () => {
handleNotificationClick(notificationId);
notification.close(); // Clean up the notification on click
};
// Automatically close the notification after 5 seconds if its not clicked
setTimeout(() => {
notification.close();
}, 5000); // Close after 5 seconds
} }
const savedtimestampAfter = await getTimestampGroupAnnouncement(); const savedtimestampAfter = await getTimestampGroupAnnouncement();
window.postMessage({ window.postMessage(
action: "SET_GROUP_ANNOUNCEMENTS", {
payload: savedtimestampAfter, action: "SET_GROUP_ANNOUNCEMENTS",
}, "*"); payload: savedtimestampAfter,
},
"*"
);
} catch (error) { } catch (error) {
} finally { } finally {
} }
@ -2940,11 +3001,7 @@ export const checkNewMessages = async () => {
const checkActiveChatsForNotifications = async () => { const checkActiveChatsForNotifications = async () => {
try { try {
checkGroupList();
checkGroupList();
} catch (error) {} } catch (error) {}
}; };
@ -3060,27 +3117,47 @@ export const checkThreads = async (bringBack) => {
Date.now() + Date.now() +
"_type=thread-post" + "_type=thread-post" +
`_data=${JSON.stringify(newAnnouncements[0])}`; `_data=${JSON.stringify(newAnnouncements[0])}`;
let isDisableNotifications = await getUserSettings({key: 'disable-push-notifications'}) || false let isDisableNotifications =
if(!isDisableNotifications){ (await getUserSettings({ key: "disable-push-notifications" })) || false;
if (!isDisableNotifications) {
// LocalNotifications.schedule({
// notifications: [
// { // Check user settings to see if notifications are disabled
// title: `New thread post!`, const isDisableNotifications =
// body: `New post in ${newAnnouncements[0]?.thread?.threadData?.title}`, (await getUserSettings({ key: "disable-push-notifications" })) ||
// id: notificationId, false;
// schedule: { at: new Date(Date.now() + 1000) }, // 1 second from now
// } if (!isDisableNotifications) {
// ] const title = "New thread post!";
// }); const body = `New post in ${newAnnouncements[0]?.thread?.threadData?.title}`;
// Create and show the notification
const notification = new window.Notification(title, {
body,
data: { id: notificationId },
});
// Handle notification click with specific actions based on `notificationId`
notification.onclick = () => {
handleNotificationClick(notificationId);
notification.close(); // Clean up the notification on click
};
// Automatically close the notification after 5 seconds if its not clicked
setTimeout(() => {
notification.close();
}, 5000); // Close after 5 seconds
}
} }
} }
const savedtimestampAfter = await getTimestampGroupAnnouncement(); const savedtimestampAfter = await getTimestampGroupAnnouncement();
window.postMessage({ window.postMessage(
action: "SET_GROUP_ANNOUNCEMENTS", {
payload: savedtimestampAfter, action: "SET_GROUP_ANNOUNCEMENTS",
}, "*"); payload: savedtimestampAfter,
},
"*"
);
} catch (error) { } catch (error) {
} finally { } finally {
} }
@ -3110,33 +3187,3 @@ export const checkThreads = async (bringBack) => {
// // Optional timeout callback // // Optional timeout callback
// BackgroundFetch.finish(taskId); // BackgroundFetch.finish(taskId);
// }); // });
// LocalNotifications.addListener('localNotificationActionPerformed', async (notification) => {
// const notificationId = notification.notification.id;
// // Check the type of notification by parsing notificationId
// const isDirect = notificationId.includes('_type=direct_');
// const isGroup = notificationId.includes('_type=group_');
// const isGroupAnnouncement = notificationId.includes('_type=group-announcement_');
// const isNewThreadPost = notificationId.includes('_type=thread-post_');
// // Handle specific notification types
// if (isDirect) {
// const fromValue = notificationId.split('_from=')[1];
// handleDirectNotification(fromValue);
// } else if (isGroup) {
// const fromValue = notificationId.split('_from=')[1];
// handleGroupNotification(fromValue);
// } else if (isGroupAnnouncement) {
// const fromValue = notificationId.split('_from=')[1];
// handleAnnouncementNotification(fromValue);
// } else if (isNewThreadPost) {
// const dataValue = notificationId.split('_data=')[1];
// const dataParsed = JSON.parse(dataValue);
// handleThreadPostNotification(dataParsed);
// }
// });