From c9055e4b68d0cb4b2a5ac6b90b0f1228512bff55 Mon Sep 17 00:00:00 2001 From: PhilReact Date: Mon, 28 Oct 2024 13:24:08 +0200 Subject: [PATCH] cases --- src/App.tsx | 14 ++-- src/background-cases.ts | 139 +++++++++++++++++++++++++++++++++ src/background.ts | 39 +++++++-- src/components/Group/Group.tsx | 125 +++++++++++++---------------- 4 files changed, 234 insertions(+), 83 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 6880ca4..3056135 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1201,10 +1201,11 @@ function App() { const handleFocus = () => { setIsFocused(true); if (isMobile) { - chrome?.runtime?.sendMessage({ - action: "clearAllNotifications", - payload: {}, + window.sendMessage("clearAllNotifications", {}) + .catch((error) => { + console.error("Failed to clear notifications:", error.message || "An error occurred"); }); + } }; @@ -1223,10 +1224,11 @@ function App() { if (document.visibilityState === "visible") { setIsFocused(true); if (isMobile) { - chrome?.runtime?.sendMessage({ - action: "clearAllNotifications", - payload: {}, + window.sendMessage("clearAllNotifications", {}) + .catch((error) => { + console.error("Failed to clear notifications:", error.message || "An error occurred"); }); + } } else { setIsFocused(false); diff --git a/src/background-cases.ts b/src/background-cases.ts index abcb7e9..06c199a 100644 --- a/src/background-cases.ts +++ b/src/background-cases.ts @@ -1,10 +1,12 @@ import { addDataPublishes, addTimestampEnterChat, + addTimestampGroupAnnouncement, addUserSettings, banFromGroup, cancelBan, cancelInvitationToGroup, + clearAllNotifications, createGroup, decryptWallet, findUsableApi, @@ -12,6 +14,7 @@ import { getBalanceInfo, getCustomNodesFromStorage, getDataPublishes, + getGroupDataSingle, getKeyPair, getLTCBalance, getNameInfo, @@ -23,11 +26,13 @@ import { kickFromGroup, leaveGroup, makeAdmin, + notifyAdminRegenerateSecretKey, registerName, removeAdmin, saveTempPublish, sendCoin, setChatHeads, + setGroupData, walletVersion, } from "./background"; @@ -898,4 +903,138 @@ export async function balanceCase(request, event) { event.origin ); } + } + + export async function notifyAdminRegenerateSecretKeyCase(request, event) { + try { + const { groupName, adminAddress } = request.payload; + const response = await notifyAdminRegenerateSecretKey({groupName, adminAddress}); + + event.source.postMessage( + { + requestId: request.requestId, + action: "notifyAdminRegenerateSecretKey", + payload: response, + type: "backgroundMessageResponse", + }, + event.origin + ); + } catch (error) { + event.source.postMessage( + { + requestId: request.requestId, + action: "notifyAdminRegenerateSecretKey", + error: error?.message, + type: "backgroundMessageResponse", + }, + event.origin + ); + } + } + + export async function addGroupNotificationTimestampCase(request, event) { + try { + const { groupId, timestamp } = request.payload; + const response = await addTimestampGroupAnnouncement({groupId, timestamp}); + + event.source.postMessage( + { + requestId: request.requestId, + action: "addGroupNotificationTimestamp", + payload: response, + type: "backgroundMessageResponse", + }, + event.origin + ); + } catch (error) { + event.source.postMessage( + { + requestId: request.requestId, + action: "addGroupNotificationTimestamp", + error: error?.message, + type: "backgroundMessageResponse", + }, + event.origin + ); + } + } + + export async function clearAllNotificationsCase(request, event) { + try { + await clearAllNotifications(); + + event.source.postMessage( + { + requestId: request.requestId, + action: "clearAllNotifications", + payload: true, + type: "backgroundMessageResponse", + }, + event.origin + ); + } catch (error) { + event.source.postMessage( + { + requestId: request.requestId, + action: "clearAllNotifications", + error: error?.message, + type: "backgroundMessageResponse", + }, + event.origin + ); + } + } + + export async function setGroupDataCase(request, event) { + try { + const { groupId, secretKeyData, secretKeyResource, admins } = request.payload; + const response = await setGroupData({groupId, secretKeyData, secretKeyResource, admins}); + + event.source.postMessage( + { + requestId: request.requestId, + action: "setGroupData", + payload: response, + type: "backgroundMessageResponse", + }, + event.origin + ); + } catch (error) { + event.source.postMessage( + { + requestId: request.requestId, + action: "setGroupData", + error: error?.message, + type: "backgroundMessageResponse", + }, + event.origin + ); + } + } + + export async function getGroupDataSingleCase(request, event) { + try { + const { groupId } = request.payload; + const response = await getGroupDataSingle({groupId}); + + event.source.postMessage( + { + requestId: request.requestId, + action: "getGroupDataSingle", + payload: response, + type: "backgroundMessageResponse", + }, + event.origin + ); + } catch (error) { + event.source.postMessage( + { + requestId: request.requestId, + action: "getGroupDataSingle", + error: error?.message, + type: "backgroundMessageResponse", + }, + event.origin + ); + } } \ No newline at end of file diff --git a/src/background.ts b/src/background.ts index 2cb3575..6f99f1f 100644 --- a/src/background.ts +++ b/src/background.ts @@ -30,17 +30,20 @@ import { TradeBotRespondMultipleRequest } from "./transactions/TradeBotRespondMu import { RESOURCE_TYPE_NUMBER_GROUP_CHAT_REACTIONS } from "./constants/resourceTypes"; import { addDataPublishesCase, + addGroupNotificationTimestampCase, addTimestampEnterChatCase, addUserSettingsCase, balanceCase, banFromGroupCase, cancelBanCase, cancelInvitationToGroupCase, + clearAllNotificationsCase, createGroupCase, decryptWalletCase, getApiKeyCase, getCustomNodesFromStorageCase, getDataPublishesCase, + getGroupDataSingleCase, getTempPublishCase, getUserSettingsCase, getWalletInfoCase, @@ -52,6 +55,7 @@ import { makeAdminCase, nameCase, notificationCase, + notifyAdminRegenerateSecretKeyCase, registerNameCase, removeAdminCase, saveTempPublishCase, @@ -59,6 +63,7 @@ import { setApiKeyCase, setChatHeadsCase, setCustomNodesCase, + setGroupDataCase, userInfoCase, validApiCase, versionCase, @@ -1038,7 +1043,7 @@ export async function getSaveWallet() { } } -async function clearAllNotifications() { +export async function clearAllNotifications() { const notifications = await chrome.notifications.getAll(); for (const notificationId of Object.keys(notifications)) { await chrome.notifications.clear(notificationId); @@ -2754,7 +2759,7 @@ async function getTimestampGroupAnnouncement() { } } -async function addTimestampGroupAnnouncement({ +export async function addTimestampGroupAnnouncement({ groupId, timestamp, seenTimestamp, @@ -2793,7 +2798,7 @@ async function getGroupData() { return {}; } } -async function getGroupDataSingle(groupId) { +export async function getGroupDataSingle(groupId) { const wallet = await getSaveWallet(); const address = wallet.address0; const key = `group-data-${address}`; @@ -2806,7 +2811,7 @@ async function getGroupDataSingle(groupId) { } } -async function setGroupData({ +export async function setGroupData({ groupId, secretKeyData, secretKeyResource, @@ -2853,7 +2858,10 @@ export async function addTimestampEnterChat({ groupId, timestamp }) { }); } -async function notifyAdminRegenerateSecretKey({ groupName, adminAddress }) { +export async function notifyAdminRegenerateSecretKey({ + groupName, + adminAddress, +}) { const wallet = await getSaveWallet(); const address = wallet.address0; const name = await getNameInfo(address); @@ -3004,9 +3012,24 @@ function setupMessageListener() { case "getApiKey": getApiKeyCase(request, event); break; - case "getCustomNodesFromStorage": - getCustomNodesFromStorageCase(request, event); - break; + case "getCustomNodesFromStorage": + getCustomNodesFromStorageCase(request, event); + break; + case "notifyAdminRegenerateSecretKey": + notifyAdminRegenerateSecretKeyCase(request, event); + break; + case "addGroupNotificationTimestamp": + addGroupNotificationTimestampCase(request, event); + break; + case "clearAllNotifications": + clearAllNotificationsCase(request, event); + break; + case "setGroupData": + setGroupDataCase(request, event); + break; + case "getGroupDataSingle": + getGroupDataSingleCase(request, event); + break; default: console.error("Unknown action:", request.action); } diff --git a/src/components/Group/Group.tsx b/src/components/Group/Group.tsx index 6abd1d8..e1341b3 100644 --- a/src/components/Group/Group.tsx +++ b/src/components/Group/Group.tsx @@ -521,21 +521,20 @@ export const Group = ({ const getGroupDataSingle = async (groupId) => { try { return new Promise((res, rej) => { - chrome?.runtime?.sendMessage( - { - action: "getGroupDataSingle", - payload: { - groupId, - }, - }, - (response) => { + window.sendMessage("getGroupDataSingle", { + groupId, + }) + .then((response) => { if (!response?.error) { res(response); return; } rej(response.error); - } - ); + }) + .catch((error) => { + rej(error.message || "An error occurred"); + }); + }); } catch (error) { return {}; @@ -797,15 +796,15 @@ export const Group = ({ setSecretKey(decryptedKeyToObject); lastFetchedSecretKey.current = Date.now(); setMemberCountFromSecretKeyData(decryptedKey.count); - chrome?.runtime?.sendMessage({ - action: "setGroupData", - payload: { - groupId: selectedGroup?.groupId, - secretKeyData: data, - secretKeyResource: publish, - admins: { names, addresses, both }, - }, - }); + window.sendMessage("setGroupData", { + groupId: selectedGroup?.groupId, + secretKeyData: data, + secretKeyResource: publish, + admins: { names, addresses, both }, + }).catch((error) => { + console.error("Failed to set group data:", error.message || "An error occurred"); + }); + if (decryptedKeyToObject) { setTriedToFetchSecretKey(true); setFirstSecretKeyInCreation(false); @@ -930,13 +929,13 @@ export const Group = ({ selectedGroupRef.current && groupSectionRef.current === "announcement" ) { - chrome?.runtime?.sendMessage({ - action: "addGroupNotificationTimestamp", - payload: { - timestamp: Date.now(), - groupId: selectedGroupRef.current.groupId, - }, - }); + window.sendMessage("addGroupNotificationTimestamp", { + timestamp: Date.now(), + groupId: selectedGroupRef.current.groupId, + }).catch((error) => { + console.error("Failed to add group notification timestamp:", error.message || "An error occurred"); + }); + setTimeout(() => { getGroupAnnouncements(); }, 200); @@ -1026,21 +1025,21 @@ export const Group = ({ try { setIsLoadingNotifyAdmin(true); await new Promise((res, rej) => { - chrome?.runtime?.sendMessage( - { - action: "notifyAdminRegenerateSecretKey", - payload: { - adminAddress: admin.address, - groupName: selectedGroup?.groupName, - }, - }, - (response) => { + window.sendMessage("notifyAdminRegenerateSecretKey", { + adminAddress: admin.address, + groupName: selectedGroup?.groupName, + }) + .then((response) => { if (!response?.error) { res(response); + return; } rej(response.error); - } - ); + }) + .catch((error) => { + rej(error.message || "An error occurred"); + }); + }); setInfoSnack({ type: "success", @@ -1198,13 +1197,13 @@ export const Group = ({ }); - chrome?.runtime?.sendMessage({ - action: "addGroupNotificationTimestamp", - payload: { + window.sendMessage("addGroupNotificationTimestamp", { timestamp: Date.now(), groupId, - }, - }); + }).catch((error) => { + console.error("Failed to add group notification timestamp:", error.message || "An error occurred"); + }); + setTimeout(() => { getGroupAnnouncements(); getTimestampEnterChat(); @@ -1362,13 +1361,13 @@ export const Group = ({ setTriedToFetchSecretKey(false); setFirstSecretKeyInCreation(false); setGroupSection("announcement"); - chrome?.runtime?.sendMessage({ - action: "addGroupNotificationTimestamp", - payload: { - timestamp: Date.now(), - groupId: findGroup.groupId, - }, - }); + window.sendMessage("addGroupNotificationTimestamp", { + timestamp: Date.now(), + groupId: findGroup.groupId, + }).catch((error) => { + console.error("Failed to add group notification timestamp:", error.message || "An error occurred"); + }); + setTimeout(() => { setSelectedGroup(findGroup); setMobileViewMode("group"); @@ -1492,13 +1491,13 @@ export const Group = ({ setSelectedDirect(null); setNewChat(false); setGroupSection("announcement"); - chrome?.runtime?.sendMessage({ - action: "addGroupNotificationTimestamp", - payload: { - timestamp: Date.now(), - groupId: selectedGroupRef.current.groupId, - }, - }); + window.sendMessage("addGroupNotificationTimestamp", { + timestamp: Date.now(), + groupId: selectedGroupRef.current.groupId, + }).catch((error) => { + console.error("Failed to add group notification timestamp:", error.message || "An error occurred"); + }); + setTimeout(() => { getGroupAnnouncements(); }, 200); @@ -1830,19 +1829,7 @@ export const Group = ({ getTimestampEnterChat(); }, 200); - // if (groupSectionRef.current === "announcement") { - // chrome?.runtime?.sendMessage({ - // action: "addGroupNotificationTimestamp", - // payload: { - // timestamp: Date.now(), - // groupId: group.groupId, - // }, - // }); - // } - - // setTimeout(() => { - // getGroupAnnouncements(); - // }, 600); + }} sx={{ display: "flex",