Sync webhook profiles to Matrix

This commit is contained in:
Cadence Ember 2024-09-26 02:34:30 +12:00
commit 419d61cf82
3 changed files with 39 additions and 10 deletions

View file

@ -208,6 +208,34 @@ async function syncUser(user, member, channel, guild, roomID) {
return mxid
}
/**
* Sync profile data for a webhook user. The _ooye_webhook name will always be reused.
* 1. Join the sim to the room if needed
* 2. Make an object of what the new room member state content would be, including uploading the profile picture if it hasn't been done before
* 4. Compare against the previously known state content, which is helpfully stored in the database
* 5. If the state content has changed, send it to Matrix and update it in the database for next time
* @param {DiscordTypes.APIUser} user
* @param {DiscordTypes.APIGuildChannel} channel
* @param {DiscordTypes.APIGuild} guild
* @param {string} roomID
* @returns {Promise<string>} mxid of the updated sim
*/
async function syncWebhook(user, channel, guild, roomID) {
const mxid = await ensureSimJoined(user, roomID)
// @ts-ignore
const content = await memberToStateContent(user, {}, guild.id)
const currentHash = _hashProfileContent(content, 0)
const existingHash = select("sim_member", "hashed_profile_content", {room_id: roomID, mxid}).safeIntegers().pluck().get()
// only do the actual sync if the hash has changed since we last looked
if (existingHash !== currentHash) {
// Update room member state
await api.sendState(roomID, "m.room.member", mxid, content, mxid)
// Update cached hash
db.prepare("UPDATE sim_member SET hashed_profile_content = ? WHERE room_id = ? AND mxid = ?").run(currentHash, roomID, mxid)
}
return mxid
}
/**
* @param {string} roomID
*/
@ -244,4 +272,5 @@ module.exports._hashProfileContent = _hashProfileContent
module.exports.ensureSim = ensureSim
module.exports.ensureSimJoined = ensureSimJoined
module.exports.syncUser = syncUser
module.exports.syncWebhook = syncWebhook
module.exports.syncAllUsersInRoom = syncAllUsersInRoom