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.

190 lines
4.2 KiB

  1. const Model = require('objection').Model
  2. const validate = require('validate.js')
  3. const _ = require('lodash')
  4. /* global WIKI */
  5. /**
  6. * Comments model
  7. */
  8. module.exports = class Comment extends Model {
  9. static get tableName() { return 'comments' }
  10. static get jsonSchema () {
  11. return {
  12. type: 'object',
  13. required: [],
  14. properties: {
  15. id: {type: 'integer'},
  16. content: {type: 'string'},
  17. render: {type: 'string'},
  18. name: {type: 'string'},
  19. email: {type: 'string'},
  20. ip: {type: 'string'},
  21. createdAt: {type: 'string'},
  22. updatedAt: {type: 'string'}
  23. }
  24. }
  25. }
  26. static get relationMappings() {
  27. return {
  28. author: {
  29. relation: Model.BelongsToOneRelation,
  30. modelClass: require('./users'),
  31. join: {
  32. from: 'comments.authorId',
  33. to: 'users.id'
  34. }
  35. },
  36. page: {
  37. relation: Model.BelongsToOneRelation,
  38. modelClass: require('./pages'),
  39. join: {
  40. from: 'comments.pageId',
  41. to: 'pages.id'
  42. }
  43. }
  44. }
  45. }
  46. $beforeUpdate() {
  47. this.updatedAt = new Date().toISOString()
  48. }
  49. $beforeInsert() {
  50. this.createdAt = new Date().toISOString()
  51. this.updatedAt = new Date().toISOString()
  52. }
  53. /**
  54. * Post New Comment
  55. */
  56. static async postNewComment ({ pageId, replyTo, content, guestName, guestEmail, user, ip }) {
  57. // -> Input validation
  58. if (user.id === 2) {
  59. const validation = validate({
  60. email: _.toLower(guestEmail),
  61. name: guestName
  62. }, {
  63. email: {
  64. email: true,
  65. length: {
  66. maximum: 255
  67. }
  68. },
  69. name: {
  70. presence: {
  71. allowEmpty: false
  72. },
  73. length: {
  74. minimum: 2,
  75. maximum: 255
  76. }
  77. }
  78. }, { format: 'flat' })
  79. if (validation && validation.length > 0) {
  80. throw new WIKI.Error.InputInvalid(validation[0])
  81. }
  82. }
  83. content = _.trim(content)
  84. if (content.length < 2) {
  85. throw new WIKI.Error.CommentContentMissing()
  86. }
  87. // -> Load Page
  88. const page = await WIKI.models.pages.getPageFromDb(pageId)
  89. if (page) {
  90. if (!WIKI.auth.checkAccess(user, ['write:comments'], {
  91. path: page.path,
  92. locale: page.localeCode
  93. })) {
  94. throw new WIKI.Error.CommentPostForbidden()
  95. }
  96. } else {
  97. throw new WIKI.Error.PageNotFound()
  98. }
  99. // -> Process by comment provider
  100. return WIKI.data.commentProvider.create({
  101. page,
  102. replyTo,
  103. content,
  104. user: {
  105. ...user,
  106. ...(user.id === 2) ? {
  107. name: guestName,
  108. email: guestEmail
  109. } : {},
  110. ip
  111. }
  112. })
  113. }
  114. /**
  115. * Update an Existing Comment
  116. */
  117. static async updateComment ({ id, content, user, ip }) {
  118. // -> Load Page
  119. const pageId = await WIKI.data.commentProvider.getPageIdFromCommentId(id)
  120. if (!pageId) {
  121. throw new WIKI.Error.CommentNotFound()
  122. }
  123. const page = await WIKI.models.pages.getPageFromDb(pageId)
  124. if (page) {
  125. if (!WIKI.auth.checkAccess(user, ['manage:comments'], {
  126. path: page.path,
  127. locale: page.localeCode
  128. })) {
  129. throw new WIKI.Error.CommentManageForbidden()
  130. }
  131. } else {
  132. throw new WIKI.Error.PageNotFound()
  133. }
  134. // -> Process by comment provider
  135. return WIKI.data.commentProvider.update({
  136. id,
  137. content,
  138. page,
  139. user: {
  140. ...user,
  141. ip
  142. }
  143. })
  144. }
  145. /**
  146. * Delete an Existing Comment
  147. */
  148. static async deleteComment ({ id, user, ip }) {
  149. // -> Load Page
  150. const pageId = await WIKI.data.commentProvider.getPageIdFromCommentId(id)
  151. if (!pageId) {
  152. throw new WIKI.Error.CommentNotFound()
  153. }
  154. const page = await WIKI.models.pages.getPageFromDb(pageId)
  155. if (page) {
  156. if (!WIKI.auth.checkAccess(user, ['manage:comments'], {
  157. path: page.path,
  158. locale: page.localeCode
  159. })) {
  160. throw new WIKI.Error.CommentManageForbidden()
  161. }
  162. } else {
  163. throw new WIKI.Error.PageNotFound()
  164. }
  165. // -> Process by comment provider
  166. await WIKI.data.commentProvider.remove({
  167. id,
  168. page,
  169. user: {
  170. ...user,
  171. ip
  172. }
  173. })
  174. }
  175. }