2024-07-29 16:58:02 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { Model } from '@converse/skeletor/src/model.js'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A chat room note.
|
|
|
|
* @class
|
|
|
|
* @namespace _converse.exports.ChatRoomNote
|
|
|
|
* @memberof _converse
|
|
|
|
*/
|
|
|
|
class ChatRoomNote extends Model {
|
|
|
|
idAttribute = 'id'
|
2024-07-31 20:32:57 +00:00
|
|
|
_aboutOccupantCache = null
|
|
|
|
_aboutOccupantCacheFor = null
|
2024-07-29 16:58:02 +00:00
|
|
|
|
|
|
|
async saveItem () {
|
|
|
|
console.log('Saving note ' + this.get('id') + '...')
|
|
|
|
await this.collection.chatroom.noteManager.saveItem(this)
|
|
|
|
console.log('Note ' + this.get('id') + ' saved.')
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteItem () {
|
|
|
|
return this.collection.chatroom.noteManager.deleteItems([this])
|
|
|
|
}
|
2024-07-30 17:47:20 +00:00
|
|
|
|
|
|
|
getAboutOccupant () {
|
|
|
|
const occupants = this.collection.chatroom?.occupants
|
|
|
|
if (!occupants?.findOccupant) { return undefined }
|
|
|
|
|
2024-07-31 20:32:57 +00:00
|
|
|
const nick = this.get('about_nick')
|
|
|
|
const jid = this.get('about_jid')
|
|
|
|
const occupantId = this.get('about_occupant_id')
|
2024-07-30 17:47:20 +00:00
|
|
|
|
2024-07-31 20:32:57 +00:00
|
|
|
if (!nick && !jid && !occupantId) {
|
|
|
|
this._aboutOccupantCache = null
|
|
|
|
this._aboutOccupantCacheFor = null
|
2024-07-30 17:47:20 +00:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:32:57 +00:00
|
|
|
// Keeping some cache, to avoid intensive search on each rendering.
|
|
|
|
const cacheKey = `${occupantId ?? ''} ${jid ?? ''} ${nick ?? ''}`
|
|
|
|
if (this._aboutOccupantCacheFor === cacheKey && this._aboutOccupantCache) {
|
|
|
|
return this._aboutOccupantCache
|
|
|
|
}
|
2024-07-30 17:47:20 +00:00
|
|
|
|
2024-07-31 20:32:57 +00:00
|
|
|
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
|
2024-07-30 17:47:20 +00:00
|
|
|
}
|
2024-07-29 16:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
ChatRoomNote
|
|
|
|
}
|