peertube-plugin-livechat/conversejs/custom/plugins/tasks/task-list.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-05-23 09:42:14 +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 task list.
* @class
* @namespace _converse.ChatRoomTaskList
* @memberof _converse
*/
class ChatRoomTaskList extends Model {
2024-04-30 15:11:10 +00:00
idAttribute = 'id'
2024-04-30 16:30:44 +00:00
getTasks () {
const taskListId = this.get('id')
return this.collection?.chatroom?.tasks?.filter({
list: taskListId
}) ?? []
}
2024-05-05 22:13:20 +00:00
async saveItem () {
console.log('Saving task list ' + this.get('id') + '...')
2024-05-06 15:26:20 +00:00
await this.collection.chatroom.taskManager.saveItem(this)
console.log('Task list ' + this.get('id') + ' saved.')
}
2024-05-05 22:13:20 +00:00
async deleteItem () {
2024-05-06 13:57:42 +00:00
const tasks = this.getTasks()
return this.collection.chatroom.taskManager.deleteItems([...tasks, this])
}
async createTask (data) {
// Cloning data to avoid side effects:
data = Object.assign({}, data)
const name = data?.name
if (!name) { throw new Error('Missing name') }
data.list = this.get('id')
if (!data.order) {
2024-05-09 12:42:07 +00:00
data.order = 0 + Math.max(
2024-05-06 15:26:20 +00:00
0,
...(this.getTasks().map(t => t.get('order') ?? 0).filter(o => !isNaN(o)))
)
2024-05-06 13:57:42 +00:00
}
console.log('Creating task ' + name + '...')
const chatroom = this.collection.chatroom
const tasksCollection = chatroom.tasks
await chatroom.taskManager.createItem(tasksCollection, data)
console.log('Task list ' + name + ' created.')
2024-05-05 22:13:20 +00:00
}
}
export {
ChatRoomTaskList
}