Task lists WIP:

* fix mod pubsub
* front-end WIP
This commit is contained in:
John Livingston
2024-05-02 17:53:08 +02:00
parent 82b741b4fc
commit ff976ee0ad
10 changed files with 88 additions and 20 deletions

View File

@ -0,0 +1 @@
export const XMLNS_TASKLIST = 'https://livingston.frama.io/peertube-plugin-livechat/protocol/tasklist'

View File

@ -1,11 +1,14 @@
import { CustomElement } from 'shared/components/element.js'
import { api } from '@converse/headless/core'
import tplMucTaskLists from './templates/muc-task-lists'
import { __ } from 'i18n'
export default class MUCTaskListsView extends CustomElement {
static get properties () {
return {
model: { type: Object, attribute: true }
model: { type: Object, attribute: true },
new_task_list_name: { type: String, attribute: false },
create_tasklist_error_message: { type: String, attribute: false }
}
}
@ -20,7 +23,29 @@ export default class MUCTaskListsView extends CustomElement {
}
render () {
return tplMucTaskLists(this.model)
return tplMucTaskLists(this, this.model)
}
async submitCreateTaskList (ev) {
ev.preventDefault()
const name = ev.target.name.value.trim()
if (this.create_tasklist_error_message) {
this.create_tasklist_error_message = ''
}
if ((name ?? '') === '') { return }
try {
await this.model.createTaskList({
name
})
this.new_task_list_name = ''
} catch (err) {
console.error(err)
// eslint-disable-next-line no-undef
this.create_tasklist_error_message = __(LOC_task_list_create_error)
}
}
}

View File

@ -1,7 +1,10 @@
import { Collection } from '@converse/skeletor/src/collection.js'
import { ChatRoomTaskList } from './task-list'
import { XMLNS_TASKLIST } from './constants'
import { initStorage } from '@converse/headless/utils/storage.js'
import { getUniqueId } from '@converse/headless/utils/core.js'
import { converse, api } from '@converse/headless/core'
const { $build } = converse.env
/**
* A list of {@link _converse.ChatRoomTaskList} instances, representing task lists associated to a MUC.
@ -75,6 +78,15 @@ class ChatRoomTaskLists extends Collection {
})
}
}
async createTaskList (data) {
const name = data?.name
if (!name) { throw new Error('Missing name') }
const item = $build('item').c('tasklist', { xmlns: XMLNS_TASKLIST })
item.c('name').t(name)
await api.pubsub.publish(this.chatroom.get('jid'), 'livechat-tasks', item)
}
}
export {

View File

@ -2,7 +2,7 @@ import { html } from 'lit'
import { repeat } from 'lit/directives/repeat.js'
import { __ } from 'i18n'
export default function tplMucTaskLists (tasklists) {
export default function tplMucTaskLists (el, tasklists) {
if (!tasklists) { // if user losed rights
return html`` // FIXME: add a message like "you dont have access"?
}
@ -14,24 +14,17 @@ export default function tplMucTaskLists (tasklists) {
const i18nTaskListName = __(LOC_task_list_name)
return html`
<form class="converse-form" @submit=${(ev) => {
ev.preventDefault()
const name = ev.target.name.value.trim()
if ((name ?? '') === '') { return }
ev.target.name.value = ''
tasklists.create({
name
})
}}
>
<form class="converse-form" @submit=${el.submitCreateTaskList}>
<div class="form-group">
<label>
${i18nCreateTaskList}
<input type="text" value="" name="name" placeholder="${i18nTaskListName}" />
<input type="text" value="${el.new_task_list_name}" name="name" placeholder="${i18nTaskListName}" />
</label>
<input type="submit" value="${i18nAdd}" class="btn btn-primary" />
${!el.create_tasklist_error_message
? ''
: html`<div class="invalid-feedback d-block">${el.create_tasklist_error_message}</div>`
}
</div>
</form>
<div class="">

View File

@ -1,5 +1,6 @@
import { converse, _converse, api } from '../../../src/headless/core.js'
import { __ } from 'i18n'
const { Strophe, $iq } = converse.env
export function getHeadingButtons (view, buttons) {
const muc = view.model
@ -38,6 +39,32 @@ function _initChatRoomTaskLists (mucModel) {
mucModel.tasklists = new _converse.ChatRoomTaskLists(undefined, { chatroom: mucModel })
mucModel.tasks = new _converse.ChatRoomTasks(undefined, { chatroom: mucModel })
// Requesting all items.
const stanza = $iq({
type: 'get',
from: _converse.bare_jid,
to: mucModel.get('jid')
}).c('pubsub', { xmlns: Strophe.NS.PUBSUB })
.c('items', { node: 'livechat-tasks' })
api.sendIQ(stanza).then(
(iq) => {
console.debug('task lists: ', iq)
},
(iq) => {
if (iq === null || !iq?.querySelector) {
console.error('Failed to retrieve tasks', iq)
return
}
if (!iq.querySelector('error[type="cancel"] item-not-found')) {
console.error('Failed to retrieve tasks:', iq)
return
}
// This is totally normal when you open an empty task list.
console.log('Not livechat-tasks node for now')
}
)
}
function _destroyChatRoomTaskLists (mucModel) {
@ -55,7 +82,7 @@ export function initOrDestroyChatRoomTaskLists (mucModel) {
}
if (mucModel.session.get('connection_status') !== converse.ROOMSTATUS.ENTERED) {
_destroyChatRoomTaskLists(mucModel)
return _destroyChatRoomTaskLists(mucModel)
}
const myself = mucModel.getOwnOccupant()