This commit is contained in:
PhilReact 2024-10-29 07:27:46 +02:00
parent 4da55da54e
commit 5074f79a5f
6 changed files with 443 additions and 115 deletions

View File

@ -205,17 +205,18 @@ export const clearAllQueues = () => {
export const pauseAllQueues = () => {
controlAllQueues("pause");
chrome?.runtime?.sendMessage({
action: "pauseAllQueues",
payload: {},
window.sendMessage("pauseAllQueues", {})
.catch((error) => {
console.error("Failed to pause all queues:", error.message || "An error occurred");
});
};
export const resumeAllQueues = () => {
controlAllQueues("resume");
chrome?.runtime?.sendMessage({
action: "resumeAllQueues",
payload: {},
window.sendMessage("resumeAllQueues", {}).catch((error) => {
console.error("Failed to resume all queues:", error.message || "An error occurred");
});
};
export const MyContext = createContext<MyContextInterface>(defaultValues);

View File

@ -6,9 +6,13 @@ import {
banFromGroup,
cancelBan,
cancelInvitationToGroup,
checkLocalFunc,
checkNewMessages,
checkThreads,
clearAllNotifications,
createGroup,
decryptDirectFunc,
decryptSingleForPublishes,
decryptSingleFunc,
decryptWallet,
findUsableApi,
@ -32,9 +36,12 @@ import {
leaveGroup,
makeAdmin,
notifyAdminRegenerateSecretKey,
pauseAllQueues,
registerName,
removeAdmin,
resumeAllQueues,
saveTempPublish,
sendChatDirect,
sendChatGroup,
sendChatNotification,
sendCoin,
@ -1415,4 +1422,228 @@ export async function publishGroupEncryptedResourceCase(request, event) {
event.origin
);
}
}
export async function pauseAllQueuesCase(request, event) {
try {
await pauseAllQueues();
event.source.postMessage(
{
requestId: request.requestId,
action: "pauseAllQueues",
payload: true,
type: "backgroundMessageResponse",
},
event.origin
);
} catch (error) {
event.source.postMessage(
{
requestId: request.requestId,
action: "pauseAllQueues",
error: error?.message,
type: "backgroundMessageResponse",
},
event.origin
);
}
}
export async function resumeAllQueuesCase(request, event) {
try {
await resumeAllQueues();
event.source.postMessage(
{
requestId: request.requestId,
action: "resumeAllQueues",
payload: true,
type: "backgroundMessageResponse",
},
event.origin
);
} catch (error) {
event.source.postMessage(
{
requestId: request.requestId,
action: "resumeAllQueues",
error: error?.message,
type: "backgroundMessageResponse",
},
event.origin
);
}
}
export async function checkLocalCase(request, event) {
try {
const response = await checkLocalFunc()
event.source.postMessage(
{
requestId: request.requestId,
action: "pauseAllQueues",
payload: response,
type: "backgroundMessageResponse",
},
event.origin
);
} catch (error) {
event.source.postMessage(
{
requestId: request.requestId,
action: "checkLocal",
error: error?.message,
type: "backgroundMessageResponse",
},
event.origin
);
}
}
export async function decryptSingleForPublishesCase(request, event) {
try {
const { data, secretKeyObject, skipDecodeBase64} = request.payload;
const response = await decryptSingleForPublishes({ data, secretKeyObject, skipDecodeBase64 });
event.source.postMessage(
{
requestId: request.requestId,
action: "decryptSingleForPublishes",
payload: response,
type: "backgroundMessageResponse",
},
event.origin
);
} catch (error) {
event.source.postMessage(
{
requestId: request.requestId,
action: "decryptSingle",
error: error?.message,
type: "backgroundMessageResponse",
},
event.origin
);
}
}
export async function decryptDirectCase(request, event) {
try {
const { data, involvingAddress} = request.payload;
const response = await decryptDirectFunc({ data, involvingAddress });
event.source.postMessage(
{
requestId: request.requestId,
action: "decryptDirect",
payload: response,
type: "backgroundMessageResponse",
},
event.origin
);
} catch (error) {
event.source.postMessage(
{
requestId: request.requestId,
action: "decryptDirect",
error: error?.message,
type: "backgroundMessageResponse",
},
event.origin
);
}
}
export async function sendChatGroupCase(request, event) {
try {
const { groupId,
typeMessage = undefined,
chatReference = undefined,
messageText} = request.payload;
const response = await sendChatGroup({ groupId, typeMessage, chatReference, messageText });
event.source.postMessage(
{
requestId: request.requestId,
action: "sendChatGroup",
payload: response,
type: "backgroundMessageResponse",
},
event.origin
);
} catch (error) {
event.source.postMessage(
{
requestId: request.requestId,
action: "sendChatGroup",
error: error?.message,
type: "backgroundMessageResponse",
},
event.origin
);
}
}
export async function sendChatDirectCase(request, event) {
try {
const { directTo,
typeMessage = undefined,
chatReference = undefined,
messageText,
publicKeyOfRecipient,
address,
otherData} = request.payload;
const response = await sendChatDirect({ directTo,
chatReference,
messageText,
typeMessage,
publicKeyOfRecipient,
address,
otherData });
event.source.postMessage(
{
requestId: request.requestId,
action: "sendChatDirect",
payload: response,
type: "backgroundMessageResponse",
},
event.origin
);
} catch (error) {
event.source.postMessage(
{
requestId: request.requestId,
action: "sendChatDirect",
error: error?.message,
type: "backgroundMessageResponse",
},
event.origin
);
}
}
export async function setupGroupWebsocketCase(request, event) {
try {
checkNewMessages();
checkThreads();
event.source.postMessage(
{
requestId: request.requestId,
action: "sendChatDirect",
payload: true,
type: "backgroundMessageResponse",
},
event.origin
);
} catch (error) {
event.source.postMessage(
{
requestId: request.requestId,
action: "sendChatDirect",
error: error?.message,
type: "backgroundMessageResponse",
},
event.origin
);
}
}

View File

@ -37,10 +37,13 @@ import {
banFromGroupCase,
cancelBanCase,
cancelInvitationToGroupCase,
checkLocalCase,
clearAllNotificationsCase,
createGroupCase,
decryptDirectCase,
decryptGroupEncryptionCase,
decryptSingleCase,
decryptSingleForPublishesCase,
decryptWalletCase,
encryptAndPublishSymmetricKeyGroupChatCase,
encryptSingleCase,
@ -64,16 +67,21 @@ import {
nameCase,
notificationCase,
notifyAdminRegenerateSecretKeyCase,
pauseAllQueuesCase,
publishGroupEncryptedResourceCase,
publishOnQDNCase,
registerNameCase,
removeAdminCase,
resumeAllQueuesCase,
saveTempPublishCase,
sendChatDirectCase,
sendChatGroupCase,
sendCoinCase,
setApiKeyCase,
setChatHeadsCase,
setCustomNodesCase,
setGroupDataCase,
setupGroupWebsocketCase,
updateThreadActivityCase,
userInfoCase,
validApiCase,
@ -150,8 +158,8 @@ export const clearAllQueues = () => {
});
};
const pauseAllQueues = () => controlAllQueues("pause");
const resumeAllQueues = () => controlAllQueues("resume");
export const pauseAllQueues = () => controlAllQueues("pause");
export const resumeAllQueues = () => controlAllQueues("resume");
const checkDifference = (createdTimestamp) => {
return (
Date.now() - createdTimestamp < timeDifferenceForNotificationChatsBackground
@ -862,7 +870,7 @@ export const checkThreads = async (bringBack) => {
} finally {
}
};
const checkNewMessages = async () => {
export const checkNewMessages = async () => {
try {
let mutedGroups = (await getUserSettings({ key: "mutedGroups" })) || [];
if (!isArray(mutedGroups)) mutedGroups = [];
@ -1731,7 +1739,7 @@ export async function sendChatGroup({
return _response;
}
async function sendChatDirect({
export async function sendChatDirect({
address,
directTo,
typeMessage,
@ -1834,7 +1842,7 @@ export async function decryptSingleFunc({
}
return holdMessages;
}
async function decryptSingleForPublishes({
export async function decryptSingleForPublishes({
messages,
secretKeyObject,
skipDecodeBase64,
@ -1857,7 +1865,7 @@ async function decryptSingleForPublishes({
return holdMessages;
}
async function decryptDirectFunc({ messages, involvingAddress }) {
export async function decryptDirectFunc({ messages, involvingAddress }) {
const senderPublicKey = await getPublicKey(involvingAddress);
let holdMessages = [];
@ -2665,7 +2673,7 @@ export async function setChatHeads(data) {
});
}
async function checkLocalFunc() {
export async function checkLocalFunc() {
const apiKey = await getApiKeyFromStorage();
return !!apiKey;
}
@ -3075,12 +3083,90 @@ function setupMessageListener() {
case "encryptSingle":
encryptSingleCase(request, event);
break;
case "decryptSingle":
decryptSingleCase(request, event);
break;
case "decryptSingle":
decryptSingleCase(request, event);
break;
case "pauseAllQueues":
pauseAllQueuesCase(request, event);
break;
case "resumeAllQueues":
resumeAllQueuesCase(request, event);
break;
case "checkLocal":
checkLocalCase(request, event);
break;
case "decryptSingleForPublishes":
decryptSingleForPublishesCase(request, event);
break;
case "decryptDirect":
decryptDirectCase(request, event);
break;
case "sendChatGroup":
sendChatGroupCase(request, event);
break;
case "sendChatDirect":
sendChatDirectCase(request, event);
break;
case "setupGroupWebsocket":
setupGroupWebsocketCase(request, event);
break;
case "logout":
{
try {
const logoutFunc = async () => {
forceCloseWebSocket();
clearAllQueues();
if (interval) {
// for announcement notification
clearInterval(interval);
}
const wallet = await getSaveWallet();
const address = wallet.address0;
const key1 = `tempPublish-${address}`;
const key2 = `group-data-${address}`;
const key3 = `${address}-publishData`;
chrome.storage.local.remove(
[
"keyPair",
"walletInfo",
"active-groups-directs",
key1,
key2,
key3,
],
() => {
if (chrome.runtime.lastError) {
// Handle error
console.error(chrome.runtime.lastError.message);
} else {
chrome.tabs.query({}, function (tabs) {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, { type: "LOGOUT" });
});
});
// Data removed successfully
event.source.postMessage(
{
requestId: request.requestId,
action: "logout",
payload: true,
type: "backgroundMessageResponse",
},
event.origin
);
}
}
);
};
logoutFunc();
} catch (error) {}
}
break;
default:
console.error("Unknown action:", request.action);
break
break;
}
});
}

View File

@ -101,43 +101,41 @@ export const ChatDirect = ({ myAddress, isNewChat, selectedDirect, setSelectedDi
const decryptMessages = (encryptedMessages: any[], isInitiated: boolean)=> {
try {
return new Promise((res, rej)=> {
chrome?.runtime?.sendMessage({ action: "decryptDirect", payload: {
window.sendMessage("decryptDirect", {
data: encryptedMessages,
involvingAddress: selectedDirect?.address
}}, (response) => {
if (!response?.error) {
processWithNewMessages(response, selectedDirect?.address)
res(response)
if(isInitiated){
involvingAddress: selectedDirect?.address,
})
.then((response) => {
if (!response?.error) {
processWithNewMessages(response, selectedDirect?.address);
res(response);
const formatted = response.map((item: any)=> {
return {
if (isInitiated) {
const formatted = response.map((item) => ({
...item,
id: item.signature,
text: item.message,
unread: item?.sender === myAddress ? false : true
}
} )
setMessages((prev)=> [...prev, ...formatted])
} else {
const formatted = response.map((item: any)=> {
return {
unread: item?.sender === myAddress ? false : true,
}));
setMessages((prev) => [...prev, ...formatted]);
} else {
const formatted = response.map((item) => ({
...item,
id: item.signature,
text: item.message,
unread: false
}
} )
setMessages(formatted)
hasInitialized.current = true
unread: false,
}));
setMessages(formatted);
hasInitialized.current = true;
}
return;
}
}
rej(response.error)
});
rej(response.error);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
})
} catch (error) {
@ -246,43 +244,52 @@ const sendChatDirect = async ({ chatReference = undefined, messageText, otherDat
if(!directTo) return
return new Promise((res, rej)=> {
chrome?.runtime?.sendMessage({ action: "sendChatDirect", payload: {
directTo, chatReference, messageText, otherData, publicKeyOfRecipient, address: directTo
}}, async (response) => {
if (!response?.error) {
if(isNewChatVar){
let getRecipientName = null
try {
getRecipientName = await getNameInfo(response.recipient)
} catch (error) {
window.sendMessage("sendChatDirect", {
directTo,
chatReference,
messageText,
otherData,
publicKeyOfRecipient,
address: directTo,
})
.then(async (response) => {
if (!response?.error) {
if (isNewChatVar) {
let getRecipientName = null;
try {
getRecipientName = await getNameInfo(response.recipient);
} catch (error) {
console.error("Error fetching recipient name:", error);
}
setSelectedDirect({
address: response.recipient,
name: getRecipientName,
timestamp: Date.now(),
sender: myAddress,
senderName: myName,
});
setNewChat(null);
window.sendMessage("addTimestampEnterChat", {
timestamp: Date.now(),
groupId: response.recipient,
}).catch((error) => {
console.error("Failed to add timestamp:", error.message || "An error occurred");
});
setTimeout(() => {
getTimestampEnterChat();
}, 400);
}
setSelectedDirect({
"address": response.recipient,
"name": getRecipientName,
"timestamp": Date.now(),
"sender": myAddress,
"senderName": myName
})
setNewChat(null)
window.sendMessage("addTimestampEnterChat", {
timestamp: Date.now(),
groupId: response.recipient,
}).catch((error) => {
console.error("Failed to add timestamp:", error.message || "An error occurred");
});
setTimeout(() => {
getTimestampEnterChat()
}, 400);
res(response);
return;
}
res(response)
return
}
rej(response.error)
});
rej(response.error);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
})
} catch (error) {
throw new Error(error)

View File

@ -424,16 +424,23 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey,
const sendChatGroup = async ({groupId, typeMessage = undefined, chatReference = undefined, messageText}: any)=> {
try {
return new Promise((res, rej)=> {
chrome?.runtime?.sendMessage({ action: "sendChatGroup", payload: {
groupId, typeMessage, chatReference, messageText
}}, (response) => {
if (!response?.error) {
res(response)
return
}
rej(response.error)
});
window.sendMessage("sendChatGroup", {
groupId,
typeMessage,
chatReference,
messageText,
})
.then((response) => {
if (!response?.error) {
res(response);
return;
}
rej(response.error);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
})
} catch (error) {
throw new Error(error)

View File

@ -86,38 +86,34 @@ export const getTempPublish = async () => {
export const decryptPublishes = async (encryptedMessages: any[], secretKey) => {
try {
return await new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "decryptSingleForPublishes",
payload: {
data: encryptedMessages,
secretKeyObject: secretKey,
skipDecodeBase64: true,
},
},
(response) => {
window.sendMessage("decryptSingleForPublishes", {
data: encryptedMessages,
secretKeyObject: secretKey,
skipDecodeBase64: true,
})
.then((response) => {
if (!response?.error) {
res(response);
// if(hasInitialized.current){
// setMessages((prev)=> [...prev, ...formatted])
// setMessages((prev) => [...prev, ...formatted]);
// } else {
// const formatted = response.map((item: any)=> {
// return {
// ...item,
// id: item.signature,
// text: item.text,
// unread: false
// }
// } )
// setMessages(formatted)
// hasInitialized.current = true
// const formatted = response.map((item) => ({
// ...item,
// id: item.signature,
// text: item.text,
// unread: false
// }));
// setMessages(formatted);
// hasInitialized.current = true;
// }
return;
}
rej(response.error);
}
);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
} catch (error) {}
};