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.

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