Use kstate for d->m pins updates

This commit is contained in:
Cadence Ember 2025-01-08 11:31:43 +13:00
commit 6e55061760
8 changed files with 50 additions and 42 deletions

View file

@ -6,7 +6,7 @@ const assert = require("assert").strict
const fetch = require("node-fetch").default
const passthrough = require("../passthrough")
const { discord, sync, db } = passthrough
const {sync} = passthrough
/** @type {import("./mreq")} */
const mreq = sync.require("./mreq")
/** @type {import("./txnid")} */

View file

@ -8,6 +8,8 @@ const passthrough = require("../passthrough")
const {sync} = passthrough
/** @type {import("./file")} */
const file = sync.require("./file")
/** @type {import("./api")} */
const api = sync.require("./api")
/** Mutates the input. Not recursive - can only include or exclude entire state events. */
function kstateStripConditionals(kstate) {
@ -102,8 +104,32 @@ function diffKState(actual, target) {
return diff
}
/* c8 ignore start */
/**
* Async because it gets all room state from the homeserver.
* @param {string} roomID
*/
async function roomToKState(roomID) {
const root = await api.getAllState(roomID)
return stateToKState(root)
}
/**
* @param {string} roomID
* @param {any} kstate
*/
async function applyKStateDiffToRoom(roomID, kstate) {
const events = await kstateToState(kstate)
return Promise.all(events.map(({type, state_key, content}) =>
api.sendState(roomID, type, state_key, content)
))
}
module.exports.kstateStripConditionals = kstateStripConditionals
module.exports.kstateUploadMxc = kstateUploadMxc
module.exports.kstateToState = kstateToState
module.exports.stateToKState = stateToKState
module.exports.diffKState = diffKState
module.exports.roomToKState = roomToKState
module.exports.applyKStateDiffToRoom = applyKStateDiffToRoom

View file

@ -3,7 +3,6 @@
const {db, from} = require("../passthrough")
const {reg} = require("./read-registration")
const ks = require("./kstate")
const {applyKStateDiffToRoom, roomToKState} = require("../d2m/actions/create-room")
/** Apply global power level requests across ALL rooms where the member cache entry exists but the power level has not been applied yet. */
function _getAffectedRooms() {
@ -23,9 +22,9 @@ async function applyPower() {
const rows = _getAffectedRooms()
for (const row of rows) {
const kstate = await roomToKState(row.room_id)
const kstate = await ks.roomToKState(row.room_id)
const diff = ks.diffKState(kstate, {"m.room.power_levels/": {users: {[row.mxid]: row.power_level}}})
await applyKStateDiffToRoom(row.room_id, diff)
await ks.applyKStateDiffToRoom(row.room_id, diff)
// There is a listener on m.room.power_levels to do this same update,
// but we update it here anyway since the homeserver does not always deliver the event round-trip.
db.prepare("UPDATE member_cache SET power_level = ? WHERE room_id = ? AND mxid = ?").run(row.power_level, row.room_id, row.mxid)