Add comments and refactor contacts class, fix link regex (#100)
This commit is contained in:
parent
7c3f256e61
commit
29d4e76f88
|
@ -8,7 +8,7 @@ import { ImageMenu } from "../Form/ImageMenu";
|
||||||
|
|
||||||
/* eslint-disable no-useless-escape */
|
/* eslint-disable no-useless-escape */
|
||||||
const regEx =
|
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 */
|
/* eslint-enable no-useless-escape */
|
||||||
|
|
||||||
type ChatMessageContentProps = {
|
type ChatMessageContentProps = {
|
||||||
|
|
|
@ -5,7 +5,6 @@ import {
|
||||||
Contacts,
|
Contacts,
|
||||||
Identity,
|
Identity,
|
||||||
Messenger,
|
Messenger,
|
||||||
utils,
|
|
||||||
} from "status-communities/dist/cjs";
|
} from "status-communities/dist/cjs";
|
||||||
|
|
||||||
import { Contact } from "../../models/Contact";
|
import { Contact } from "../../models/Contact";
|
||||||
|
@ -37,7 +36,6 @@ export function useMessenger(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
newContacts.addContact(utils.bufToHex(identity.publicKey));
|
|
||||||
return newContacts;
|
return newContacts;
|
||||||
}
|
}
|
||||||
}, [messenger]);
|
}, [messenger]);
|
||||||
|
|
|
@ -6,42 +6,65 @@ import { StatusUpdate_StatusType } from "./proto/communities/v1/status_update";
|
||||||
import { bufToHex } from "./utils";
|
import { bufToHex } from "./utils";
|
||||||
import { StatusUpdate } from "./wire/status_update";
|
import { StatusUpdate } from "./wire/status_update";
|
||||||
|
|
||||||
|
const STATUS_BROADCAST_INTERVAL = 300000;
|
||||||
|
|
||||||
export class Contacts {
|
export class Contacts {
|
||||||
waku: Waku;
|
waku: Waku;
|
||||||
identity: Identity;
|
identity: Identity;
|
||||||
private callback: (id: string, clock: number) => void;
|
private callback: (publicKey: string, clock: number) => void;
|
||||||
private contacts: string[] = [];
|
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(
|
public constructor(
|
||||||
identity: Identity,
|
identity: Identity,
|
||||||
waku: Waku,
|
waku: Waku,
|
||||||
callback: (id: string, clock: number) => void
|
callback: (publicKey: string, clock: number) => void
|
||||||
) {
|
) {
|
||||||
this.waku = waku;
|
this.waku = waku;
|
||||||
this.identity = identity;
|
this.identity = identity;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
this.startBroadcast();
|
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 now = new Date();
|
||||||
const callback = (wakuMessage: WakuMessage): void => {
|
const callback = (wakuMessage: WakuMessage): void => {
|
||||||
if (wakuMessage.payload) {
|
if (wakuMessage.payload) {
|
||||||
const msg = StatusUpdate.decode(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.contacts.push(publicKey);
|
||||||
this.callback(id, 0);
|
this.callback(publicKey, 0);
|
||||||
this.waku.store.queryHistory([idToContactCodeTopic(id)], {
|
this.waku.store.queryHistory([idToContactCodeTopic(publicKey)], {
|
||||||
callback: (msgs) => msgs.forEach((e) => callback(e)),
|
callback: (msgs) => msgs.forEach((e) => callback(e)),
|
||||||
timeFilter: {
|
timeFilter: {
|
||||||
startTime: new Date(now.getTime() - 400000),
|
startTime: new Date(now.getTime() - STATUS_BROADCAST_INTERVAL * 2),
|
||||||
endTime: now,
|
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);
|
this.waku.relay.send(msg);
|
||||||
};
|
};
|
||||||
send();
|
send();
|
||||||
setInterval(send, 300000);
|
setInterval(send, STATUS_BROADCAST_INTERVAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue