From 29d4e76f88187bb448f705a5c7d9439a5448630a Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Thu, 28 Oct 2021 15:01:43 +0200 Subject: [PATCH] Add comments and refactor contacts class, fix link regex (#100) --- .../components/Chat/ChatMessageContent.tsx | 2 +- .../src/hooks/messenger/useMessenger.ts | 2 - packages/status-communities/src/contacts.ts | 45 ++++++++++++++----- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/packages/react-chat/src/components/Chat/ChatMessageContent.tsx b/packages/react-chat/src/components/Chat/ChatMessageContent.tsx index 4b5e5af2..d35ee1b5 100644 --- a/packages/react-chat/src/components/Chat/ChatMessageContent.tsx +++ b/packages/react-chat/src/components/Chat/ChatMessageContent.tsx @@ -8,7 +8,7 @@ import { ImageMenu } from "../Form/ImageMenu"; /* eslint-disable no-useless-escape */ const regEx = - /(?:(?:http|https):\/\/)?(?:[-a-z0-9]+\.)+[a-z]+(?::\d+)?(?:(?:\/[-\+~%/\.\w]+)?\/?(?:[&?][-\+=&;%@\.\w]+)?(?:#[\w]+)?)?/gi; + /(?:(?:http|https):\/\/)?(?:[-a-z0-9]+\.)+[a-z]+(?::\d+)?(?:(?:\/[-\+~%/\.\w]+)?\/?(?:[&?][-\+=&;%@\.\w]+)?(?:#[\w-]+)?)?/gi; /* eslint-enable no-useless-escape */ type ChatMessageContentProps = { diff --git a/packages/react-chat/src/hooks/messenger/useMessenger.ts b/packages/react-chat/src/hooks/messenger/useMessenger.ts index 09d3a2d5..76df6d09 100644 --- a/packages/react-chat/src/hooks/messenger/useMessenger.ts +++ b/packages/react-chat/src/hooks/messenger/useMessenger.ts @@ -5,7 +5,6 @@ import { Contacts, Identity, Messenger, - utils, } from "status-communities/dist/cjs"; import { Contact } from "../../models/Contact"; @@ -37,7 +36,6 @@ export function useMessenger( }); } ); - newContacts.addContact(utils.bufToHex(identity.publicKey)); return newContacts; } }, [messenger]); diff --git a/packages/status-communities/src/contacts.ts b/packages/status-communities/src/contacts.ts index 7e7e5536..175d7d47 100644 --- a/packages/status-communities/src/contacts.ts +++ b/packages/status-communities/src/contacts.ts @@ -6,42 +6,65 @@ import { StatusUpdate_StatusType } from "./proto/communities/v1/status_update"; import { bufToHex } from "./utils"; import { StatusUpdate } from "./wire/status_update"; +const STATUS_BROADCAST_INTERVAL = 300000; + export class Contacts { waku: Waku; identity: Identity; - private callback: (id: string, clock: number) => void; + private callback: (publicKey: string, clock: number) => void; private contacts: string[] = []; + /** + * Contacts holds a list of user contacts and listens to their status broadcast + * + * When watched user broadcast callback is called. + * + * Class also broadcasts own status on contact-code topic + * + * @param identity identity of user that is used to broadcast status message + * + * @param waku waku class used to listen to broadcast and broadcast status + * + * @param callback callback function called when user status broadcast is received + */ public constructor( identity: Identity, waku: Waku, - callback: (id: string, clock: number) => void + callback: (publicKey: string, clock: number) => void ) { this.waku = waku; this.identity = identity; this.callback = callback; this.startBroadcast(); + this.addContact(bufToHex(identity.publicKey)); } - public addContact(id: string): void { - if (!this.contacts.find((e) => id === e)) { + /** + * Add contact to watch list of status broadcast + * + * When user broadcasts its status callback is called + * + * @param publicKey public key of user + */ + public addContact(publicKey: string): void { + if (!this.contacts.find((e) => publicKey === e)) { const now = new Date(); const callback = (wakuMessage: WakuMessage): void => { if (wakuMessage.payload) { const msg = StatusUpdate.decode(wakuMessage.payload); - this.callback(id, msg.clock ?? 0); + this.callback(publicKey, msg.clock ?? 0); } }; - this.contacts.push(id); - this.callback(id, 0); - this.waku.store.queryHistory([idToContactCodeTopic(id)], { + this.contacts.push(publicKey); + this.callback(publicKey, 0); + this.waku.store.queryHistory([idToContactCodeTopic(publicKey)], { callback: (msgs) => msgs.forEach((e) => callback(e)), timeFilter: { - startTime: new Date(now.getTime() - 400000), + startTime: new Date(now.getTime() - STATUS_BROADCAST_INTERVAL * 2), endTime: now, }, }); - this.waku.relay.addObserver(callback, [idToContactCodeTopic(id)]); + this.waku.relay.addObserver(callback, [idToContactCodeTopic(publicKey)]); } } @@ -58,6 +81,6 @@ export class Contacts { this.waku.relay.send(msg); }; send(); - setInterval(send, 300000); + setInterval(send, STATUS_BROADCAST_INTERVAL); } }