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.

283 lines
7.4 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
  1. const {query} = require("../client")
  2. const {Tag} = require("./Tag")
  3. const {Chapter} = require("./Chapter")
  4. class Story {
  5. /**
  6. * Construct a story object. You should use the API instead of calling this.
  7. *
  8. * @param {string} id
  9. * @param {string} name
  10. * @param {string} author
  11. * @param {"Info"|"News"|"Document"|"Background"|"Story"} category Story's category
  12. * @param {boolean} listed
  13. * @param {boolean} open
  14. * @param {string} createdDate
  15. * @param {string} updatedDate
  16. * @param {string} fictionalDate
  17. * @param {{kind:string, name:string}[]} tags
  18. * @param {Chapter[]} chapters
  19. */
  20. constructor(id, name, author, category, listed, open, createdDate, updatedDate, fictionalDate, tags, chapters) {
  21. this.id = id
  22. this.name = name
  23. this.author = author
  24. this.category = category
  25. this.listed = listed
  26. this.open = open
  27. this.createdDate = new Date(createdDate)
  28. this.updatedDate = new Date(updatedDate)
  29. this.fictionalDate = fictionalDate != null ? new Date(fictionalDate) : null
  30. this.tags = tags.map(dt => new Tag(dt.kind, dt.name))
  31. this.chapters = chapters != null ? chapters.map(c => new Chapter(c.id, c.title, c.author, c.source, c.createdDate, c.fictionalDate, c.editedDate)) : null
  32. }
  33. }
  34. class StoryCategory {
  35. constructor(name, description) {
  36. this.name = name
  37. this.description = description
  38. }
  39. }
  40. const data = {
  41. categories: null
  42. }
  43. /**
  44. * storyApi contains the API queries
  45. */
  46. const storyApi = {
  47. /**
  48. * Call `stories(filter)` query
  49. *
  50. * @param {{author:string, category:string, tags:Tag[]|Tag, unlisted: boolean, open: boolean, earliestFictionalDate:Date|string, latestFictionalDate:Date|string, limit:number}} filter
  51. * @returns {Promise<Story[]>}
  52. */
  53. list(filter = {}, options = {}) {
  54. if (filter.earliestFictionalDate != null && typeof(filter.earliestFictionalDate) !== "string") {
  55. filter.earliestFictionalDate = filter.earliestFictionalDate.toISOString()
  56. }
  57. if (filter.latestFictionalDate != null && typeof(filter.latestFictionalDate) !== "string") {
  58. filter.latestFictionalDate = filter.latestFictionalDate.toISOString()
  59. }
  60. return query(`
  61. query ListStories($filter: StoriesFilter) {
  62. stories(filter: $filter) {
  63. id
  64. name
  65. author
  66. category
  67. tags {
  68. kind
  69. name
  70. }
  71. listed
  72. open
  73. createdDate
  74. fictionalDate
  75. updatedDate
  76. }
  77. }
  78. `, {filter}, options).then(({stories}) => {
  79. return stories.map(d => new Story(d.id, d.name, d.author, d.category, d.listed, d.open, d.createdDate, d.updatedDate, d.fictionalDate, d.tags))
  80. })
  81. },
  82. /**
  83. * @param {string} id
  84. * @returns {Promise<Story>}
  85. */
  86. find(id) {
  87. return query(`
  88. query FindStory($id: String!) {
  89. story(id: $id) {
  90. id
  91. name
  92. author
  93. category
  94. tags {
  95. kind
  96. name
  97. }
  98. listed
  99. open
  100. createdDate
  101. fictionalDate
  102. updatedDate
  103. chapters {
  104. id
  105. title
  106. author
  107. source
  108. createdDate
  109. fictionalDate
  110. editedDate
  111. }
  112. }
  113. }
  114. `, {id}).then(({story}) => {
  115. return new Story(
  116. story.id, story.name, story.author, story.category,
  117. story.listed, story.open,
  118. story.createdDate, story.updatedDate, story.fictionalDate,
  119. story.tags, story.chapters
  120. )
  121. })
  122. },
  123. /**
  124. * Call `__type(name: "StoryCategory")` query and extracts the catogires from it
  125. *
  126. * @returns {Promise<StoryCategory[]>}
  127. */
  128. categories() {
  129. if (data.categories !== null) {
  130. return data.categories
  131. }
  132. return query(`
  133. query ListStoryCategories {
  134. categoryType: __type(name: "StoryCategory") {
  135. enumValues {
  136. name
  137. description
  138. }
  139. }
  140. }
  141. `, {}).then(({categoryType}) => {
  142. data.categories = categoryType.enumValues.map(d => new StoryCategory(d.name, d.description))
  143. return data.categories
  144. })
  145. },
  146. /**
  147. * Call `addStoryTag(input)` mutation, returns the new tag list.
  148. *
  149. * @param {{id:string, tag:Tag}} input
  150. * @returns {Promise<{tags: Tag[]}>}
  151. */
  152. addTag(input) {
  153. return query(`
  154. mutation AddStoryTag($input: StoryTagAddInput!) {
  155. addStoryTag(input:$input) {
  156. tags {
  157. kind
  158. name
  159. }
  160. }
  161. }
  162. `, {input}, {permissions: ["member", "story.edit"]}).then(({addStoryTag}) => {
  163. return addStoryTag
  164. })
  165. },
  166. /**
  167. * Call `removeStoryTag(input)` mutation, returns the new tag list.
  168. *
  169. * @param {{id:string, tag:Tag}} input
  170. * @returns {Promise<{tags: Tag[]}>}
  171. */
  172. removeTag(input) {
  173. return query(`
  174. mutation RemoveStoryTag($input: StoryTagRemoveInput!) {
  175. removeStoryTag(input:$input) {
  176. tags {
  177. kind
  178. name
  179. }
  180. }
  181. }
  182. `, {input}, {permissions: ["member", "story.edit"]}).then(({removeStoryTag}) => {
  183. return removeStoryTag
  184. })
  185. },
  186. /**
  187. * Call `addStory(input)` mutation, returns the ID.
  188. *
  189. * @param {{name:string, category:string, author:string, open:boolean, listed:boolean, fictionalDate:Date, tags:Tag[]}} input
  190. * @returns {Promise<Story>}
  191. */
  192. add(input) {
  193. return query(`
  194. mutation AddStory($input: StoryAddInput!) {
  195. addStory(input:$input) {
  196. id
  197. name
  198. author
  199. category
  200. tags {
  201. kind
  202. name
  203. }
  204. listed
  205. open
  206. createdDate
  207. fictionalDate
  208. updatedDate
  209. chapters {
  210. id
  211. title
  212. author
  213. source
  214. createdDate
  215. fictionalDate
  216. editedDate
  217. }
  218. }
  219. }
  220. `, {input}, {permissions: ["member", "story.edit"]}).then(({addStory}) => {
  221. return new Story(
  222. addStory.id, addStory.name, addStory.author, addStory.category,
  223. addStory.listed, addStory.open,
  224. addStory.createdDate, addStory.updatedDate, addStory.fictionalDate,
  225. addStory.tags, addStory.chapters
  226. )
  227. })
  228. },
  229. /**
  230. * Call `editStory(input)` mutation, returns the changable fields.
  231. *
  232. * @param {{id:string, name:string, category:string, author:string, open:boolean, listed:boolean, fictionalDate:Date, clearFictionalDate:boolean}} input
  233. * @returns {Promise<{id:string, name:string, category:string, author:string, open:boolean, listed:boolean, fictionalDate:Date}>}
  234. */
  235. edit(input) {
  236. return query(`
  237. mutation EditStory($input: StoryEditInput!) {
  238. editStory(input:$input) {
  239. id
  240. name
  241. category
  242. author
  243. open
  244. listed
  245. fictionalDate
  246. }
  247. }
  248. `, {input}, {permissions: ["member", "story.edit"]}).then(({editStory}) => {
  249. return editStory
  250. })
  251. },
  252. /**
  253. * Call `removeStory(input)` mutation, returns the id.
  254. *
  255. * @param {{id:string}} input
  256. * @returns {Promise<{id:string}>}
  257. */
  258. remove(input) {
  259. return query(`
  260. mutation RemoveStory($input: StoryRemoveInput!) {
  261. removeStory(input:$input) {
  262. id
  263. }
  264. }
  265. `, {input}, {permissions: ["member", "story.remove"]}).then(({removeStory}) => {
  266. return removeStory
  267. })
  268. },
  269. }
  270. module.exports = {storyApi, Story}