Moderator notes WIP (#144):

* Fix notes occupants when unknown
* notes.getAboutOccupant result cache
This commit is contained in:
John Livingston 2024-07-31 22:32:57 +02:00
parent 48763e6173
commit bb2aca71c1
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC

View File

@ -12,6 +12,8 @@ import { Model } from '@converse/skeletor/src/model.js'
*/ */
class ChatRoomNote extends Model { class ChatRoomNote extends Model {
idAttribute = 'id' idAttribute = 'id'
_aboutOccupantCache = null
_aboutOccupantCacheFor = null
async saveItem () { async saveItem () {
console.log('Saving note ' + this.get('id') + '...') console.log('Saving note ' + this.get('id') + '...')
@ -27,22 +29,51 @@ class ChatRoomNote extends Model {
const occupants = this.collection.chatroom?.occupants const occupants = this.collection.chatroom?.occupants
if (!occupants?.findOccupant) { return undefined } if (!occupants?.findOccupant) { return undefined }
if (this.get('about_occupant_id')) { const nick = this.get('about_nick')
const o = occupants.findOccupant({ occupant_id: this.get('about_occupant_id') }) const jid = this.get('about_jid')
if (o) { return o } const occupantId = this.get('about_occupant_id')
}
if (!this.get('about_nick') && !this.get('about_jid')) { if (!nick && !jid && !occupantId) {
this._aboutOccupantCache = null
this._aboutOccupantCacheFor = null
return undefined return undefined
} }
const o = occupants.findOccupant({ // Keeping some cache, to avoid intensive search on each rendering.
nick: this.get('about_nick'), const cacheKey = `${occupantId ?? ''} ${jid ?? ''} ${nick ?? ''}`
jid: this.get('about_jid') if (this._aboutOccupantCacheFor === cacheKey && this._aboutOccupantCache) {
}) return this._aboutOccupantCache
if (o) { return o } }
return undefined this._aboutOccupantCacheFor = cacheKey
if (occupantId) {
const o = occupants.findOccupant({ occupant_id: occupantId })
if (o) {
this._aboutOccupantCache = o
return o
}
}
if (jid) {
const o = occupants.findOccupant({
jid,
nick
})
if (o) {
this._aboutOccupantCache = o
return o
}
}
// If we don't find it, maybe it is a user that has spoken a long time ago (or never spoked).
// In such case, we must create a dummy occupant:
this._aboutOccupantCache = occupants.create({
nick,
occupant_id: occupantId,
jid
})
return this._aboutOccupantCache
} }
} }