Skip to content

Commit

Permalink
Use websocket notify
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Nov 30, 2023
1 parent b52d9ee commit 61258ad
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions app/utils/NotifyApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const notifyAvailable = () => {
&& $STM_Config.notify_service && $STM_Config.notify_service.host;
};

const notifyWsAvailable = () => {
return notifyAvailable() && $STM_Config.notify_service.host_ws
}

const notifyUrl = (pathname) => {
return new URL(pathname, window.$STM_Config.notify_service.host).toString();
};
Expand All @@ -36,6 +40,67 @@ function saveSession(response) {
localStorage.setItem('X-Session', session);
}

async function connectNotifyWs() {
if (!window.notifyWs) {
window.notifyWsReq = { id: 0, requests: {}, callbacks: {} }
await new Promise((resolve, reject) => {
const notifyWs = new WebSocket($STM_Config.notify_service.host_ws)
window.notifyWs = notifyWs

notifyWs.addEventListener('open', () => {
notifyWs.isOpen = true
resolve()
})

notifyWs.addEventListener('сlose', () => {
if (!notifyWs.isOpen) {
const err = new Error('notifyWs - cannot connect')
reject(err)
}
})

notifyWs.addEventListener('message', (msg) => {
console.log('notifyWs message:', msg)
const data = JSON.parse(msg.data)
const id = data.id
const request = window.notifyWsReq.requests[id]
if (request) {
if (data.err) {
request.callback(new Error(data.err.code + ': ' + data.err.msg), data)
return
}
request.callback(null, data.data)
} else if (!id && data.data && data.data.event) {
const { event } = data.data
const callback = window.notifyWsReq.callbacks[event]
if (callback) {
callback.callback(null, data.data)
}
}
})
})
}
}

async function notifyWsSend(api, args, callback = null, eventCallback = null) {
await connectNotifyWs()
const id = window.notifyWsReq.id++
let msg = {
api,
args,
id
}
msg = JSON.stringify(msg)
if (callback) {
window.notifyWsReq.requests[id] = { callback }
}
if (eventCallback) {
const { event, callback } = eventCallback
window.notifyWsReq.callbacks[event] = { callback }
}
window.notifyWs.send(msg)
}

export function notifyApiLogin(account, authSession) {
if (!notifyAvailable()) return;
let request = Object.assign({}, request_base, {
Expand Down Expand Up @@ -71,6 +136,24 @@ export function getNotifications(account) {
});
}

export async function getNotificationsWs(account) {
if (!notifyWsAvailable()) {
console.error('getNotificationsWs - no notify_service.host_ws in config?')
return null
}
return await new Promise(async (resolve, reject) => {
await notifyWsSend('counters', {
account
}, (err, res) => {
if (err) {
reject(err)
return
}
resolve(res.counters)
})
})
}

export function markNotificationRead(account, fields) {
if (!notifyAvailable()) return Promise.resolve(null);
let request = Object.assign({}, request_base, {method: 'put', mode: 'cors'});
Expand All @@ -84,6 +167,59 @@ export function markNotificationRead(account, fields) {
});
}

export async function markNotificationReadWs(account, fields) {
if (!notifyWsAvailable()) return null
const xSession = notifySession()
const scopes = fields.join(',')
return await new Promise(async (resolve, reject) => {
await notifyWsSend('counters/read', {
account,
'X-Session': xSession,
scopes,
}, (err, res) => {
if (err) {
reject(err)
return
}
resolve(res.counters)
})
})
}

export async function counterSubscribeWs(account, callback) {
if (!notifyWsAvailable()) return null
const xSession = notifySession()
return await new Promise(async (resolve, reject) => {
await notifyWsSend('counters/subscribe', {
account,
'X-Session': xSession,
}, (err, res) => {
if (err) {
reject(err)
return
}
resolve(res)
}, callback)
})
}

export async function counterUnsubscribeWs(account) {
if (!notifyWsAvailable()) return null
const xSession = notifySession()
return await new Promise(async (resolve, reject) => {
await notifyWsSend('counters/unsubscribe', {
account,
'X-Session': xSession,
}, (err, res) => {
if (err) {
reject(err)
return
}
resolve(res)
})
})
}

export async function notificationSubscribe(account, scopes = 'message', sidKey = '__subscriber_id') {
if (!notifyAvailable()) return;
if (window[sidKey]) return;
Expand Down Expand Up @@ -172,7 +308,11 @@ export async function notificationTake(account, removeTaskIds, forEach, sidKey =

if (process.env.BROWSER) {
window.getNotifications = getNotifications;
window.getNotificationsWs = getNotificationsWs
window.markNotificationRead = markNotificationRead;
window.markNotificationReadWs = markNotificationReadWs
window.counterSubscribeWs = counterSubscribeWs
window.counterUnsubscribeWs = counterUnsubscribeWs
window.notificationSubscribe = notificationSubscribe;
window.notificationUnsubscribe = notificationUnsubscribe;
window.notificationTake = notificationTake;
Expand Down

0 comments on commit 61258ad

Please sign in to comment.