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.

114 lines
2.9 KiB

  1. const {query} = require("../client")
  2. class Post {
  3. /**
  4. * @param {string} id
  5. * @param {number} position
  6. * @param {Date | string | number} time
  7. * @param {string} kind
  8. * @param {string} nick
  9. * @param {string} text
  10. */
  11. constructor(id, position, time, kind, nick, text) {
  12. this.id = id
  13. this.position = position
  14. this.time = new Date(time)
  15. this.kind = kind
  16. this.nick = nick
  17. this.text = text
  18. }
  19. static fromData(data) {
  20. return new Post(data.id, data.position, data.time, data.kind, data.nick, data.text)
  21. }
  22. }
  23. class PostAPI {
  24. /**
  25. * Call `addPost(input)` mutation, returns the id and edited fields of the post
  26. *
  27. * @param {{logId:string, time:Date, kind:string, nick:string, text:string}} input
  28. * @returns {Promise<{id:string, time:Date, kind:string, nick:string, text:string}>}
  29. */
  30. add(input) {
  31. return query(`
  32. mutation AddPost($input: PostAddInput!) {
  33. addPost(input: $input) {
  34. id
  35. position
  36. time
  37. kind
  38. nick
  39. text
  40. }
  41. }
  42. `, {input}, {permissions: ["post.add"]}).then(({addPost}) => {
  43. return new Post(addPost.id, addPost.position, addPost.time, addPost.kind, addPost.nick, addPost.text)
  44. })
  45. }
  46. /**
  47. * Call `movePost(input)` mutation, returns the id and position of all affected posts.
  48. *
  49. * @param {{id:string, toPosition: number}} input
  50. * @returns {Promise<{id:string, position:number}[]>}
  51. */
  52. move(input) {
  53. return query(`
  54. mutation MovePost($input: PostMoveInput!) {
  55. movePost(input: $input) {
  56. id
  57. position
  58. }
  59. }
  60. `, {input}, {permissions: ["post.move"]}).then(({movePost}) => {
  61. return movePost
  62. })
  63. }
  64. /**
  65. * Call `editPost(input)` mutation, returns the id and edited fields of the post
  66. *
  67. * @param {{id:string, time:Date, kind:string, nick:string, text:string}} input
  68. * @returns {Promise<{id:string, time:Date, kind:string, nick:string, text:string}>}
  69. */
  70. edit(input) {
  71. return query(`
  72. mutation EditPost($input: PostEditInput!) {
  73. editPost(input: $input) {
  74. id
  75. time
  76. kind
  77. nick
  78. text
  79. }
  80. }
  81. `, {input}, {permissions: ["post.edit"]}).then(({editPost}) => {
  82. if (editPost.time != null) {
  83. editPost.time = new Date(editPost.time)
  84. }
  85. return editPost
  86. })
  87. }
  88. /**
  89. * Call `removePost(input)` mutation, returns the id of the affected post.
  90. *
  91. * @param {{id:string, toPosition: number}} input
  92. * @returns {Promise<{id:string}>}
  93. */
  94. remove(input) {
  95. return query(`
  96. mutation RemovePost($input: PostRemoveInput!) {
  97. removePost(input: $input) {
  98. id
  99. }
  100. }
  101. `, {input}, {permissions: ["post.remove"]}).then(({removePost}) => {
  102. return removePost
  103. })
  104. }
  105. }
  106. module.exports = {Post, postApi: new PostAPI}