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 },
|
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
|
||||||
"rules": {
|
"rules": {
|
||||||
"@typescript-eslint/explicit-function-return-type": ["warn"],
|
|
||||||
"@typescript-eslint/explicit-module-boundary-types": "off",
|
"@typescript-eslint/explicit-module-boundary-types": "off",
|
||||||
"eslint-comments/disable-enable-pair": [
|
"eslint-comments/disable-enable-pair": [
|
||||||
"error",
|
"error",
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
],
|
],
|
||||||
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
|
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
|
||||||
"rules": {
|
"rules": {
|
||||||
"@typescript-eslint/explicit-function-return-type": ["warn"],
|
|
||||||
"@typescript-eslint/explicit-module-boundary-types": "off",
|
"@typescript-eslint/explicit-module-boundary-types": "off",
|
||||||
"eslint-comments/disable-enable-pair": [
|
"eslint-comments/disable-enable-pair": [
|
||||||
"error",
|
"error",
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
// import { StoreCodec } from "js-waku";
|
// import { StoreCodec } from "js-waku";
|
||||||
|
import { getBootstrapNodes, StoreCodec } from "js-waku";
|
||||||
import { useCallback, useEffect, useState } from "react";
|
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";
|
import { ChatMessage } from "../models/ChatMessage";
|
||||||
|
|
||||||
|
@ -46,14 +52,12 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (chatId != id) {
|
setNotifications((prevNotifications) => {
|
||||||
setNotifications((prevNotifications) => {
|
return {
|
||||||
return {
|
...prevNotifications,
|
||||||
...prevNotifications,
|
[id]: prevNotifications[id] + 1,
|
||||||
[id]: prevNotifications[id] + 1,
|
};
|
||||||
};
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
@ -61,15 +65,63 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const createMessenger = async () => {
|
const createMessenger = async () => {
|
||||||
const identity = Identity.generate();
|
const identity = Identity.generate();
|
||||||
const messenger = await Messenger.create(identity);
|
|
||||||
await new Promise((resolve) =>
|
const messenger = await Messenger.create(identity, {
|
||||||
messenger.waku.libp2p.pubsub.once("pubsub:subscription-change", () =>
|
bootstrap: getBootstrapNodes.bind({}, [
|
||||||
resolve(null)
|
"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(
|
await Promise.all(
|
||||||
chatIdList.map(async (id) => {
|
chatIdList.map(async (id) => {
|
||||||
await messenger.joinChat(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);
|
clearNotifications(id);
|
||||||
messenger.addObserver((message) => {
|
messenger.addObserver((message) => {
|
||||||
addNewMessage(
|
addNewMessage(
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
|
import { ApplicationMetadataMessage } from "./application_metadata_message";
|
||||||
|
import { ChatMessage } from "./chat_message";
|
||||||
import { Identity } from "./identity";
|
import { Identity } from "./identity";
|
||||||
import { Messenger } from "./messenger";
|
import { Messenger } from "./messenger";
|
||||||
|
export { Messenger, Identity, ApplicationMetadataMessage, ChatMessage };
|
||||||
export { Messenger, Identity };
|
|
||||||
|
|
Loading…
Reference in New Issue