From bc9ba5d16ab1972c61a62562571db232c6527603 Mon Sep 17 00:00:00 2001 From: 1aerostorm Date: Tue, 1 Oct 2024 16:43:19 +0300 Subject: [PATCH] HF 30 - Private groups - WebSocket ping --- src/components/pages/Messages.jsx | 100 +++++++----------------------- src/utils/NotifyApiClient.js | 19 +++--- 2 files changed, 32 insertions(+), 87 deletions(-) diff --git a/src/components/pages/Messages.jsx b/src/components/pages/Messages.jsx index 9a4e7137..23f8af5a 100644 --- a/src/components/pages/Messages.jsx +++ b/src/components/pages/Messages.jsx @@ -32,7 +32,7 @@ import { getRoleInGroup, opGroup } from 'app/utils/groups' import { getProfileImage, } from 'app/utils/NormalizeProfile'; import { normalizeContacts, normalizeMessages } from 'app/utils/Normalizators'; import { fitToPreview } from 'app/utils/ImageUtils'; -import { notificationSubscribe, notificationSubscribeWs, +import { notificationSubscribe, notificationSubscribeWs, notifyWsPing, notificationShallowUnsubscribe, notificationTake, queueWatch, sendOffchainMessage } from 'app/utils/NotifyApiClient'; import { flash, unflash } from 'app/components/elements/messages/FlashTitle'; import { addShortcut } from 'app/utils/app/ShortcutUtils' @@ -200,83 +200,6 @@ class Messages extends React.Component { return false } - async setCallback0(username, removeTaskIds) { - if (process.env.NO_NOTIFY) { // config-overrides.js, yarn run dev - return - } - if (this.checkLoggedOut(username)) return - if (this.paused) { - setTimeout(() => { - this.setCallback(username, removeTaskIds) - }, 250) - return - } - let subscribed = null; - try { - subscribed = await notificationSubscribe(username); - } catch (ex) { - console.error('notificationSubscribe', ex); - this.notifyErrorsInc(15); - setTimeout(() => { - this.setCallback(username, removeTaskIds); - }, 5000); - return; - } - if (subscribed) { // if was not already subscribed - this.notifyErrorsClear(); - } - if (this.checkLoggedOut(username)) return - const watched = this.watchGroup(this.props.to) - try { - this.notifyAbort = new fetchEx.AbortController() - window.notifyAbort = this.notifyAbort - const takeResult = await notificationTake(username, removeTaskIds, (type, op, timestamp, task_id) => { - const isDonate = type === 'donate' - const toAcc = this.getToAcc() - const group = opGroup(op) - let updateMessage = group === this.state.to || (!group && (op.from === toAcc || - op.to === toAcc)) - const isMine = username === op.from; - if (type === 'private_message') { - if (op.update) { - this.props.messageEdited(op, timestamp, updateMessage, isMine); - } else if (this.nonce !== op.nonce) { - this.props.messaged(op, timestamp, updateMessage, isMine); - this.nonce = op.nonce - if (!isMine && !this.windowFocused) { - this.flashMessage(); - } - } - } else if (type === 'private_delete_message') { - this.props.messageDeleted(op, updateMessage, isMine); - } else if (type === 'private_mark_message') { - this.props.messageRead(op, timestamp, updateMessage, isMine); - } else if (isDonate) { - this.props.messageDonated(op, updateMessage, isMine) - } - }, this.notifyAbort); - removeTaskIds = takeResult.removeTaskIds - window.__lastTake = takeResult.__lastTake - setTimeout(() => { - this.setCallback(username, removeTaskIds); - }, 250); - } catch (ex) { - console.error('notificationTake', ex); - this.notifyErrorsInc(3); - let delay = 2000 - if (ex.message.includes('No such queue')) { - console.log('notificationTake: resubscribe forced...') - notificationShallowUnsubscribe() - delay = 250 - } - setTimeout(() => { - this.setCallback(username, removeTaskIds) - }, delay); - return; - } - if (watched) this.notifyErrorsClear(); - } - async setCallback(username, removeTaskIds) { if (process.env.NO_NOTIFY) { // config-overrides.js, yarn run dev return @@ -329,6 +252,27 @@ class Messages extends React.Component { }, 5000) return } + this.notifyErrorsClear() + const ping = async (firstCall = false) => { + if (!firstCall) { + try { + await notifyWsPing() + if (this.state.notifyErrors) { + // Queue can be cleared by Notify + setTimeout(() => { + this.setCallback(username) + }, 100) + return + } + this.notifyErrorsClear() + } catch (err) { + console.error('Notify ping failed', err) + this.notifyErrorsInc(10) + } + } + setTimeout(ping, 10000) + } + ping(true) } componentDidMount() { diff --git a/src/utils/NotifyApiClient.js b/src/utils/NotifyApiClient.js index d998273c..ddfe95ba 100644 --- a/src/utils/NotifyApiClient.js +++ b/src/utils/NotifyApiClient.js @@ -62,23 +62,16 @@ async function connectNotifyWs() { notifyWs.addEventListener('open', () => { notifyWs.isOpen = true clearTimeout(timeout) - setInterval(() => { - console.log('NOTW', notifyWs.readyState) - //notifyWs.send('_heartbeat') - }, 1000) resolve() }) - notifyWs.addEventListener('error', () => { - alert('NOTW close') - }) - notifyWs.addEventListener('сlose', () => { - alert('NOTW close') if (!notifyWs.isOpen) { clearTimeout(timeout) const err = new Error('notifyWs - cannot connect') reject(err) + } else { + console.log('NOTW close') } }) @@ -138,6 +131,14 @@ async function notifyWsSend(api, args, callback = null, eventCallback = null) { } } +export async function notifyWsPing() { + await connectNotifyWs() + if (!window.notifyWs || window.notifyWs.readyState !== 1) { + throw new Error('Ping detected what Notify WS not ready') + } + window.notifyWs.send(JSON.stringify({ ping: 1 })) +} + export function notifyApiLogin(account, authSession) { if (!notifyAvailable()) return; let request = Object.assign({}, request_base, {