preemptively cache members as we find them

This commit is contained in:
Cadence Ember 2023-08-26 22:50:54 +12:00
parent 3ebfa8e3a7
commit 3f2a8d959c
3 changed files with 12 additions and 2 deletions

View file

@ -80,7 +80,7 @@ async function getMemberFromCacheOrHomeserver(roomID, mxid, api) {
const row = db.prepare("SELECT displayname, avatar_url FROM member_cache WHERE room_id = ? AND mxid = ?").get(roomID, mxid) const row = db.prepare("SELECT displayname, avatar_url FROM member_cache WHERE room_id = ? AND mxid = ?").get(roomID, mxid)
if (row) return row if (row) return row
return api.getStateEvent(roomID, "m.room.member", mxid).then(event => { return api.getStateEvent(roomID, "m.room.member", mxid).then(event => {
db.prepare("INSERT INTO member_cache (room_id, mxid, displayname, avatar_url) VALUES (?, ?, ?, ?)").run(roomID, mxid, event?.displayname || null, event?.avatar_url || null) db.prepare("REPLACE INTO member_cache (room_id, mxid, displayname, avatar_url) VALUES (?, ?, ?, ?)").run(roomID, mxid, event?.displayname || null, event?.avatar_url || null)
return event return event
}).catch(() => { }).catch(() => {
return {displayname: null, avatar_url: null} return {displayname: null, avatar_url: null}

View file

@ -91,3 +91,13 @@ async event => {
const name = event.content.name || null const name = event.content.name || null
db.prepare("UPDATE channel_room SET nick = ? WHERE room_id = ?").run(name, event.room_id) db.prepare("UPDATE channel_room SET nick = ? WHERE room_id = ?").run(name, event.room_id)
})) }))
sync.addTemporaryListener(as, "type:m.room.member", guard("m.room.member",
/**
* @param {Ty.Event.StateOuter<Ty.Event.M_Room_Member>} event
*/
async event => {
if (event.state_key[0] !== "@") return
if (utils.eventSenderIsFromDiscord(event.sender)) return
db.prepare("REPLACE INTO member_cache (room_id, mxid, displayname, avatar_url) VALUES (?, ?, ?, ?)").run(event.room_id, event.sender, event.content.displayname || null, event.content.avatar_url || null)
}))

2
types.d.ts vendored
View file

@ -80,7 +80,7 @@ export namespace Event {
export type M_Room_Member = { export type M_Room_Member = {
membership: string membership: string
display_name?: string displayname?: string
avatar_url?: string avatar_url?: string
} }