Skip to content

Commit

Permalink
HF 30 - Private groups - WebSocket ping
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Oct 1, 2024
1 parent 3d2a3de commit bc9ba5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 87 deletions.
100 changes: 22 additions & 78 deletions src/components/pages/Messages.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
19 changes: 10 additions & 9 deletions src/utils/NotifyApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
})

Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit bc9ba5d

Please sign in to comment.