Refactor retrieving messages and timestamp (#51)

This commit is contained in:
Szymon Szlachtowicz 2021-10-07 15:20:07 +02:00 committed by GitHub
parent d4353cad84
commit 000ffa2aba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 47 deletions

View File

@ -83,7 +83,7 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
); );
const addNewMessage = useCallback( const addNewMessage = useCallback(
(msg: ApplicationMetadataMessage, id: string) => { (msg: ApplicationMetadataMessage, id: string, date: Date) => {
if ( if (
msg.signer && msg.signer &&
(msg.chatMessage?.text || msg.chatMessage?.image) && (msg.chatMessage?.text || msg.chatMessage?.image) &&
@ -94,13 +94,37 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
if (msg.chatMessage?.image) { if (msg.chatMessage?.image) {
img = uintToImgUrl(msg.chatMessage?.image.payload); img = uintToImgUrl(msg.chatMessage?.image.payload);
} }
const date = new Date(msg.chatMessage.clock);
addNewMessageRaw(msg.signer, content, date, id, img); addNewMessageRaw(msg.signer, content, date, id, img);
} }
}, },
[addNewMessageRaw] [addNewMessageRaw]
); );
const loadNextDay = useCallback(
(id: string) => {
if (messenger) {
const endTime = lastLoadTime[id];
const startTime = new Date();
startTime.setDate(endTime.getDate() - 1);
startTime.setHours(0, 0, 0, 0);
messenger.retrievePreviousMessages(
id,
startTime,
endTime,
() => undefined
);
setLastLoadTime((prev) => {
return {
...prev,
[id]: startTime,
};
});
}
},
[lastLoadTime, messenger]
);
useEffect(() => { useEffect(() => {
const createMessenger = async () => { const createMessenger = async () => {
const identity = Identity.generate(); const identity = Identity.generate();
@ -124,29 +148,19 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
}); });
await Promise.all( await Promise.all(
chatIdList.map(async (id) => await messenger.joinChat(id))
);
Promise.all(
chatIdList.map(async (id) => { chatIdList.map(async (id) => {
const today = new Date(); await messenger.joinChat(id);
const yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
yesterday.setHours(0, 0, 0, 0);
setLastLoadTime((prev) => { setLastLoadTime((prev) => {
return { return {
...prev, ...prev,
[id]: yesterday, [id]: new Date(),
}; };
}); });
await messenger.retrievePreviousMessages( messenger.addObserver(
id, (msg, date) => addNewMessage(msg, id, date),
yesterday, id
today,
(messages) => messages.forEach((msg) => addNewMessage(msg, id))
); );
clearNotifications(id); clearNotifications(id);
messenger.addObserver((msg) => addNewMessage(msg, id), id);
}) })
); );
setMessenger(messenger); setMessenger(messenger);
@ -154,40 +168,22 @@ export function useMessenger(chatId: string, chatIdList: string[]) {
createMessenger(); createMessenger();
}, []); }, []);
const loadNextDay = useCallback( useEffect(() => {
(id: string) => { if (messenger) {
if (messenger) { chatIdList.forEach(loadNextDay);
const endTime = lastLoadTime[id]; }
const startTime = new Date(); }, [messenger]);
startTime.setDate(endTime.getDate() - 1);
startTime.setHours(0, 0, 0, 0);
messenger.retrievePreviousMessages(id, startTime, endTime, (messages) =>
messages.forEach((msg) => addNewMessage(msg, id))
);
setLastLoadTime((prev) => {
return {
...prev,
[id]: startTime,
};
});
}
},
[lastLoadTime, messenger]
);
const sendMessage = useCallback( const sendMessage = useCallback(
async (messageText: string, image?: Uint8Array) => { async (messageText: string, image?: Uint8Array) => {
let mediaContent = undefined; const mediaContent = image
if (image) { ? {
mediaContent = { image,
image, imageType: 1,
imageType: 1, contentType: 1,
contentType: 1, }
}; : undefined;
}
await messenger?.sendMessage(messageText, chatId, mediaContent); await messenger?.sendMessage(messageText, chatId, mediaContent);
addNewMessageRaw( addNewMessageRaw(
messenger?.identity.publicKey ?? new Uint8Array(), messenger?.identity.publicKey ?? new Uint8Array(),
messageText, messageText,