d->m: Sync missed emojis/stickers after restart

This commit is contained in:
Cadence Ember 2024-01-10 23:56:10 +13:00
parent a67708269d
commit 9efd6a49b8
7 changed files with 140 additions and 85 deletions

View file

@ -2,6 +2,7 @@
const assert = require("assert").strict
const DiscordTypes = require("discord-api-types/v10")
const deepEqual = require("deep-equal")
const reg = require("../../matrix/read-registration")
const passthrough = require("../../passthrough")
@ -199,22 +200,33 @@ async function syncSpaceFully(guildID) {
/**
* @param {import("discord-api-types/v10").GatewayGuildEmojisUpdateDispatchData | import("discord-api-types/v10").GatewayGuildStickersUpdateDispatchData} data
* @param {boolean} checkBeforeSync false to always send new state, true to check the current state and only apply if state would change
*/
async function syncSpaceExpressions(data) {
async function syncSpaceExpressions(data, checkBeforeSync) {
// No need for kstate here. Each of these maps to a single state event, which will always overwrite what was there before. I can just send the state event.
const spaceID = select("guild_space", "space_id", {guild_id: data.guild_id}).pluck().get()
if (!spaceID) return
if ("emojis" in data && data.emojis.length) {
const content = await expression.emojisToState(data.emojis)
api.sendState(spaceID, "im.ponies.room_emotes", "moe.cadence.ooye.pack.emojis", content)
/**
* @typedef {import("discord-api-types/v10").GatewayGuildEmojisUpdateDispatchData & import("discord-api-types/v10").GatewayGuildStickersUpdateDispatchData} Expressions
* @param {string} spaceID
* @param {Expressions extends any ? keyof Expressions : never} key
* @param {string} eventKey
* @param {(emojis: any[]) => any} fn
*/
async function update(spaceID, key, eventKey, fn) {
if (!(key in data) || !data[key].length) return
const content = await fn(data[key])
if (checkBeforeSync) {
const existing = await api.getStateEvent(spaceID, "im.ponies.room_emotes", eventKey)
if (deepEqual(existing, content, {strict: true})) return
}
api.sendState(spaceID, "im.ponies.room_emotes", eventKey, content)
}
if ("stickers" in data && data.stickers.length) {
const content = await expression.stickersToState(data.stickers)
api.sendState(spaceID, "im.ponies.room_emotes", "moe.cadence.ooye.pack.stickers", content)
}
update(spaceID, "emojis", "moe.cadence.ooye.pack.emojis", expression.emojisToState)
update(spaceID, "stickers", "moe.cadence.ooye.pack.stickers", expression.stickersToState)
}
module.exports.createSpace = createSpace

View file

@ -42,6 +42,7 @@ const utils = {
client.channels.set(thread.id, thread)
}
if (listen === "full") {
eventDispatcher.checkMissedExpressions(message.d)
eventDispatcher.checkMissedMessages(client, message.d)
}

View file

@ -125,6 +125,16 @@ module.exports = {
}
},
/**
* When logging back in, check if we missed any changes to emojis or stickers. Apply the changes if so.
* @param {import("./discord-client")} client
* @param {DiscordTypes.GatewayGuildCreateDispatchData} guild
*/
async checkMissedExpressions(guild) {
const data = {guild_id: guild.id, ...guild}
createSpace.syncSpaceExpressions(data, true)
},
/**
* Announces to the parent room that the thread room has been created.
* See notes.md, "Ignore MESSAGE_UPDATE and bridge THREAD_CREATE as the announcement"
@ -262,6 +272,6 @@ module.exports = {
* @param {DiscordTypes.GatewayGuildEmojisUpdateDispatchData | DiscordTypes.GatewayGuildStickersUpdateDispatchData} data
*/
async onExpressionsUpdate(client, data) {
await createSpace.syncSpaceExpressions(data)
await createSpace.syncSpaceExpressions(data, false)
}
}