1 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { isAbuseMessageValid } from '@server/helpers/custom-validators/abuses'
3 import { MAbuseMessage, MAbuseMessageFormattable } from '@server/types/models'
4 import { AttributesOnly } from '@shared/typescript-utils'
5 import { AbuseMessage } from '@shared/models'
6 import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account'
7 import { getSort, throwIfNotValid } from '../utils'
8 import { AbuseModel } from './abuse'
11 tableName: 'abuseMessage',
17 fields: [ 'accountId' ]
21 export class AbuseMessageModel extends Model<Partial<AttributesOnly<AbuseMessageModel>>> {
24 @Is('AbuseMessage', value => throwIfNotValid(value, isAbuseMessageValid, 'message'))
25 @Column(DataType.TEXT)
38 @ForeignKey(() => AccountModel)
42 @BelongsTo(() => AccountModel, {
51 @ForeignKey(() => AbuseModel)
55 @BelongsTo(() => AbuseModel, {
64 static listForApi (abuseId: number) {
68 order: getSort('createdAt'),
72 model: AccountModel.scope(AccountScopeNames.SUMMARY),
78 return AbuseMessageModel.findAndCountAll(options)
79 .then(({ rows, count }) => ({ data: rows, total: count }))
82 static loadByIdAndAbuseId (messageId: number, abuseId: number): Promise<MAbuseMessage> {
83 return AbuseMessageModel.findOne({
91 toFormattedJSON (this: MAbuseMessageFormattable): AbuseMessage {
92 const account = this.Account
93 ? this.Account.toFormattedSummaryJSON()
98 createdAt: this.createdAt,
100 byModerator: this.byModerator,
101 message: this.message,