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.

116 lines
2.9 KiB

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. let eventNames = []
  3. class LogHeader {
  4. /**
  5. * Construct a log header. You should probably use the logHeaderApi instead of doing
  6. * this manually, even for mutations.
  7. *
  8. * @param {string} id
  9. * @param {Date|string} date
  10. * @param {string} channelName
  11. * @param {string} title
  12. * @param {string} description
  13. * @param {string} event
  14. * @param {boolean} open
  15. * @param {{id:string,name:string,shortName:string,author:string}[]} characters
  16. */
  17. constructor(id, shortId, date, channelName, title, description, eventName, open, characters) {
  18. this.id = id
  19. this.shortId = shortId
  20. this.date = new Date(date)
  21. this.channelName = channelName
  22. this.title = title || null
  23. this.description = description || null
  24. this.eventName = eventName || null
  25. this.open = open
  26. this.characters = characters.map(ch => new LogHeaderCharacter(ch.id, ch.name, ch.shortName, ch.author))
  27. }
  28. }
  29. class LogHeaderCharacter {
  30. /**
  31. * Construct a log header character list entry.
  32. *
  33. * @param {string} id
  34. * @param {string} name
  35. * @param {string} shortName
  36. * @param {string} author
  37. */
  38. constructor(id, name, shortName, author) {
  39. this.id = id
  40. this.name = name
  41. this.shortName = shortName
  42. this.author = author
  43. }
  44. }
  45. /**
  46. * logHeaderApi contains the API queries for the LogHeader frontend model, which is a subset of the
  47. * logs model.
  48. */
  49. const logHeaderApi = {
  50. /**
  51. * Call `logs` query, returns event name
  52. *
  53. * @returns {Promise<string[]>}
  54. */
  55. eventNames() {
  56. return query(`
  57. query LogHeadersEventNames() {
  58. headers: logs(filter:{limit:0}) {
  59. eventName
  60. }
  61. }
  62. `, {}).then(({headers}) => {
  63. const eventNames = []
  64. const seen = {}
  65. for (const header of headers) {
  66. if (!header.eventName) {
  67. continue
  68. }
  69. if (!seen[header.eventName]) {
  70. eventNames.push(header.eventName)
  71. seen[header.eventName] = true
  72. }
  73. }
  74. return eventNames
  75. })
  76. },
  77. /**
  78. * Call `logs(filter)` query
  79. *
  80. * @param {{search:string, channels:string|string[], events:string|string[], open:boolean, characters:string|string[], limit:number}} filter
  81. * @returns {Promise<LogHeader[]>}
  82. */
  83. list(filter = {}) {
  84. return query(`
  85. query LogHeaders($filter: LogsFilter) {
  86. headers: logs(filter:$filter) {
  87. id
  88. shortId
  89. date
  90. channelName
  91. title
  92. description
  93. eventName
  94. open
  95. characters {
  96. id
  97. name
  98. shortName
  99. author
  100. }
  101. }
  102. }
  103. `, {filter}).then(({headers}) => {
  104. return headers.map(h => new LogHeader(h.id, h.shortId, h.date, h.channelName, h.title, h.description, h.eventName, h.open, h.characters))
  105. })
  106. },
  107. }
  108. module.exports = {LogHeader, LogHeaderCharacter, logHeaderApi, eventNames}