Moderator notes WIP (#144)

This commit is contained in:
John Livingston 2024-07-30 19:47:20 +02:00
parent 704e660f37
commit 9c2b84027a
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
6 changed files with 121 additions and 6 deletions

View File

@ -76,7 +76,10 @@ export default class MUCNotesView extends DraggablesCustomElement {
})
await this.model.createNote({
description: description
description: description,
about_jid: ev.target.about_jid?.value || undefined,
about_nick: ev.target.about_nick?.value || undefined,
about_occupant_id: ev.target.about_occupant_id?.value || undefined
})
this.closeCreateNoteForm()

View File

@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
import { PubSubManager } from '../../shared/lib/pubsub-manager.js'
export class NotePubSubManager extends PubSubManager {
_additionalModelToData (item, data) {
super._additionalModelToData(item, data)
data.about_jid = item.get('about_jid')
data.about_occupant_id = item.get('about_occupant_id')
data.about_nick = item.get('about_nick')
}
_additionalDataToItemNode (data, item) {
super._additionalDataToItemNode(data, item)
const aboutAttributes = {}
if (data.about_jid !== undefined) {
aboutAttributes.jid = data.about_jid
}
if (data.about_nick !== undefined) {
aboutAttributes.nick = data.about_nick
}
const occupantId = data.about_occupant_id
if (occupantId !== undefined || Object.values(aboutAttributes).length) {
item.c('note-about', aboutAttributes)
if (occupantId) {
item.c('occupant-id', { xmlns: 'urn:xmpp:occupant-id:0', id: occupantId }).up()
}
item.up()
}
}
_additionalParseItemNode (itemNode, type, data) {
super._additionalParseItemNode(itemNode, type, data)
const about = itemNode.querySelector('& > note-about')
if (!about) { return }
data.about_jid = about.getAttribute('jid')
data.about_nick = about.getAttribute('nick')
const occupantIdEl = about.querySelector('& > occupant-id')
if (occupantIdEl) {
data.about_occupant_id = occupantIdEl.getAttribute('id')
}
}
}

View File

@ -22,6 +22,28 @@ class ChatRoomNote extends Model {
async deleteItem () {
return this.collection.chatroom.noteManager.deleteItems([this])
}
getAboutOccupant () {
const occupants = this.collection.chatroom?.occupants
if (!occupants?.findOccupant) { return undefined }
if (this.get('about_occupant_id')) {
const o = occupants.findOccupant({ occupant_id: this.get('about_occupant_id') })
if (o) { return o }
}
if (!this.get('about_nick') && !this.get('about_jid')) {
return undefined
}
const o = occupants.findOccupant({
nick: this.get('about_nick'),
jid: this.get('about_jid')
})
if (o) { return o }
return undefined
}
}
export {

View File

@ -9,10 +9,20 @@ export function tplMucNote (el, note) {
// eslint-disable-next-line no-undef
const i18nDelete = __(LOC_moderator_note_delete)
const aboutOccupant = note.getAboutOccupant()
return !el.edit
? html`
<div draggable="true" class="note-line draggables-line">
<div class="note-description">${note.get('description') ?? ''}</div>
${
aboutOccupant
? html`
<livechat-converse-muc-note-occupant
.model=${aboutOccupant}
></livechat-converse-muc-note-occupant>`
: ''
}
<button class="note-action" title="${__('Edit')}"
@click=${el.toggleEdit}
>
@ -27,6 +37,16 @@ export function tplMucNote (el, note) {
: html`
<div class="note-line draggables-line">
<form class="converse-form" @submit=${el.saveNote}>
${
aboutOccupant
? html`
<livechat-converse-muc-note-occupant
full_display=${true}
.model=${aboutOccupant}
></livechat-converse-muc-note-occupant>
`
: ''
}
${_tplNoteForm(note)}
<fieldset class="form-group">
<input type="submit" class="btn btn-primary" value="${__('Ok')}" />
@ -53,9 +73,9 @@ function _tplNoteForm (note) {
function _tplNoteOccupantFormFields (occupant) {
if (!occupant) { return '' }
return html`
<input type="hidden" name="occupant_nick" value=${occupant.get('nick')} />
<input type="hidden" name="occupant_jid" value=${occupant.get('jid')} />
<input type="hidden" name="occupant_id" value=${occupant.get('occupant_id')} />
<input type="hidden" name="about_nick" value=${occupant.get('nick')} />
<input type="hidden" name="about_jid" value=${occupant.get('jid')} />
<input type="hidden" name="about_occupant_id" value=${occupant.get('occupant_id')} />
`
}

View File

@ -3,7 +3,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { XMLNS_NOTE } from './constants.js'
import { PubSubManager } from '../../shared/lib/pubsub-manager.js'
import { NotePubSubManager } from './note-pubsub-manager.js'
import { converse, _converse, api } from '../../../src/headless/index.js'
import { __ } from 'i18n'
@ -87,7 +87,7 @@ function _initChatRoomNotes (mucModel) {
mucModel.notes = new _converse.exports.ChatRoomNotes(undefined, { chatroom: mucModel })
mucModel.noteManager = new PubSubManager(
mucModel.noteManager = new NotePubSubManager(
mucModel.get('jid'),
'livechat-notes', // the node name
{

View File

@ -123,6 +123,7 @@ export class PubSubManager {
if (v === undefined) { continue }
data[field] = v
}
this._additionalModelToData(item, data)
console.log('Saving item...')
await this._save(type, data, id)
@ -178,6 +179,8 @@ export class PubSubManager {
item.c(fieldName).t(data[fieldName]).up()
}
this._additionalDataToItemNode(data, item)
await api.pubsub.publish(this.roomJID, this.node, item)
}
@ -336,6 +339,7 @@ export class PubSubManager {
}
}
}
this._additionalParseItemNode(itemNode, type, data)
return data
}
@ -351,4 +355,19 @@ export class PubSubManager {
_typeFromCollection (collection) {
return Object.values(this.types).find(type => type.collection === collection)
}
/**
* Overload to add some custom code for model to data conversion.
*/
_additionalModelToData (_item, _data) {}
/**
* Overload to add some custom code for data to stanza conversion.
*/
_additionalDataToItemNode (_data, _item) {}
/**
* Overload to add some custom code item parsing.
*/
_additionalParseItemNode (_itemNode, _type, _data) {}
}