abuse-message.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { FindOptions } from 'sequelize'
  2. import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
  3. import { isAbuseMessageValid } from '@server/helpers/custom-validators/abuses'
  4. import { MAbuseMessage, MAbuseMessageFormattable } from '@server/types/models'
  5. import { AbuseMessage } from '@shared/models'
  6. import { AttributesOnly } from '@shared/typescript-utils'
  7. import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account'
  8. import { getSort, throwIfNotValid } from '../utils'
  9. import { AbuseModel } from './abuse'
  10. @Table({
  11. tableName: 'abuseMessage',
  12. indexes: [
  13. {
  14. fields: [ 'abuseId' ]
  15. },
  16. {
  17. fields: [ 'accountId' ]
  18. }
  19. ]
  20. })
  21. export class AbuseMessageModel extends Model<Partial<AttributesOnly<AbuseMessageModel>>> {
  22. @AllowNull(false)
  23. @Is('AbuseMessage', value => throwIfNotValid(value, isAbuseMessageValid, 'message'))
  24. @Column(DataType.TEXT)
  25. message: string
  26. @AllowNull(false)
  27. @Column
  28. byModerator: boolean
  29. @CreatedAt
  30. createdAt: Date
  31. @UpdatedAt
  32. updatedAt: Date
  33. @ForeignKey(() => AccountModel)
  34. @Column
  35. accountId: number
  36. @BelongsTo(() => AccountModel, {
  37. foreignKey: {
  38. name: 'accountId',
  39. allowNull: true
  40. },
  41. onDelete: 'set null'
  42. })
  43. Account: AccountModel
  44. @ForeignKey(() => AbuseModel)
  45. @Column
  46. abuseId: number
  47. @BelongsTo(() => AbuseModel, {
  48. foreignKey: {
  49. name: 'abuseId',
  50. allowNull: false
  51. },
  52. onDelete: 'cascade'
  53. })
  54. Abuse: AbuseModel
  55. static listForApi (abuseId: number) {
  56. const getQuery = (forCount: boolean) => {
  57. const query: FindOptions = {
  58. where: { abuseId },
  59. order: getSort('createdAt')
  60. }
  61. if (forCount !== true) {
  62. query.include = [
  63. {
  64. model: AccountModel.scope(AccountScopeNames.SUMMARY),
  65. required: false
  66. }
  67. ]
  68. }
  69. return query
  70. }
  71. return Promise.all([
  72. AbuseMessageModel.count(getQuery(true)),
  73. AbuseMessageModel.findAll(getQuery(false))
  74. ]).then(([ total, data ]) => ({ total, data }))
  75. }
  76. static loadByIdAndAbuseId (messageId: number, abuseId: number): Promise<MAbuseMessage> {
  77. return AbuseMessageModel.findOne({
  78. where: {
  79. id: messageId,
  80. abuseId
  81. }
  82. })
  83. }
  84. toFormattedJSON (this: MAbuseMessageFormattable): AbuseMessage {
  85. const account = this.Account
  86. ? this.Account.toFormattedSummaryJSON()
  87. : null
  88. return {
  89. id: this.id,
  90. createdAt: this.createdAt,
  91. byModerator: this.byModerator,
  92. message: this.message,
  93. account
  94. }
  95. }
  96. }