You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
4.3 KiB
159 lines
4.3 KiB
const {query} = require("../client")
|
|
const {Comment} = require("./Comment")
|
|
|
|
class Chapter {
|
|
/**
|
|
* @param {string} id
|
|
* @param {string} title
|
|
* @param {string} author
|
|
* @param {string} source
|
|
* @param {string | Date} createdDate
|
|
* @param {string | Date} fictionalDate
|
|
* @param {string | Date} editedDate
|
|
* @param {boolean} canComment
|
|
* @param {"Disabled"|"Article"|"Chat"|"Message"} commentMode
|
|
* @param {boolean} commentsLocked
|
|
* @param {any[]} comments
|
|
*/
|
|
constructor(id, title, author, source, createdDate, fictionalDate, editedDate, canComment, commentMode, commentsLocked, comments) {
|
|
this.id = id
|
|
this.title = title
|
|
this.author = author
|
|
this.source = source
|
|
this.createdDate = new Date(createdDate)
|
|
this.fictionalDate = fictionalDate != null ? new Date(fictionalDate) : null
|
|
this.editedDate = new Date(editedDate)
|
|
this.canComment = canComment
|
|
this.commentMode = commentMode
|
|
this.commentsLocked = commentsLocked
|
|
this.comments = (comments||[]).map(c => Comment.fromData(c))
|
|
}
|
|
}
|
|
|
|
class ChapterAPI {
|
|
/**
|
|
* Call `addChapter(input)` mutation, returns the entire object.
|
|
*
|
|
* @param {{id:string, storyId:string, title:string, author?:string, fictionalDate?:Date, source: string}} input
|
|
* @returns {Promise<Chapter>}
|
|
*/
|
|
addChapter(input) {
|
|
return query(`
|
|
mutation AddChapter($input: ChapterAddInput!) {
|
|
addChapter(input: $input) {
|
|
id
|
|
title
|
|
author
|
|
source
|
|
createdDate
|
|
fictionalDate
|
|
editedDate
|
|
canComment
|
|
commentMode
|
|
commentsLocked
|
|
comments {
|
|
id
|
|
subject
|
|
author
|
|
characterName
|
|
character {
|
|
id
|
|
name
|
|
author
|
|
description
|
|
}
|
|
fictionalDate
|
|
createdDate
|
|
editedDate
|
|
source
|
|
}
|
|
}
|
|
}
|
|
`, {input}, {permissions: ["member", "story.add"]}).then((data) => {
|
|
const ac = data.addChapter
|
|
|
|
return new Chapter(ac.id, ac.title, ac.author, ac.source, ac.createdDate, ac.fictionalDate, ac.editedDate, ac.canComment, ac.commentMode, ac.commentsLocked, ac.comments)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Call `editChapter(input)` mutation, returns editable fields
|
|
*
|
|
* @param {{id:string, title:string, fictionalDate:Date, source: string}} input
|
|
* @returns {Promise<{title:string, fictionalDate:Date, source:string}>}
|
|
*/
|
|
editChapter(input) {
|
|
return query(`
|
|
mutation EditChapter($input: ChapterEditInput!) {
|
|
editChapter(input: $input) {
|
|
title
|
|
fictionalDate
|
|
source
|
|
canComment
|
|
commentMode
|
|
commentsLocked
|
|
comments {
|
|
id
|
|
subject
|
|
author
|
|
characterName
|
|
character {
|
|
id
|
|
name
|
|
author
|
|
description
|
|
}
|
|
fictionalDate
|
|
createdDate
|
|
editedDate
|
|
source
|
|
}
|
|
}
|
|
}
|
|
`, {input}, {permissions: ["member", "story.edit"]}).then(({editChapter}) => {
|
|
if (editChapter.fictionalDate != null) {
|
|
editChapter.fictionalDate = new Date(editChapter.fictionalDate)
|
|
}
|
|
|
|
return Object.assign({comments:[]}, editChapter)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Call `moveChapter(input)` mutation, returns the ID.
|
|
*
|
|
* @param {{id:string, storyId:string}} input
|
|
* @returns {Promise<{id:string}>}
|
|
*/
|
|
moveChapter(input) {
|
|
return query(`
|
|
mutation MoveChapter($input: ChapterMoveInput!) {
|
|
moveChapter(input: $input) {
|
|
id
|
|
}
|
|
}
|
|
`, {input}, {permissions: ["member", "story.move"]}).then(({moveChapter}) => {
|
|
return moveChapter
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Call `removeChapter(input)` mutation, returns only the id
|
|
*
|
|
* @param {{id:string}} input
|
|
* @returns {Promise<{id: string}>}
|
|
*/
|
|
removeChapter(input) {
|
|
return query(`
|
|
mutation RemoveChapter($input: ChapterRemoveInput!) {
|
|
removeChapter(input: $input) {
|
|
id
|
|
}
|
|
}
|
|
`, {input}, {permissions: ["member", "story.edit"]}).then(({removeChapter}) => {
|
|
return removeChapter
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = {Chapter, chapterApi: new ChapterAPI}
|