2024-05-03 17:28:26 +00:00
|
|
|
import { XMLNS_TASKLIST, XMLNS_TASK } from './constants.js'
|
|
|
|
import { PubSubManager } from '../../shared/lib/pubsub-manager.js'
|
2024-04-30 15:11:10 +00:00
|
|
|
import { converse, _converse, api } from '../../../src/headless/core.js'
|
2024-04-29 14:46:21 +00:00
|
|
|
import { __ } from 'i18n'
|
|
|
|
|
|
|
|
export function getHeadingButtons (view, buttons) {
|
|
|
|
const muc = view.model
|
|
|
|
if (muc.get('type') !== _converse.CHATROOMS_TYPE) {
|
|
|
|
// only on MUC.
|
|
|
|
return buttons
|
|
|
|
}
|
|
|
|
|
2024-04-30 15:11:10 +00:00
|
|
|
if (!muc.tasklists) { // this is defined only if user has access (see initOrDestroyChatRoomTaskLists)
|
2024-04-29 14:46:21 +00:00
|
|
|
return buttons
|
|
|
|
}
|
|
|
|
|
2024-05-03 17:28:26 +00:00
|
|
|
// TODO: use disco to discover the feature.
|
|
|
|
// (if the chat is remote, the server could use a livechat version that does not support this feature)
|
|
|
|
|
2024-04-29 14:46:21 +00:00
|
|
|
// Adding a "Open task list" button.
|
|
|
|
buttons.unshift({
|
2024-04-30 15:11:10 +00:00
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
i18n_text: __(LOC_tasks),
|
2024-04-29 14:46:21 +00:00
|
|
|
handler: async (ev) => {
|
|
|
|
ev.preventDefault()
|
|
|
|
ev.stopPropagation()
|
2024-04-30 07:21:18 +00:00
|
|
|
// opening the muc task lists view:
|
|
|
|
api.modal.show('livechat-converse-muc-task-lists-modal', { model: muc })
|
2024-04-29 14:46:21 +00:00
|
|
|
},
|
|
|
|
a_class: '',
|
|
|
|
icon_class: 'fa-list', // FIXME
|
|
|
|
name: 'muc-tasks'
|
|
|
|
})
|
|
|
|
|
|
|
|
return buttons
|
|
|
|
}
|
2024-04-30 15:11:10 +00:00
|
|
|
|
|
|
|
function _initChatRoomTaskLists (mucModel) {
|
2024-05-03 17:28:26 +00:00
|
|
|
if (mucModel.taskManager) {
|
2024-04-30 15:11:10 +00:00
|
|
|
// already initiliazed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mucModel.tasklists = new _converse.ChatRoomTaskLists(undefined, { chatroom: mucModel })
|
2024-04-30 16:30:44 +00:00
|
|
|
mucModel.tasks = new _converse.ChatRoomTasks(undefined, { chatroom: mucModel })
|
2024-05-02 15:53:08 +00:00
|
|
|
|
2024-05-03 17:28:26 +00:00
|
|
|
mucModel.taskManager = new PubSubManager(
|
|
|
|
mucModel.get('jid'),
|
|
|
|
'livechat-tasks', // the node name
|
|
|
|
{
|
|
|
|
tasklist: {
|
|
|
|
itemTag: 'tasklist',
|
|
|
|
xmlns: XMLNS_TASKLIST,
|
|
|
|
collection: mucModel.tasklists,
|
|
|
|
fields: {
|
|
|
|
name: String
|
|
|
|
}
|
|
|
|
},
|
|
|
|
task: {
|
|
|
|
itemTag: 'task',
|
|
|
|
xmlns: XMLNS_TASK,
|
|
|
|
collection: mucModel.tasks,
|
|
|
|
fields: {
|
2024-05-06 13:57:42 +00:00
|
|
|
name: String,
|
|
|
|
description: String
|
2024-05-03 17:28:26 +00:00
|
|
|
},
|
|
|
|
attributes: {
|
|
|
|
done: Boolean,
|
|
|
|
list: String,
|
|
|
|
order: Number
|
|
|
|
}
|
2024-05-02 15:53:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2024-05-03 17:28:26 +00:00
|
|
|
mucModel.taskManager.start().catch(err => console.log(err))
|
2024-04-30 15:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _destroyChatRoomTaskLists (mucModel) {
|
2024-05-03 17:28:26 +00:00
|
|
|
if (!mucModel.taskManager) { return }
|
|
|
|
|
|
|
|
mucModel.taskManager.stop().catch(err => console.log(err))
|
|
|
|
mucModel.taskManager = undefined
|
2024-04-30 15:11:10 +00:00
|
|
|
|
|
|
|
// mucModel.tasklists.unload() FIXME: add a method to unregister from the pubsub, and empty the tasklist.
|
|
|
|
mucModel.tasklists = undefined
|
2024-04-30 16:30:44 +00:00
|
|
|
mucModel.tasks = undefined
|
2024-04-30 15:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function initOrDestroyChatRoomTaskLists (mucModel) {
|
|
|
|
if (mucModel.get('type') !== _converse.CHATROOMS_TYPE) {
|
|
|
|
// only on MUC.
|
|
|
|
return _destroyChatRoomTaskLists(mucModel)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mucModel.session.get('connection_status') !== converse.ROOMSTATUS.ENTERED) {
|
2024-05-02 15:53:08 +00:00
|
|
|
return _destroyChatRoomTaskLists(mucModel)
|
2024-04-30 15:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const myself = mucModel.getOwnOccupant()
|
|
|
|
if (!myself || !myself.isModerator()) {
|
|
|
|
// User must be moderator
|
|
|
|
return _destroyChatRoomTaskLists(mucModel)
|
|
|
|
}
|
|
|
|
|
|
|
|
return _initChatRoomTaskLists(mucModel)
|
|
|
|
}
|