Update Discord libraries

This commit is contained in:
Cadence Ember
2026-01-21 14:33:24 +13:00
parent 345b7d6135
commit 90fcbd0ddc
7 changed files with 59 additions and 56 deletions

View File

@@ -34,7 +34,7 @@ async function updatePins(channelID, roomID, convertedTimestamp) {
throw e
}
const kstate = await ks.roomToKState(roomID)
const kstate = await ks.roomToKState(roomID, [["m.room.pinned_events", ""]])
const pinned = pinsToList.pinsToList(discordPins, kstate)
const diff = ks.diffKState(kstate, {"m.room.pinned_events/": {pinned}})

View File

@@ -3,10 +3,11 @@
const {select} = require("../../passthrough")
/**
* @param {import("discord-api-types/v10").RESTGetAPIChannelPinsResult} pins
* @param {import("discord-api-types/v10").RESTGetAPIChannelMessagesPinsResult} pins
* @param {{"m.room.pinned_events/"?: {pinned?: string[]}}} kstate
*/
function pinsToList(pins, kstate) {
/** Most recent last. */
let alreadyPinned = kstate["m.room.pinned_events/"]?.pinned || []
// If any of the already pinned messages are bridged messages then remove them from the already pinned list.
@@ -15,13 +16,13 @@ function pinsToList(pins, kstate) {
// * Matrix-only unbridged messages that are pinned will remain pinned.
alreadyPinned = alreadyPinned.filter(event_id => {
const messageID = select("event_message", "message_id", {event_id}).pluck().get()
return !messageID || pins.find(m => m.id === messageID) // if it is bridged then remove it from the filter
return !messageID || pins.items.find(m => m.message.id === messageID) // if it is bridged then remove it from the filter
})
/** @type {string[]} */
const result = []
for (const message of pins) {
const eventID = select("event_message", "event_id", {message_id: message.id, part: 0}).pluck().get()
for (const pin of pins.items) {
const eventID = select("event_message", "event_id", {message_id: pin.message.id, part: 0}).pluck().get()
if (eventID && !alreadyPinned.includes(eventID)) result.push(eventID)
}
result.reverse()

View File

@@ -1,6 +1,7 @@
const {test} = require("supertape")
const data = require("../../../test/data")
const {pinsToList} = require("./pins-to-list")
const mixin = require("@cloudrac3r/mixin-deep")
test("pins2list: converts known IDs, ignores unknown IDs", t => {
const result = pinsToList(data.pins.faked, {})
@@ -46,7 +47,9 @@ test("pins2list: already pinned unknown items are not moved", t => {
})
test("pins2list: bridged messages can be unpinned", t => {
const result = pinsToList(data.pins.faked.slice(0, -2), {
const shortPins = mixin({}, data.pins.faked)
shortPins.items = shortPins.items.slice(0, -2)
const result = pinsToList(shortPins, {
"m.room.pinned_events/": {
pinned: [
"$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA",

View File

@@ -140,10 +140,20 @@ function diffKState(actual, target) {
/**
* Async because it gets all room state from the homeserver.
* @param {string} roomID
* @param {[type: string, key: string][]} [limitToEvents]
*/
async function roomToKState(roomID) {
const root = await api.getAllState(roomID)
return stateToKState(root)
async function roomToKState(roomID, limitToEvents) {
if (!limitToEvents) {
const root = await api.getAllState(roomID)
return stateToKState(root)
} else {
const root = []
await Promise.all(limitToEvents.map(async ([type, key]) => {
const outer = await api.getStateEventOuter(roomID, type, key)
root.push(outer)
}))
return stateToKState(root)
}
}
/**