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.

256 lines
5.7 KiB

  1. const {query, Subscription} = require("../client")
  2. const {Character} = require("./Character")
  3. const {Channel} = require("./Channel")
  4. const {Post} = require("./Post")
  5. class Log {
  6. /**
  7. * @param {string} id
  8. * @param {string} shortId
  9. * @param {Date | string | number} date
  10. * @param {string} title
  11. * @param {string} eventName
  12. * @param {string} description
  13. * @param {string} open
  14. * @param {any} channel
  15. * @param {any[]} characters
  16. * @param {any[]} posts
  17. */
  18. constructor(id, shortId, date, title, eventName, description, open, channel, characters, posts) {
  19. this.id = id
  20. this.shortId = shortId
  21. this.date = date
  22. this.title = title
  23. this.eventName = eventName
  24. this.description = description
  25. this.open = open
  26. this.channel = Channel.fromData(channel)
  27. this.characters = characters.map(c => Character.fromData(c))
  28. this.posts = posts.map(p => Post.fromData(p))
  29. }
  30. static fromData(data) {
  31. return new Log(data.id, data.shortId, data.date, data.title, data.eventName, data.description, data.open, data.channel, data.characters, data.posts)
  32. }
  33. }
  34. class LogAPI {
  35. find(id) {
  36. return query(`
  37. query FindLog($id: String!) {
  38. log(id: $id) {
  39. id
  40. shortId
  41. date
  42. title
  43. eventName
  44. description
  45. open
  46. channel {
  47. name
  48. logged
  49. hub
  50. eventName
  51. locationName
  52. }
  53. characters {
  54. id
  55. nicks
  56. author
  57. name
  58. shortName
  59. description
  60. }
  61. posts {
  62. id
  63. position
  64. time
  65. kind
  66. nick
  67. text
  68. }
  69. }
  70. }
  71. `, {id}).then(({log}) => {
  72. return Log.fromData(log)
  73. })
  74. }
  75. /**
  76. * Call `addLog(input)` mutation, returns a Log
  77. *
  78. * @param {{time:Date, channel:string, title:string, eventName:string, description:string, open:boolean}} input
  79. * @returns {Promise<Log>}
  80. */
  81. add(input) {
  82. return query(`
  83. mutation FindLog($input: LogAddInput!) {
  84. addLog(input: $input) {
  85. id
  86. shortId
  87. date
  88. title
  89. eventName
  90. description
  91. open
  92. channel {
  93. name
  94. logged
  95. hub
  96. eventName
  97. locationName
  98. }
  99. characters {
  100. id
  101. nicks
  102. author
  103. name
  104. shortName
  105. description
  106. }
  107. posts {
  108. id
  109. position
  110. time
  111. kind
  112. nick
  113. text
  114. }
  115. }
  116. }
  117. `, {input}, {permissions: ["log.add "]}).then(({addLog}) => {
  118. return Log.fromData(addLog)
  119. })
  120. }
  121. /**
  122. * Call `importLog(input)` mutation, returns multpile logs.
  123. *
  124. * @param {{importer:"MircLike"|"ForumLog", date?:Date|string|number, channel:string, timezone:string, data:string}} input
  125. * @returns {Promise<Log[]>}
  126. */
  127. import(input) {
  128. return query(`
  129. mutation ImportLog($input: LogImportInput!) {
  130. importLog(input: $input) {
  131. id
  132. shortId
  133. date
  134. title
  135. eventName
  136. description
  137. open
  138. channel {
  139. name
  140. logged
  141. hub
  142. eventName
  143. locationName
  144. }
  145. characters {
  146. id
  147. nicks
  148. author
  149. name
  150. shortName
  151. description
  152. }
  153. posts {
  154. id
  155. position
  156. time
  157. kind
  158. nick
  159. text
  160. }
  161. }
  162. }
  163. `, {input}, {permissions: ["log.add"]}).then(({importLog}) => {
  164. return importLog.map(d => Log.fromData(d))
  165. })
  166. }
  167. /**
  168. * Call `editLog(input)` mutation, returns the id and all editable fields.
  169. *
  170. * @param {{id:string, title:string, eventName:string, description:string, open:boolean}} input
  171. * @returns {Promise<{id:string, title:string, eventName:string, description:string, open:boolean}>}
  172. */
  173. edit(input) {
  174. return query(`
  175. mutation EditLog($input: LogEditInput!) {
  176. editLog(input: $input) {
  177. id
  178. title
  179. eventName
  180. description
  181. open
  182. }
  183. }
  184. `, {input}, {permissions: ["log.edit"]}).then(({editLog}) => {
  185. return editLog
  186. })
  187. }
  188. /**
  189. * Call `removeLog(input)` mutation, returns the id
  190. *
  191. * @param {{id:string}} input
  192. * @returns {Promise<{id:string}>}
  193. */
  194. remove(input) {
  195. return query(`
  196. mutation RemoveLog($input: LogRemoveInput!) {
  197. removeLog(input: $input) {
  198. id
  199. }
  200. }
  201. `, {input}, {permissions: ["log.remove"]}).then(({editLog}) => {
  202. return editLog
  203. })
  204. }
  205. /**
  206. * Subscribe to the changes.
  207. *
  208. * @param {string} logId The long logId, the short one won't do.
  209. */
  210. subscribeChanges(logId) {
  211. return new Subscription(`
  212. subscription Changes($keys: [ChangeKeyInput!]!) {
  213. changes(keys: $keys) {
  214. id
  215. model
  216. op
  217. author
  218. listed
  219. keys {
  220. model
  221. id
  222. }
  223. date
  224. objects {
  225. type: __typename
  226. ...on Log {
  227. id
  228. title
  229. eventName
  230. description
  231. open
  232. }
  233. ...on Post {
  234. id
  235. time
  236. kind
  237. nick
  238. text
  239. position
  240. }
  241. }
  242. }
  243. }
  244. `, {keys: [{model: "Log", id: logId}]})
  245. }
  246. }
  247. module.exports = { Log, logsApi: new LogAPI }