mirror of
https://gitdab.com/cadence/out-of-your-element.git
synced 2025-09-10 20:32:50 +02:00
d->m: Presence
This commit is contained in:
parent
f98c30cac3
commit
8ad0117fd2
6 changed files with 70 additions and 1 deletions
45
src/d2m/actions/set-presence.js
Normal file
45
src/d2m/actions/set-presence.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
// @ts-check
|
||||
|
||||
const passthrough = require("../../passthrough")
|
||||
const {sync, select} = passthrough
|
||||
/** @type {import("../../matrix/api")} */
|
||||
const api = sync.require("../../matrix/api")
|
||||
|
||||
// Adding a debounce to all updates because events are issued multiple times, once for each guild.
|
||||
// Sometimes a status update is even issued twice in a row for the same user+guild, weird!
|
||||
const presenceDelay = 1500
|
||||
/** @type {Map<string, NodeJS.Timeout>} user ID -> cancelable timeout */
|
||||
const presenceDelayMap = new Map()
|
||||
|
||||
/**
|
||||
* @param {string} userID Discord user ID
|
||||
* @param {string} status status field from Discord's PRESENCE_UPDATE event
|
||||
*/
|
||||
function setPresence(userID, status) {
|
||||
// cancel existing timer if one is already set
|
||||
if (presenceDelayMap.has(userID)) {
|
||||
clearTimeout(presenceDelayMap.get(userID))
|
||||
}
|
||||
// new timer, which will run if nothing else comes in soon
|
||||
presenceDelayMap.set(userID, setTimeout(setPresenceCallback, presenceDelay, userID, status).unref())
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} user_id Discord user ID
|
||||
* @param {string} status status field from Discord's PRESENCE_UPDATE event
|
||||
*/
|
||||
function setPresenceCallback(user_id, status) {
|
||||
presenceDelayMap.delete(user_id)
|
||||
const mxid = select("sim", "mxid", {user_id}).pluck().get()
|
||||
if (!mxid) return
|
||||
const presence =
|
||||
( status === "online" ? "online"
|
||||
: status === "offline" ? "offline"
|
||||
: "unavailable") // idle, dnd, and anything else they dream up in the future
|
||||
api.setPresence(presence, mxid).catch(e => {
|
||||
console.error("d->m: Skipping presence update failure:")
|
||||
console.error(e)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.setPresence = setPresence
|
Loading…
Add table
Add a link
Reference in a new issue