comments.js 4.2 KB

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