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.

121 lines
3.0 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
  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. filter = Object.assign({}, filter)
  85. if (filter.limit != null && filter.limit <= 0) {
  86. delete filter.limit
  87. }
  88. return query(`
  89. query LogHeaders($filter: LogsFilter) {
  90. headers: logs(filter:$filter) {
  91. id
  92. shortId
  93. date
  94. channelName
  95. title
  96. description
  97. eventName
  98. open
  99. characters {
  100. id
  101. name
  102. shortName
  103. author
  104. }
  105. }
  106. }
  107. `, {filter}).then(({headers}) => {
  108. return headers.map(h => new LogHeader(h.id, h.shortId, h.date, h.channelName, h.title, h.description, h.eventName, h.open, h.characters))
  109. })
  110. },
  111. }
  112. module.exports = {LogHeader, LogHeaderCharacter, logHeaderApi, eventNames}