Add emit self pubsub config (#59)

This commit is contained in:
Szymon Szlachtowicz 2021-10-08 16:01:37 +02:00 committed by GitHub
parent 771392c960
commit a722e8527a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 42 deletions

View File

@ -62,15 +62,22 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
});
}, []);
const addNewMessageRaw = useCallback(
(
signer: Uint8Array,
content: string,
date: Date,
id: string,
image?: string
) => {
const sender = signer.reduce((p, c) => p + c.toString(16), "0x");
const addNewMessage = useCallback(
(msg: ApplicationMetadataMessage, id: string, date: Date) => {
if (
msg.signer &&
(msg.chatMessage?.text || msg.chatMessage?.image) &&
msg.chatMessage.clock
) {
const content = msg.chatMessage.text ?? "";
let image: string | undefined = undefined;
if (msg.chatMessage?.image) {
image = uintToImgUrl(msg.chatMessage?.image.payload);
}
const sender = msg.signer.reduce(
(p: string, c: number): string => p + c.toString(16),
"0x"
);
const newMessage = { sender, content, date, image };
setMessages((prev) => {
return {
@ -89,28 +96,11 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
[id]: prev[id] + 1,
};
});
}
},
[]
);
const addNewMessage = useCallback(
(msg: ApplicationMetadataMessage, id: string, date: Date) => {
if (
msg.signer &&
(msg.chatMessage?.text || msg.chatMessage?.image) &&
msg.chatMessage.clock
) {
const content = msg.chatMessage.text ?? "";
let img: string | undefined = undefined;
if (msg.chatMessage?.image) {
img = uintToImgUrl(msg.chatMessage?.image.payload);
}
addNewMessageRaw(msg.signer, content, date, id, img);
}
},
[addNewMessageRaw]
);
const loadNextDay = useCallback(
async (id: string) => {
if (messenger) {
@ -147,6 +137,14 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
const identity = Identity.generate();
const messenger = await Messenger.create(identity, {
libp2p: {
config: {
pubsub: {
enabled: true,
emitSelf: true,
},
},
},
bootstrap: getBootstrapNodes.bind({}, [
"fleets",
"wakuv2.test",
@ -200,13 +198,6 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
contentType: 0,
};
await messenger?.sendMessage(chatId, content);
addNewMessageRaw(
messenger?.identity.publicKey ?? new Uint8Array(),
messageText,
new Date(),
chatId,
image ? uintToImgUrl(image) : undefined
);
},
[chatId, messenger]
);