propagate and receive last seen app
This commit is contained in:
parent
a495e184dc
commit
0116d817ef
8 changed files with 91 additions and 20 deletions
|
|
@ -441,27 +441,17 @@ class Mediator {
|
||||||
|
|
||||||
this.socket.on(IS_GUN_AUTH, this.isGunAuth)
|
this.socket.on(IS_GUN_AUTH, this.isGunAuth)
|
||||||
|
|
||||||
this.socket.on('SET_LAST_SEEN_APP', async body => {
|
this.socket.on(Action.SET_LAST_SEEN_APP, async body => {
|
||||||
try {
|
try {
|
||||||
await throwOnInvalidToken(body.token)
|
await throwOnInvalidToken(body.token)
|
||||||
await new Promise((res, rej) => {
|
await API.Actions.setLastSeenApp()
|
||||||
getUser()
|
this.socket.emit(Action.SET_LAST_SEEN_APP, {
|
||||||
.get('lastSeenApp')
|
|
||||||
.put(Date.now(), ack => {
|
|
||||||
if (ack.err) {
|
|
||||||
rej(new Error(ack.err))
|
|
||||||
} else {
|
|
||||||
res()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
this.socket.emit('SET_LAST_SEEN_APP', {
|
|
||||||
ok: true,
|
ok: true,
|
||||||
msg: null,
|
msg: null,
|
||||||
origBody: body
|
origBody: body
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.socket.emit('SET_LAST_SEEN_APP', {
|
this.socket.emit(Action.SET_LAST_SEEN_APP, {
|
||||||
ok: false,
|
ok: false,
|
||||||
msg: e.message,
|
msg: e.message,
|
||||||
origBody: body
|
origBody: body
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,8 @@ const Actions = {
|
||||||
SET_AVATAR: "SET_AVATAR",
|
SET_AVATAR: "SET_AVATAR",
|
||||||
SET_DISPLAY_NAME: "SET_DISPLAY_NAME",
|
SET_DISPLAY_NAME: "SET_DISPLAY_NAME",
|
||||||
SET_BIO: "SET_BIO",
|
SET_BIO: "SET_BIO",
|
||||||
DISCONNECT: "DISCONNECT"
|
DISCONNECT: "DISCONNECT",
|
||||||
|
SET_LAST_SEEN_APP: "SET_LAST_SEEN_APP"
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Actions;
|
module.exports = Actions;
|
||||||
|
|
|
||||||
|
|
@ -1186,6 +1186,23 @@ const disconnect = async pub => {
|
||||||
await generateHandshakeAddress()
|
await generateHandshakeAddress()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
const setLastSeenApp = () =>
|
||||||
|
new Promise((res, rej) => {
|
||||||
|
require('../Mediator')
|
||||||
|
.getUser()
|
||||||
|
.get(Key.LAST_SEEN_APP)
|
||||||
|
.put(Date.now(), ack => {
|
||||||
|
if (ack.err) {
|
||||||
|
rej(new Error(ack.err))
|
||||||
|
} else {
|
||||||
|
res()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
__createOutgoingFeed,
|
__createOutgoingFeed,
|
||||||
acceptRequest,
|
acceptRequest,
|
||||||
|
|
@ -1202,5 +1219,6 @@ module.exports = {
|
||||||
generateOrderAddress,
|
generateOrderAddress,
|
||||||
setBio,
|
setBio,
|
||||||
saveSeedBackup,
|
saveSeedBackup,
|
||||||
disconnect
|
disconnect,
|
||||||
|
setLastSeenApp
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -457,6 +457,7 @@ const processChats = debounce(() => {
|
||||||
const Streams = require('../streams')
|
const Streams = require('../streams')
|
||||||
const pubToAvatar = Streams.getPubToAvatar()
|
const pubToAvatar = Streams.getPubToAvatar()
|
||||||
const pubToDn = Streams.getPubToDn()
|
const pubToDn = Streams.getPubToDn()
|
||||||
|
const pubToLastSeenApp = Streams.getPubToLastSeenApp()
|
||||||
const existingOutgoings = /** @type {[string, Outgoing][]} */ (Object.entries(
|
const existingOutgoings = /** @type {[string, Outgoing][]} */ (Object.entries(
|
||||||
getCurrentOutgoings()
|
getCurrentOutgoings()
|
||||||
).filter(([_, o]) => o !== null))
|
).filter(([_, o]) => o !== null))
|
||||||
|
|
@ -468,11 +469,15 @@ const processChats = debounce(() => {
|
||||||
for (const [outID, out] of existingOutgoings) {
|
for (const [outID, out] of existingOutgoings) {
|
||||||
if (typeof pubToAvatar[out.with] === 'undefined') {
|
if (typeof pubToAvatar[out.with] === 'undefined') {
|
||||||
// eslint-disable-next-line no-empty-function
|
// eslint-disable-next-line no-empty-function
|
||||||
Streams.onAvatar(() => {}, out.with)
|
Streams.onAvatar(() => {}, out.with)()
|
||||||
}
|
}
|
||||||
if (typeof pubToDn[out.with] === 'undefined') {
|
if (typeof pubToDn[out.with] === 'undefined') {
|
||||||
// eslint-disable-next-line no-empty-function
|
// eslint-disable-next-line no-empty-function
|
||||||
Streams.onDisplayName(() => {}, out.with)
|
Streams.onDisplayName(() => {}, out.with)()
|
||||||
|
}
|
||||||
|
if (typeof pubToLastSeenApp[out.with] === 'undefined') {
|
||||||
|
// eslint-disable-next-line no-empty-function
|
||||||
|
Streams.onPubToLastSeenApp(() => {}, out.with)()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {ChatMessage[]} */
|
/** @type {ChatMessage[]} */
|
||||||
|
|
@ -496,7 +501,8 @@ const processChats = debounce(() => {
|
||||||
id: out.with + outID,
|
id: out.with + outID,
|
||||||
messages: msgs,
|
messages: msgs,
|
||||||
recipientAvatar: pubToAvatar[out.with] || null,
|
recipientAvatar: pubToAvatar[out.with] || null,
|
||||||
recipientDisplayName: pubToDn[out.with] || null
|
recipientDisplayName: pubToDn[out.with] || null,
|
||||||
|
lastSeenApp: pubToLastSeenApp[out.with] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
newChats.push(chat)
|
newChats.push(chat)
|
||||||
|
|
@ -531,6 +537,7 @@ const onChats = cb => {
|
||||||
Streams.onAvatar(processChats)
|
Streams.onAvatar(processChats)
|
||||||
Streams.onDisplayName(processChats)
|
Streams.onDisplayName(processChats)
|
||||||
Streams.onPubToFeed(processChats)
|
Streams.onPubToFeed(processChats)
|
||||||
|
Streams.onPubToLastSeenApp(processChats)
|
||||||
onChatsSubbed = true
|
onChatsSubbed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,3 +36,5 @@ exports.ORDER_TO_RESPONSE = 'orderToResponse'
|
||||||
exports.BIO = 'bio'
|
exports.BIO = 'bio'
|
||||||
|
|
||||||
exports.SEED_BACKUP = 'seedBackup'
|
exports.SEED_BACKUP = 'seedBackup'
|
||||||
|
|
||||||
|
exports.LAST_SEEN_APP = 'lastSeenApp'
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,7 @@ exports.isChatMessage = item => {
|
||||||
* @prop {ChatMessage[]} messages Sorted from most recent to least recent.
|
* @prop {ChatMessage[]} messages Sorted from most recent to least recent.
|
||||||
* @prop {string|null} recipientDisplayName
|
* @prop {string|null} recipientDisplayName
|
||||||
* @prop {boolean} didDisconnect True if the recipient performed a disconnect.
|
* @prop {boolean} didDisconnect True if the recipient performed a disconnect.
|
||||||
|
* @prop {number|undefined|null} lastSeenApp
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -187,5 +187,8 @@ module.exports = {
|
||||||
getAddresses: require('./addresses').getAddresses,
|
getAddresses: require('./addresses').getAddresses,
|
||||||
onLastSentReqIDs: require('./lastSentReqID').onLastSentReqIDs,
|
onLastSentReqIDs: require('./lastSentReqID').onLastSentReqIDs,
|
||||||
getSentReqIDs: require('./lastSentReqID').getSentReqIDs,
|
getSentReqIDs: require('./lastSentReqID').getSentReqIDs,
|
||||||
PubToIncoming: require('./pubToIncoming')
|
PubToIncoming: require('./pubToIncoming'),
|
||||||
|
|
||||||
|
getPubToLastSeenApp: require('./pubToLastSeenApp').getPubToLastSeenApp,
|
||||||
|
onPubToLastSeenApp: require('./pubToLastSeenApp').on
|
||||||
}
|
}
|
||||||
|
|
|
||||||
49
services/gunDB/contact-api/streams/pubToLastSeenApp.js
Normal file
49
services/gunDB/contact-api/streams/pubToLastSeenApp.js
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
const Key = require('../key')
|
||||||
|
/**
|
||||||
|
* @typedef {Record<string, number|undefined|null>} Timestamps
|
||||||
|
* @typedef {(timestamps: Timestamps) => void} Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @type {Timestamps} */
|
||||||
|
const pubToLastSeenApp = {}
|
||||||
|
|
||||||
|
const getPubToLastSeenApp = () => pubToLastSeenApp
|
||||||
|
|
||||||
|
/** @type {Set<Listener>} */
|
||||||
|
const listeners = new Set()
|
||||||
|
|
||||||
|
const notifyListeners = () => {
|
||||||
|
listeners.forEach(l => l(pubToLastSeenApp))
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @type {Set<string>} */
|
||||||
|
const pubsWithListeners = new Set()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Listener} cb
|
||||||
|
* @param {string=} pub
|
||||||
|
*/
|
||||||
|
const on = (cb, pub) => {
|
||||||
|
listeners.add(cb)
|
||||||
|
cb(pubToLastSeenApp)
|
||||||
|
if (pub && pubsWithListeners.add(pub)) {
|
||||||
|
pubToLastSeenApp[pub] = null;
|
||||||
|
notifyListeners()
|
||||||
|
require('../../Mediator')
|
||||||
|
.getGun()
|
||||||
|
.user(pub)
|
||||||
|
.get(Key.LAST_SEEN_APP)
|
||||||
|
.on(timestamp => {
|
||||||
|
pubToLastSeenApp[pub] = typeof timestamp === 'number' ? timestamp : undefined
|
||||||
|
notifyListeners()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return () => {
|
||||||
|
listeners.delete(cb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getPubToLastSeenApp,
|
||||||
|
on,
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue