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.

130 lines
3.2 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
  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. * Return all characters in that log.
  30. *
  31. * @param {...string} ids Character IDs
  32. */
  33. findCharacters(...ids) {
  34. return this.characters.filter(c => ids.includes(c.id))
  35. }
  36. }
  37. class LogHeaderCharacter {
  38. /**
  39. * Construct a log header character list entry.
  40. *
  41. * @param {string} id
  42. * @param {string} name
  43. * @param {string} shortName
  44. * @param {string} author
  45. */
  46. constructor(id, name, shortName, author) {
  47. this.id = id
  48. this.name = name
  49. this.shortName = shortName
  50. this.author = author
  51. }
  52. }
  53. /**
  54. * logHeaderApi contains the API queries for the LogHeader frontend model, which is a subset of the
  55. * logs model.
  56. */
  57. const logHeaderApi = {
  58. /**
  59. * Call `logs` query, returns event name
  60. *
  61. * @returns {Promise<string[]>}
  62. */
  63. eventNames() {
  64. return query(`
  65. query LogHeadersEventNames() {
  66. headers: logs(filter:{limit:0}) {
  67. eventName
  68. }
  69. }
  70. `, {}).then(({headers}) => {
  71. const eventNames = []
  72. const seen = {}
  73. for (const header of headers) {
  74. if (!header.eventName) {
  75. continue
  76. }
  77. if (!seen[header.eventName]) {
  78. eventNames.push(header.eventName)
  79. seen[header.eventName] = true
  80. }
  81. }
  82. return eventNames
  83. })
  84. },
  85. /**
  86. * Call `logs(filter)` query
  87. *
  88. * @param {{search:string, channels:string|string[], events:string|string[], open:boolean, characters:string|string[], limit:number}} filter
  89. * @returns {Promise<LogHeader[]>}
  90. */
  91. list(filter = {}) {
  92. filter = Object.assign({}, filter)
  93. if (filter.limit != null && filter.limit <= 0) {
  94. delete filter.limit
  95. }
  96. return query(`
  97. query LogHeaders($filter: LogsFilter) {
  98. headers: logs(filter:$filter) {
  99. id
  100. shortId
  101. date
  102. channelName
  103. title
  104. description
  105. eventName
  106. open
  107. characters {
  108. id
  109. name
  110. shortName
  111. author
  112. }
  113. }
  114. }
  115. `, {filter}).then(({headers}) => {
  116. return headers.map(h => new LogHeader(h.id, h.shortId, h.date, h.channelName, h.title, h.description, h.eventName, h.open, h.characters))
  117. })
  118. },
  119. }
  120. module.exports = {LogHeader, LogHeaderCharacter, logHeaderApi, eventNames}