ニジカ投稿局 https://tv.nizika.tv
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.6 KiB

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