The frontend/UI server, written in JS using the MarkoJS library
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.

158 lines
4.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. const {query} = require("../client")
  2. const {Comment} = require("./Comment")
  3. class Chapter {
  4. /**
  5. * @param {string} id
  6. * @param {string} title
  7. * @param {string} author
  8. * @param {string} source
  9. * @param {string | Date} createdDate
  10. * @param {string | Date} fictionalDate
  11. * @param {string | Date} editedDate
  12. * @param {boolean} canComment
  13. * @param {"Disabled"|"Article"|"Chat"|"Message"} commentMode
  14. * @param {boolean} commentsLocked
  15. * @param {any[]} comments
  16. */
  17. constructor(id, title, author, source, createdDate, fictionalDate, editedDate, canComment, commentMode, commentsLocked, comments) {
  18. this.id = id
  19. this.title = title
  20. this.author = author
  21. this.source = source
  22. this.createdDate = new Date(createdDate)
  23. this.fictionalDate = fictionalDate != null ? new Date(fictionalDate) : null
  24. this.editedDate = new Date(editedDate)
  25. this.canComment = canComment
  26. this.commentMode = commentMode
  27. this.commentsLocked = commentsLocked
  28. this.comments = (comments||[]).map(c => Comment.fromData(c))
  29. }
  30. }
  31. class ChapterAPI {
  32. /**
  33. * Call `addChapter(input)` mutation, returns the entire object.
  34. *
  35. * @param {{id:string, storyId:string, title:string, author?:string, fictionalDate?:Date, source: string}} input
  36. * @returns {Promise<Chapter>}
  37. */
  38. addChapter(input) {
  39. return query(`
  40. mutation AddChapter($input: ChapterAddInput!) {
  41. addChapter(input: $input) {
  42. id
  43. title
  44. author
  45. source
  46. createdDate
  47. fictionalDate
  48. editedDate
  49. canComment
  50. commentMode
  51. commentsLocked
  52. comments {
  53. id
  54. subject
  55. author
  56. characterName
  57. character {
  58. id
  59. name
  60. author
  61. description
  62. }
  63. fictionalDate
  64. createdDate
  65. editedDate
  66. source
  67. }
  68. }
  69. }
  70. `, {input}, {permissions: ["member", "story.add"]}).then((data) => {
  71. const ac = data.addChapter
  72. 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)
  73. })
  74. }
  75. /**
  76. * Call `editChapter(input)` mutation, returns editable fields
  77. *
  78. * @param {{id:string, title:string, fictionalDate:Date, source: string}} input
  79. * @returns {Promise<{title:string, fictionalDate:Date, source:string}>}
  80. */
  81. editChapter(input) {
  82. return query(`
  83. mutation EditChapter($input: ChapterEditInput!) {
  84. editChapter(input: $input) {
  85. title
  86. fictionalDate
  87. source
  88. canComment
  89. commentMode
  90. commentsLocked
  91. comments {
  92. id
  93. subject
  94. author
  95. characterName
  96. character {
  97. id
  98. name
  99. author
  100. description
  101. }
  102. fictionalDate
  103. createdDate
  104. editedDate
  105. source
  106. }
  107. }
  108. }
  109. `, {input}, {permissions: ["member", "story.edit"]}).then(({editChapter}) => {
  110. if (editChapter.fictionalDate != null) {
  111. editChapter.fictionalDate = new Date(editChapter.fictionalDate)
  112. }
  113. return Object.assign({comments:[]}, editChapter)
  114. })
  115. }
  116. /**
  117. * Call `moveChapter(input)` mutation, returns the ID.
  118. *
  119. * @param {{id:string, storyId:string}} input
  120. * @returns {Promise<{id:string}>}
  121. */
  122. moveChapter(input) {
  123. return query(`
  124. mutation MoveChapter($input: ChapterMoveInput!) {
  125. moveChapter(input: $input) {
  126. id
  127. }
  128. }
  129. `, {input}, {permissions: ["member", "story.move"]}).then(({moveChapter}) => {
  130. return moveChapter
  131. })
  132. }
  133. /**
  134. * Call `removeChapter(input)` mutation, returns only the id
  135. *
  136. * @param {{id:string}} input
  137. * @returns {Promise<{id: string}>}
  138. */
  139. removeChapter(input) {
  140. return query(`
  141. mutation RemoveChapter($input: ChapterRemoveInput!) {
  142. removeChapter(input: $input) {
  143. id
  144. }
  145. }
  146. `, {input}, {permissions: ["member", "story.edit"]}).then(({removeChapter}) => {
  147. return removeChapter
  148. })
  149. }
  150. }
  151. module.exports = {Chapter, chapterApi: new ChapterAPI}