This commit is contained in:
PhilReact 2024-10-28 13:24:08 +02:00
parent abb9eea4c4
commit c9055e4b68
4 changed files with 234 additions and 83 deletions

View File

@ -1201,10 +1201,11 @@ function App() {
const handleFocus = () => { const handleFocus = () => {
setIsFocused(true); setIsFocused(true);
if (isMobile) { if (isMobile) {
chrome?.runtime?.sendMessage({ window.sendMessage("clearAllNotifications", {})
action: "clearAllNotifications", .catch((error) => {
payload: {}, console.error("Failed to clear notifications:", error.message || "An error occurred");
}); });
} }
}; };
@ -1223,10 +1224,11 @@ function App() {
if (document.visibilityState === "visible") { if (document.visibilityState === "visible") {
setIsFocused(true); setIsFocused(true);
if (isMobile) { if (isMobile) {
chrome?.runtime?.sendMessage({ window.sendMessage("clearAllNotifications", {})
action: "clearAllNotifications", .catch((error) => {
payload: {}, console.error("Failed to clear notifications:", error.message || "An error occurred");
}); });
} }
} else { } else {
setIsFocused(false); setIsFocused(false);

View File

@ -1,10 +1,12 @@
import { import {
addDataPublishes, addDataPublishes,
addTimestampEnterChat, addTimestampEnterChat,
addTimestampGroupAnnouncement,
addUserSettings, addUserSettings,
banFromGroup, banFromGroup,
cancelBan, cancelBan,
cancelInvitationToGroup, cancelInvitationToGroup,
clearAllNotifications,
createGroup, createGroup,
decryptWallet, decryptWallet,
findUsableApi, findUsableApi,
@ -12,6 +14,7 @@ import {
getBalanceInfo, getBalanceInfo,
getCustomNodesFromStorage, getCustomNodesFromStorage,
getDataPublishes, getDataPublishes,
getGroupDataSingle,
getKeyPair, getKeyPair,
getLTCBalance, getLTCBalance,
getNameInfo, getNameInfo,
@ -23,11 +26,13 @@ import {
kickFromGroup, kickFromGroup,
leaveGroup, leaveGroup,
makeAdmin, makeAdmin,
notifyAdminRegenerateSecretKey,
registerName, registerName,
removeAdmin, removeAdmin,
saveTempPublish, saveTempPublish,
sendCoin, sendCoin,
setChatHeads, setChatHeads,
setGroupData,
walletVersion, walletVersion,
} from "./background"; } from "./background";
@ -898,4 +903,138 @@ export async function balanceCase(request, event) {
event.origin 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
);
}
} }

View File

@ -30,17 +30,20 @@ import { TradeBotRespondMultipleRequest } from "./transactions/TradeBotRespondMu
import { RESOURCE_TYPE_NUMBER_GROUP_CHAT_REACTIONS } from "./constants/resourceTypes"; import { RESOURCE_TYPE_NUMBER_GROUP_CHAT_REACTIONS } from "./constants/resourceTypes";
import { import {
addDataPublishesCase, addDataPublishesCase,
addGroupNotificationTimestampCase,
addTimestampEnterChatCase, addTimestampEnterChatCase,
addUserSettingsCase, addUserSettingsCase,
balanceCase, balanceCase,
banFromGroupCase, banFromGroupCase,
cancelBanCase, cancelBanCase,
cancelInvitationToGroupCase, cancelInvitationToGroupCase,
clearAllNotificationsCase,
createGroupCase, createGroupCase,
decryptWalletCase, decryptWalletCase,
getApiKeyCase, getApiKeyCase,
getCustomNodesFromStorageCase, getCustomNodesFromStorageCase,
getDataPublishesCase, getDataPublishesCase,
getGroupDataSingleCase,
getTempPublishCase, getTempPublishCase,
getUserSettingsCase, getUserSettingsCase,
getWalletInfoCase, getWalletInfoCase,
@ -52,6 +55,7 @@ import {
makeAdminCase, makeAdminCase,
nameCase, nameCase,
notificationCase, notificationCase,
notifyAdminRegenerateSecretKeyCase,
registerNameCase, registerNameCase,
removeAdminCase, removeAdminCase,
saveTempPublishCase, saveTempPublishCase,
@ -59,6 +63,7 @@ import {
setApiKeyCase, setApiKeyCase,
setChatHeadsCase, setChatHeadsCase,
setCustomNodesCase, setCustomNodesCase,
setGroupDataCase,
userInfoCase, userInfoCase,
validApiCase, validApiCase,
versionCase, versionCase,
@ -1038,7 +1043,7 @@ export async function getSaveWallet() {
} }
} }
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)) {
await chrome.notifications.clear(notificationId); await chrome.notifications.clear(notificationId);
@ -2754,7 +2759,7 @@ async function getTimestampGroupAnnouncement() {
} }
} }
async function addTimestampGroupAnnouncement({ export async function addTimestampGroupAnnouncement({
groupId, groupId,
timestamp, timestamp,
seenTimestamp, seenTimestamp,
@ -2793,7 +2798,7 @@ async function getGroupData() {
return {}; return {};
} }
} }
async function getGroupDataSingle(groupId) { export async function getGroupDataSingle(groupId) {
const wallet = await getSaveWallet(); const wallet = await getSaveWallet();
const address = wallet.address0; const address = wallet.address0;
const key = `group-data-${address}`; const key = `group-data-${address}`;
@ -2806,7 +2811,7 @@ async function getGroupDataSingle(groupId) {
} }
} }
async function setGroupData({ export async function setGroupData({
groupId, groupId,
secretKeyData, secretKeyData,
secretKeyResource, 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 wallet = await getSaveWallet();
const address = wallet.address0; const address = wallet.address0;
const name = await getNameInfo(address); const name = await getNameInfo(address);
@ -3004,9 +3012,24 @@ function setupMessageListener() {
case "getApiKey": case "getApiKey":
getApiKeyCase(request, event); getApiKeyCase(request, event);
break; break;
case "getCustomNodesFromStorage": case "getCustomNodesFromStorage":
getCustomNodesFromStorageCase(request, event); getCustomNodesFromStorageCase(request, event);
break; 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: default:
console.error("Unknown action:", request.action); console.error("Unknown action:", request.action);
} }

View File

@ -521,21 +521,20 @@ export const Group = ({
const getGroupDataSingle = async (groupId) => { const getGroupDataSingle = async (groupId) => {
try { try {
return new Promise((res, rej) => { return new Promise((res, rej) => {
chrome?.runtime?.sendMessage( window.sendMessage("getGroupDataSingle", {
{ groupId,
action: "getGroupDataSingle", })
payload: { .then((response) => {
groupId,
},
},
(response) => {
if (!response?.error) { if (!response?.error) {
res(response); res(response);
return; return;
} }
rej(response.error); rej(response.error);
} })
); .catch((error) => {
rej(error.message || "An error occurred");
});
}); });
} catch (error) { } catch (error) {
return {}; return {};
@ -797,15 +796,15 @@ export const Group = ({
setSecretKey(decryptedKeyToObject); setSecretKey(decryptedKeyToObject);
lastFetchedSecretKey.current = Date.now(); lastFetchedSecretKey.current = Date.now();
setMemberCountFromSecretKeyData(decryptedKey.count); setMemberCountFromSecretKeyData(decryptedKey.count);
chrome?.runtime?.sendMessage({ window.sendMessage("setGroupData", {
action: "setGroupData", groupId: selectedGroup?.groupId,
payload: { secretKeyData: data,
groupId: selectedGroup?.groupId, secretKeyResource: publish,
secretKeyData: data, admins: { names, addresses, both },
secretKeyResource: publish, }).catch((error) => {
admins: { names, addresses, both }, console.error("Failed to set group data:", error.message || "An error occurred");
}, });
});
if (decryptedKeyToObject) { if (decryptedKeyToObject) {
setTriedToFetchSecretKey(true); setTriedToFetchSecretKey(true);
setFirstSecretKeyInCreation(false); setFirstSecretKeyInCreation(false);
@ -930,13 +929,13 @@ export const Group = ({
selectedGroupRef.current && selectedGroupRef.current &&
groupSectionRef.current === "announcement" groupSectionRef.current === "announcement"
) { ) {
chrome?.runtime?.sendMessage({ window.sendMessage("addGroupNotificationTimestamp", {
action: "addGroupNotificationTimestamp", timestamp: Date.now(),
payload: { groupId: selectedGroupRef.current.groupId,
timestamp: Date.now(), }).catch((error) => {
groupId: selectedGroupRef.current.groupId, console.error("Failed to add group notification timestamp:", error.message || "An error occurred");
}, });
});
setTimeout(() => { setTimeout(() => {
getGroupAnnouncements(); getGroupAnnouncements();
}, 200); }, 200);
@ -1026,21 +1025,21 @@ export const Group = ({
try { try {
setIsLoadingNotifyAdmin(true); setIsLoadingNotifyAdmin(true);
await new Promise((res, rej) => { await new Promise((res, rej) => {
chrome?.runtime?.sendMessage( window.sendMessage("notifyAdminRegenerateSecretKey", {
{ adminAddress: admin.address,
action: "notifyAdminRegenerateSecretKey", groupName: selectedGroup?.groupName,
payload: { })
adminAddress: admin.address, .then((response) => {
groupName: selectedGroup?.groupName,
},
},
(response) => {
if (!response?.error) { if (!response?.error) {
res(response); res(response);
return;
} }
rej(response.error); rej(response.error);
} })
); .catch((error) => {
rej(error.message || "An error occurred");
});
}); });
setInfoSnack({ setInfoSnack({
type: "success", type: "success",
@ -1198,13 +1197,13 @@ export const Group = ({
}); });
chrome?.runtime?.sendMessage({ window.sendMessage("addGroupNotificationTimestamp", {
action: "addGroupNotificationTimestamp",
payload: {
timestamp: Date.now(), timestamp: Date.now(),
groupId, groupId,
}, }).catch((error) => {
}); console.error("Failed to add group notification timestamp:", error.message || "An error occurred");
});
setTimeout(() => { setTimeout(() => {
getGroupAnnouncements(); getGroupAnnouncements();
getTimestampEnterChat(); getTimestampEnterChat();
@ -1362,13 +1361,13 @@ export const Group = ({
setTriedToFetchSecretKey(false); setTriedToFetchSecretKey(false);
setFirstSecretKeyInCreation(false); setFirstSecretKeyInCreation(false);
setGroupSection("announcement"); setGroupSection("announcement");
chrome?.runtime?.sendMessage({ window.sendMessage("addGroupNotificationTimestamp", {
action: "addGroupNotificationTimestamp", timestamp: Date.now(),
payload: { groupId: findGroup.groupId,
timestamp: Date.now(), }).catch((error) => {
groupId: findGroup.groupId, console.error("Failed to add group notification timestamp:", error.message || "An error occurred");
}, });
});
setTimeout(() => { setTimeout(() => {
setSelectedGroup(findGroup); setSelectedGroup(findGroup);
setMobileViewMode("group"); setMobileViewMode("group");
@ -1492,13 +1491,13 @@ export const Group = ({
setSelectedDirect(null); setSelectedDirect(null);
setNewChat(false); setNewChat(false);
setGroupSection("announcement"); setGroupSection("announcement");
chrome?.runtime?.sendMessage({ window.sendMessage("addGroupNotificationTimestamp", {
action: "addGroupNotificationTimestamp", timestamp: Date.now(),
payload: { groupId: selectedGroupRef.current.groupId,
timestamp: Date.now(), }).catch((error) => {
groupId: selectedGroupRef.current.groupId, console.error("Failed to add group notification timestamp:", error.message || "An error occurred");
}, });
});
setTimeout(() => { setTimeout(() => {
getGroupAnnouncements(); getGroupAnnouncements();
}, 200); }, 200);
@ -1830,19 +1829,7 @@ export const Group = ({
getTimestampEnterChat(); getTimestampEnterChat();
}, 200); }, 200);
// if (groupSectionRef.current === "announcement") {
// chrome?.runtime?.sendMessage({
// action: "addGroupNotificationTimestamp",
// payload: {
// timestamp: Date.now(),
// groupId: group.groupId,
// },
// });
// }
// setTimeout(() => {
// getGroupAnnouncements();
// }, 600);
}} }}
sx={{ sx={{
display: "flex", display: "flex",