2024-08-25 13:34:46 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const {db, from} = require("../passthrough")
|
|
|
|
const reg = require("./read-registration")
|
|
|
|
const ks = require("./kstate")
|
|
|
|
const {applyKStateDiffToRoom, roomToKState} = require("../d2m/actions/create-room")
|
|
|
|
|
2024-08-27 14:53:27 +00:00
|
|
|
/** 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() {
|
2024-09-01 03:32:41 +00:00
|
|
|
return from("member_cache")
|
|
|
|
.join("member_power", "mxid")
|
|
|
|
.join("channel_room", "room_id") // only include rooms that are bridged
|
2024-08-27 14:53:27 +00:00
|
|
|
.and("where member_power.room_id = '*' and member_cache.power_level != member_power.power_level")
|
|
|
|
.selectUnsafe("mxid", "member_cache.room_id", "member_power.power_level")
|
|
|
|
.all()
|
|
|
|
}
|
2024-08-25 13:34:46 +00:00
|
|
|
|
2024-08-27 14:53:27 +00:00
|
|
|
async function applyPower() {
|
2024-09-01 03:26:25 +00:00
|
|
|
// Migrate reg.ooye.invite setting to database
|
|
|
|
for (const mxid of reg.ooye.invite) {
|
|
|
|
db.prepare("INSERT OR IGNORE INTO member_power (mxid, room_id, power_level) VALUES (?, ?, 100)").run(mxid, "*")
|
|
|
|
}
|
|
|
|
|
2024-08-27 14:53:27 +00:00
|
|
|
const rows = _getAffectedRooms()
|
2024-08-25 13:34:46 +00:00
|
|
|
for (const row of rows) {
|
|
|
|
const kstate = await 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)
|
|
|
|
// 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)
|
|
|
|
}
|
2024-08-27 14:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports._getAffectedRooms = _getAffectedRooms
|
|
|
|
module.exports.applyPower = applyPower
|