diff --git a/packages/react-chat/src/components/Chat/ChatMessageContent.tsx b/packages/react-chat/src/components/Chat/ChatMessageContent.tsx index 4b5e5af..d35ee1b 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 09d3a2d..76df6d0 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 7e7e553..175d7d4 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); } }