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} */ addChapter(input) { return query(` mutation AddChapter($input: ChapterAddInput!) { addChapter(input: $input) { id title author source createdDate fictionalDate editedDate } } `, {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) }) } /** * 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 } } `, {input}, {permissions: ["member", "story.edit"]}).then(({editChapter}) => { if (editChapter.fictionalDate != null) { editChapter.fictionalDate = new Date(editChapter.fictionalDate) } return 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}