This commit is contained in:
2024-10-29 06:49:25 +02:00
parent cbd372bc07
commit 4da55da54e
13 changed files with 603 additions and 359 deletions

View File

@@ -201,31 +201,30 @@ export const AppPublish = ({ names, categories }) => {
setIsLoading("Publishing... Please wait.");
const fileBase64 = await fileToBase64(file);
await new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "publishOnQDN",
payload: {
data: fileBase64,
service: appType,
title,
description,
category,
tag1,
tag2,
tag3,
tag4,
tag5,
uploadType: 'zip'
},
},
(response) => {
window.sendMessage("publishOnQDN", {
data: fileBase64,
service: appType,
title,
description,
category,
tag1,
tag2,
tag3,
tag4,
tag5,
uploadType: "zip",
})
.then((response) => {
if (!response?.error) {
res(response);
return;
}
rej(response.error);
}
);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
setInfoSnack({
type: "success",

View File

@@ -75,23 +75,21 @@ export const AnnouncementDiscussion = ({
if (!selectedAnnouncement) return;
return new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "publishGroupEncryptedResource",
payload: {
encryptedData,
identifier,
},
},
(response) => {
window.sendMessage("publishGroupEncryptedResource", {
encryptedData,
identifier,
})
.then((response) => {
if (!response?.error) {
res(response);
return
return;
}
rej(response.error);
}
);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
} catch (error) {}
};

View File

@@ -125,184 +125,165 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey,
return
}
return new Promise((res, rej)=> {
chrome?.runtime?.sendMessage({ action: "decryptSingle", payload: {
window.sendMessage("decryptSingle", {
data: encryptedMessages,
secretKeyObject: secretKey
}}, (response) => {
if (!response?.error) {
const filterUImessages = encryptedMessages.filter((item)=> !isExtMsg(item.data))
const decodedUIMessages = decodeBase64ForUIChatMessages(filterUImessages)
const combineUIAndExtensionMsgs = [...decodedUIMessages, ...response]
processWithNewMessages(combineUIAndExtensionMsgs?.map((item)=> {
return {
...item,
...(item?.decryptedData || {})
secretKeyObject: secretKey,
})
.then((response) => {
if (!response?.error) {
const filterUIMessages = encryptedMessages.filter((item) => !isExtMsg(item.data));
const decodedUIMessages = decodeBase64ForUIChatMessages(filterUIMessages);
const combineUIAndExtensionMsgs = [...decodedUIMessages, ...response];
processWithNewMessages(
combineUIAndExtensionMsgs.map((item) => ({
...item,
...(item?.decryptedData || {}),
})),
selectedGroup
);
res(combineUIAndExtensionMsgs);
if (isInitiated) {
const formatted = combineUIAndExtensionMsgs
.filter((rawItem) => !rawItem?.chatReference)
.map((item) => ({
...item,
id: item.signature,
text: item?.decryptedData?.message || "",
repliedTo: item?.repliedTo || item?.decryptedData?.repliedTo,
unread: item?.sender === myAddress ? false : !!item?.chatReference ? false : true,
isNotEncrypted: !!item?.messageText,
}));
setMessages((prev) => [...prev, ...formatted]);
setChatReferences((prev) => {
const organizedChatReferences = { ...prev };
combineUIAndExtensionMsgs
.filter((rawItem) => rawItem && rawItem.chatReference && rawItem.decryptedData?.type === "reaction")
.forEach((item) => {
try {
const content = item.decryptedData?.content;
const sender = item.sender;
const newTimestamp = item.timestamp;
const contentState = item.decryptedData?.contentState;
if (!content || typeof content !== "string" || !sender || typeof sender !== "string" || !newTimestamp) {
console.warn("Invalid content, sender, or timestamp in reaction data", item);
return;
}
organizedChatReferences[item.chatReference] = {
...(organizedChatReferences[item.chatReference] || {}),
reactions: organizedChatReferences[item.chatReference]?.reactions || {},
};
organizedChatReferences[item.chatReference].reactions[content] =
organizedChatReferences[item.chatReference].reactions[content] || [];
let latestTimestampForSender = null;
organizedChatReferences[item.chatReference].reactions[content] =
organizedChatReferences[item.chatReference].reactions[content].filter((reaction) => {
if (reaction.sender === sender) {
latestTimestampForSender = Math.max(latestTimestampForSender || 0, reaction.timestamp);
}
return reaction.sender !== sender;
});
if (latestTimestampForSender && newTimestamp < latestTimestampForSender) {
return;
}
if (contentState !== false) {
organizedChatReferences[item.chatReference].reactions[content].push(item);
}
if (organizedChatReferences[item.chatReference].reactions[content].length === 0) {
delete organizedChatReferences[item.chatReference].reactions[content];
}
} catch (error) {
console.error("Error processing reaction item:", error, item);
}
});
return organizedChatReferences;
});
} else {
const formatted = combineUIAndExtensionMsgs
.filter((rawItem) => !rawItem?.chatReference)
.map((item) => ({
...item,
id: item.signature,
text: item?.decryptedData?.message || "",
repliedTo: item?.repliedTo || item?.decryptedData?.repliedTo,
isNotEncrypted: !!item?.messageText,
unread: false,
}));
setMessages(formatted);
setChatReferences((prev) => {
const organizedChatReferences = { ...prev };
combineUIAndExtensionMsgs
.filter((rawItem) => rawItem && rawItem.chatReference && rawItem.decryptedData?.type === "reaction")
.forEach((item) => {
try {
const content = item.decryptedData?.content;
const sender = item.sender;
const newTimestamp = item.timestamp;
const contentState = item.decryptedData?.contentState;
if (!content || typeof content !== "string" || !sender || typeof sender !== "string" || !newTimestamp) {
console.warn("Invalid content, sender, or timestamp in reaction data", item);
return;
}
organizedChatReferences[item.chatReference] = {
...(organizedChatReferences[item.chatReference] || {}),
reactions: organizedChatReferences[item.chatReference]?.reactions || {},
};
organizedChatReferences[item.chatReference].reactions[content] =
organizedChatReferences[item.chatReference].reactions[content] || [];
let latestTimestampForSender = null;
organizedChatReferences[item.chatReference].reactions[content] =
organizedChatReferences[item.chatReference].reactions[content].filter((reaction) => {
if (reaction.sender === sender) {
latestTimestampForSender = Math.max(latestTimestampForSender || 0, reaction.timestamp);
}
return reaction.sender !== sender;
});
if (latestTimestampForSender && newTimestamp < latestTimestampForSender) {
return;
}
if (contentState !== false) {
organizedChatReferences[item.chatReference].reactions[content].push(item);
}
if (organizedChatReferences[item.chatReference].reactions[content].length === 0) {
delete organizedChatReferences[item.chatReference].reactions[content];
}
} catch (error) {
console.error("Error processing reaction item:", error, item);
}
});
return organizedChatReferences;
});
}
}), selectedGroup)
res(combineUIAndExtensionMsgs)
if(isInitiated){
const formatted = combineUIAndExtensionMsgs.filter((rawItem)=> !rawItem?.chatReference).map((item: any)=> {
return {
...item,
id: item.signature,
text: item?.decryptedData?.message || "",
repliedTo: item?.repliedTo || item?.decryptedData?.repliedTo,
unread: item?.sender === myAddress ? false : !!item?.chatReference ? false : true,
isNotEncrypted: !!item?.messageText
}
} )
setMessages((prev)=> [...prev, ...formatted])
setChatReferences((prev) => {
let organizedChatReferences = { ...prev };
combineUIAndExtensionMsgs
.filter((rawItem) => rawItem && rawItem.chatReference && rawItem.decryptedData?.type === 'reaction')
.forEach((item) => {
try {
const content = item.decryptedData?.content;
const sender = item.sender;
const newTimestamp = item.timestamp;
const contentState = item.decryptedData?.contentState;
if (!content || typeof content !== 'string' || !sender || typeof sender !== 'string' || !newTimestamp) {
console.warn("Invalid content, sender, or timestamp in reaction data", item);
return;
}
// Initialize chat reference and reactions if not present
organizedChatReferences[item.chatReference] = {
...(organizedChatReferences[item.chatReference] || {}),
reactions: organizedChatReferences[item.chatReference]?.reactions || {}
};
organizedChatReferences[item.chatReference].reactions[content] =
organizedChatReferences[item.chatReference].reactions[content] || [];
// Remove any existing reactions from the same sender before adding the new one
let latestTimestampForSender = null;
// Track the latest reaction timestamp for the same content and sender
organizedChatReferences[item.chatReference].reactions[content] =
organizedChatReferences[item.chatReference].reactions[content].filter((reaction) => {
if (reaction.sender === sender) {
// Track the latest timestamp for this sender
latestTimestampForSender = Math.max(latestTimestampForSender || 0, reaction.timestamp);
}
return reaction.sender !== sender;
});
// Compare with the latest tracked timestamp for this sender
if (latestTimestampForSender && newTimestamp < latestTimestampForSender) {
// Ignore this item if it's older than the latest known reaction
return;
}
// Add the new reaction only if contentState is true
if (contentState !== false) {
organizedChatReferences[item.chatReference].reactions[content].push(item);
}
// If the reactions for a specific content are empty, clean up the object
if (organizedChatReferences[item.chatReference].reactions[content].length === 0) {
delete organizedChatReferences[item.chatReference].reactions[content];
}
} catch (error) {
console.error("Error processing reaction item:", error, item);
}
});
return organizedChatReferences;
});
} else {
const formatted = combineUIAndExtensionMsgs.filter((rawItem)=> !rawItem?.chatReference).map((item: any)=> {
return {
...item,
id: item.signature,
text: item?.decryptedData?.message || "",
repliedTo: item?.repliedTo || item?.decryptedData?.repliedTo,
isNotEncrypted: !!item?.messageText,
unread: false
}
} )
setMessages(formatted)
setChatReferences((prev) => {
let organizedChatReferences = { ...prev };
combineUIAndExtensionMsgs
.filter((rawItem) => rawItem && rawItem.chatReference && rawItem.decryptedData?.type === 'reaction')
.forEach((item) => {
try {
const content = item.decryptedData?.content;
const sender = item.sender;
const newTimestamp = item.timestamp;
const contentState = item.decryptedData?.contentState;
if (!content || typeof content !== 'string' || !sender || typeof sender !== 'string' || !newTimestamp) {
console.warn("Invalid content, sender, or timestamp in reaction data", item);
return;
}
// Initialize chat reference and reactions if not present
organizedChatReferences[item.chatReference] = {
...(organizedChatReferences[item.chatReference] || {}),
reactions: organizedChatReferences[item.chatReference]?.reactions || {}
};
organizedChatReferences[item.chatReference].reactions[content] =
organizedChatReferences[item.chatReference].reactions[content] || [];
// Remove any existing reactions from the same sender before adding the new one
let latestTimestampForSender = null;
// Track the latest reaction timestamp for the same content and sender
organizedChatReferences[item.chatReference].reactions[content] =
organizedChatReferences[item.chatReference].reactions[content].filter((reaction) => {
if (reaction.sender === sender) {
// Track the latest timestamp for this sender
latestTimestampForSender = Math.max(latestTimestampForSender || 0, reaction.timestamp);
}
return reaction.sender !== sender;
});
// Compare with the latest tracked timestamp for this sender
if (latestTimestampForSender && newTimestamp < latestTimestampForSender) {
// Ignore this item if it's older than the latest known reaction
return;
}
// Add the new reaction only if contentState is true
if (contentState !== false) {
organizedChatReferences[item.chatReference].reactions[content].push(item);
}
// If the reactions for a specific content are empty, clean up the object
if (organizedChatReferences[item.chatReference].reactions[content].length === 0) {
delete organizedChatReferences[item.chatReference].reactions[content];
}
} catch (error) {
console.error("Error processing reaction item:", error, item);
}
});
return organizedChatReferences;
});
}
}
rej(response.error)
});
rej(response.error);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
})
} catch (error) {
@@ -418,17 +399,22 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey,
const encryptChatMessage = async (data: string, secretKeyObject: any, reactiontypeNumber?: number)=> {
try {
return new Promise((res, rej)=> {
chrome?.runtime?.sendMessage({ action: "encryptSingle", payload: {
window.sendMessage("encryptSingle", {
data,
secretKeyObject,
typeNumber: reactiontypeNumber
}}, (response) => {
if (!response?.error) {
res(response)
}
rej(response.error)
});
typeNumber: reactiontypeNumber,
})
.then((response) => {
if (!response?.error) {
res(response);
return;
}
rej(response.error);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
})
} catch (error) {

View File

@@ -208,43 +208,42 @@ export const GroupAnnouncements = ({
const encryptChatMessage = async (data: string, secretKeyObject: any) => {
try {
return new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "encryptSingle",
payload: {
data,
secretKeyObject,
},
},
(response) => {
window.sendMessage("encryptSingle", {
data,
secretKeyObject,
})
.then((response) => {
if (!response?.error) {
res(response);
return;
}
rej(response.error);
}
);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
} catch (error) {}
};
const publishAnc = async ({ encryptedData, identifier }: any) => {
return new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "publishGroupEncryptedResource",
payload: {
encryptedData,
identifier,
},
},
(response) => {
window.sendMessage("publishGroupEncryptedResource", {
encryptedData,
identifier,
})
.then((response) => {
if (!response?.error) {
res(response);
return;
}
rej(response.error);
}
);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
};
const clearEditorContent = () => {

View File

@@ -145,22 +145,23 @@ export const GroupMail = ({
const updateThreadActivity = async ({threadId, qortalName, groupId, thread}) => {
try {
await new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "updateThreadActivity",
payload: {
threadId, qortalName, groupId, thread
},
},
(response) => {
window.sendMessage("updateThreadActivity", {
threadId,
qortalName,
groupId,
thread,
})
.then((response) => {
if (!response?.error) {
res(response);
return
return;
}
rej(response.error);
}
);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
} catch (error) {

View File

@@ -94,46 +94,42 @@ export const publishGroupEncryptedResource = async ({
identifier,
}) => {
return new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "publishGroupEncryptedResource",
payload: {
encryptedData,
identifier,
},
},
(response) => {
window.sendMessage("publishGroupEncryptedResource", {
encryptedData,
identifier,
})
.then((response) => {
if (!response?.error) {
res(response);
return
return;
}
rej(response.error);
}
);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
};
export const encryptSingleFunc = async (data: string, secretKeyObject: any) => {
try {
return new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "encryptSingle",
payload: {
data,
secretKeyObject,
},
},
(response) => {
window.sendMessage("encryptSingle", {
data,
secretKeyObject,
})
.then((response) => {
if (!response?.error) {
res(response);
return;
}
rej(response.error);
}
);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
} catch (error) {}
};

View File

@@ -199,21 +199,20 @@ export const getGroupMembers = async (groupNumber: number) => {
export const decryptResource = async (data: string) => {
try {
return new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "decryptGroupEncryption",
payload: {
data,
},
},
(response) => {
window.sendMessage("decryptGroupEncryption", {
data,
})
.then((response) => {
if (!response?.error) {
res(response);
return;
}
rej(response.error);
}
);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
} catch (error) {}
};

View File

@@ -24,34 +24,33 @@ export const ListOfThreadPostsWatched = () => {
const getPosts = async () => {
try {
await new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "getThreadActivity",
payload: {},
},
(response) => {
if (!response?.error) {
if (!response) {
res(null);
return;
}
const uniquePosts = response.reduce((acc, current) => {
const x = acc.find(
(item) => item?.thread?.threadId === current?.thread?.threadId
);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
setPosts(uniquePosts);
res(uniquePosts);
window.sendMessage("getThreadActivity", {})
.then((response) => {
if (!response?.error) {
if (!response) {
res(null);
return;
}
rej(response.error);
const uniquePosts = response.reduce((acc, current) => {
const x = acc.find(
(item) => item?.thread?.threadId === current?.thread?.threadId
);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
setPosts(uniquePosts);
res(uniquePosts);
return;
}
);
rej(response.error);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
} catch (error) {
} finally {

View File

@@ -74,13 +74,13 @@ export const WebSocketActive = ({ myAddress, setIsLoadingGroups }) => {
).sort((a, b) => (b.timestamp || 0) - (a.timestamp || 0));
chrome?.runtime?.sendMessage({
action: 'handleActiveGroupDataFromSocket',
payload: {
groups: sortedGroups,
directs: sortedDirects,
},
});
window.sendMessage("handleActiveGroupDataFromSocket", {
groups: sortedGroups,
directs: sortedDirects,
}).catch((error) => {
console.error("Failed to handle active group data from socket:", error.message || "An error occurred");
});
}
} catch (error) {
console.error('Error parsing onmessage data:', error);

View File

@@ -63,24 +63,22 @@ const [isLoading, setIsLoading] = useState(false)
setIsLoading(true);
const avatarBase64 = await fileToBase64(avatarFile)
await new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "publishOnQDN",
payload: {
data: avatarBase64,
identifier: "qortal_avatar",
service: 'THUMBNAIL'
},
},
(response) => {
if (!response?.error) {
res(response);
return
}
rej(response.error);
window.sendMessage("publishOnQDN", {
data: avatarBase64,
identifier: "qortal_avatar",
service: "THUMBNAIL",
})
.then((response) => {
if (!response?.error) {
res(response);
return;
}
);
rej(response.error);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
setAvatarFile(null);
setTempAvatar(`data:image/webp;base64,${avatarBase64}`)

View File

@@ -87,24 +87,22 @@ export const Save = ({isDesktop}) => {
publishFee: fee.fee + ' QORT'
})
const response = await new Promise((res, rej) => {
chrome?.runtime?.sendMessage(
{
action: "publishOnQDN",
payload: {
data: encryptData,
identifier: "ext_saved_settings",
service: 'DOCUMENT_PRIVATE'
},
},
(response) => {
if (!response?.error) {
res(response);
return
}
rej(response.error);
window.sendMessage("publishOnQDN", {
data: encryptData,
identifier: "ext_saved_settings",
service: "DOCUMENT_PRIVATE",
})
.then((response) => {
if (!response?.error) {
res(response);
return;
}
);
rej(response.error);
})
.catch((error) => {
rej(error.message || "An error occurred");
});
});
if(response?.identifier){
setOldPinnedApps(pinnedApps)