2022-03-21 09:20:47 -04:00
|
|
|
import { FindOptions } from 'sequelize'
|
2020-07-24 09:05:51 -04:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
|
|
|
import { isAbuseMessageValid } from '@server/helpers/custom-validators/abuses'
|
2020-12-08 08:30:29 -05:00
|
|
|
import { MAbuseMessage, MAbuseMessageFormattable } from '@server/types/models'
|
2020-07-24 09:05:51 -04:00
|
|
|
import { AbuseMessage } from '@shared/models'
|
2022-02-28 02:34:43 -05:00
|
|
|
import { AttributesOnly } from '@shared/typescript-utils'
|
2020-07-24 09:05:51 -04:00
|
|
|
import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account'
|
2023-01-10 05:09:30 -05:00
|
|
|
import { getSort, throwIfNotValid } from '../shared'
|
2020-07-24 09:05:51 -04:00
|
|
|
import { AbuseModel } from './abuse'
|
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'abuseMessage',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'abuseId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'accountId' ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2021-05-12 08:09:04 -04:00
|
|
|
export class AbuseMessageModel extends Model<Partial<AttributesOnly<AbuseMessageModel>>> {
|
2020-07-24 09:05:51 -04:00
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AbuseMessage', value => throwIfNotValid(value, isAbuseMessageValid, 'message'))
|
|
|
|
@Column(DataType.TEXT)
|
|
|
|
message: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
byModerator: boolean
|
|
|
|
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@ForeignKey(() => AccountModel)
|
|
|
|
@Column
|
|
|
|
accountId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => AccountModel, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'accountId',
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'set null'
|
|
|
|
})
|
|
|
|
Account: AccountModel
|
|
|
|
|
|
|
|
@ForeignKey(() => AbuseModel)
|
|
|
|
@Column
|
|
|
|
abuseId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => AbuseModel, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'abuseId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
Abuse: AbuseModel
|
|
|
|
|
|
|
|
static listForApi (abuseId: number) {
|
2022-02-28 02:34:43 -05:00
|
|
|
const getQuery = (forCount: boolean) => {
|
|
|
|
const query: FindOptions = {
|
|
|
|
where: { abuseId },
|
|
|
|
order: getSort('createdAt')
|
|
|
|
}
|
2020-07-24 09:05:51 -04:00
|
|
|
|
2022-02-28 02:34:43 -05:00
|
|
|
if (forCount !== true) {
|
|
|
|
query.include = [
|
|
|
|
{
|
|
|
|
model: AccountModel.scope(AccountScopeNames.SUMMARY),
|
|
|
|
required: false
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2020-07-24 09:05:51 -04:00
|
|
|
|
2022-02-28 02:34:43 -05:00
|
|
|
return query
|
2020-07-24 09:05:51 -04:00
|
|
|
}
|
|
|
|
|
2022-02-28 02:34:43 -05:00
|
|
|
return Promise.all([
|
|
|
|
AbuseMessageModel.count(getQuery(true)),
|
|
|
|
AbuseMessageModel.findAll(getQuery(false))
|
|
|
|
]).then(([ total, data ]) => ({ total, data }))
|
2020-07-24 09:05:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static loadByIdAndAbuseId (messageId: number, abuseId: number): Promise<MAbuseMessage> {
|
|
|
|
return AbuseMessageModel.findOne({
|
|
|
|
where: {
|
|
|
|
id: messageId,
|
|
|
|
abuseId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
toFormattedJSON (this: MAbuseMessageFormattable): AbuseMessage {
|
|
|
|
const account = this.Account
|
|
|
|
? this.Account.toFormattedSummaryJSON()
|
|
|
|
: null
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: this.id,
|
2020-07-24 11:21:25 -04:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
|
2020-07-24 09:05:51 -04:00
|
|
|
byModerator: this.byModerator,
|
|
|
|
message: this.message,
|
|
|
|
|
|
|
|
account
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|