Use history in messaging (#21)
This commit is contained in:
parent
e379aeca84
commit
0f9572cbfb
|
@ -14,7 +14,6 @@
|
|||
],
|
||||
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
|
||||
"rules": {
|
||||
"@typescript-eslint/explicit-function-return-type": ["warn"],
|
||||
"@typescript-eslint/explicit-module-boundary-types": "off",
|
||||
"eslint-comments/disable-enable-pair": [
|
||||
"error",
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
],
|
||||
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
|
||||
"rules": {
|
||||
"@typescript-eslint/explicit-function-return-type": ["warn"],
|
||||
"@typescript-eslint/explicit-module-boundary-types": "off",
|
||||
"eslint-comments/disable-enable-pair": [
|
||||
"error",
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
// import { StoreCodec } from "js-waku";
|
||||
import { getBootstrapNodes, StoreCodec } from "js-waku";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { Identity, Messenger } from "status-communities/dist/cjs";
|
||||
import {
|
||||
ChatMessage as ApiChatMessage,
|
||||
ApplicationMetadataMessage,
|
||||
Identity,
|
||||
Messenger,
|
||||
} from "status-communities/dist/cjs";
|
||||
|
||||
import { ChatMessage } from "../models/ChatMessage";
|
||||
|
||||
|
@ -46,14 +52,12 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
|
|||
};
|
||||
}
|
||||
});
|
||||
if (chatId != id) {
|
||||
setNotifications((prevNotifications) => {
|
||||
return {
|
||||
...prevNotifications,
|
||||
[id]: prevNotifications[id] + 1,
|
||||
};
|
||||
});
|
||||
}
|
||||
setNotifications((prevNotifications) => {
|
||||
return {
|
||||
...prevNotifications,
|
||||
[id]: prevNotifications[id] + 1,
|
||||
};
|
||||
});
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
@ -61,15 +65,63 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
|
|||
useEffect(() => {
|
||||
const createMessenger = async () => {
|
||||
const identity = Identity.generate();
|
||||
const messenger = await Messenger.create(identity);
|
||||
await new Promise((resolve) =>
|
||||
messenger.waku.libp2p.pubsub.once("pubsub:subscription-change", () =>
|
||||
resolve(null)
|
||||
)
|
||||
);
|
||||
|
||||
const messenger = await Messenger.create(identity, {
|
||||
bootstrap: getBootstrapNodes.bind({}, [
|
||||
"fleets",
|
||||
"wakuv2.prod",
|
||||
"waku-websocket",
|
||||
]),
|
||||
});
|
||||
await new Promise((resolve) => {
|
||||
messenger.waku.libp2p.peerStore.on(
|
||||
"change:protocols",
|
||||
({ protocols }) => {
|
||||
if (protocols.includes(StoreCodec)) {
|
||||
resolve("");
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
chatIdList.map(async (id) => {
|
||||
await messenger.joinChat(id);
|
||||
const chat = messenger.chatsById.get(id);
|
||||
if (chat) {
|
||||
const messages = await messenger.waku.store.queryHistory(
|
||||
[chat.contentTopic],
|
||||
{ decryptionKeys: [chat.symKey] }
|
||||
);
|
||||
messages.sort((a, b) =>
|
||||
(a?.timestamp?.getTime() ?? 0) > (b?.timestamp?.getTime() ?? 0)
|
||||
? 1
|
||||
: -1
|
||||
);
|
||||
messages.forEach((message) => {
|
||||
if (message.payload) {
|
||||
const metadata = ApplicationMetadataMessage.decode(
|
||||
message.payload
|
||||
);
|
||||
if (metadata.payload) {
|
||||
const chatMessage = ApiChatMessage.decode(metadata.payload);
|
||||
if (
|
||||
metadata.signer &&
|
||||
chatMessage.text &&
|
||||
chatMessage.proto.timestamp
|
||||
) {
|
||||
addNewMessage(
|
||||
metadata.signer,
|
||||
chatMessage.text,
|
||||
new Date(chatMessage.proto.timestamp ?? 0),
|
||||
id
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
clearNotifications(id);
|
||||
messenger.addObserver((message) => {
|
||||
addNewMessage(
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { ApplicationMetadataMessage } from "./application_metadata_message";
|
||||
import { ChatMessage } from "./chat_message";
|
||||
import { Identity } from "./identity";
|
||||
import { Messenger } from "./messenger";
|
||||
|
||||
export { Messenger, Identity };
|
||||
export { Messenger, Identity, ApplicationMetadataMessage, ChatMessage };
|
||||
|
|
Loading…
Reference in New Issue