PeerTube/server/core/models/abuse/abuse-message.ts

114 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-03-21 14:20:47 +01:00
import { FindOptions } from 'sequelize'
2024-02-22 10:12:04 +01:00
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Table, UpdatedAt } from 'sequelize-typescript'
import { isAbuseMessageValid } from '@server/helpers/custom-validators/abuses.js'
import { MAbuseMessage, MAbuseMessageFormattable } from '@server/types/models/index.js'
import { AbuseMessage } from '@peertube/peertube-models'
import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account.js'
2024-02-22 10:12:04 +01:00
import { SequelizeModel, getSort, throwIfNotValid } from '../shared/index.js'
import { AbuseModel } from './abuse.js'
2020-07-24 15:05:51 +02:00
@Table({
tableName: 'abuseMessage',
indexes: [
{
fields: [ 'abuseId' ]
},
{
fields: [ 'accountId' ]
}
]
})
2024-02-22 10:12:04 +01:00
export class AbuseMessageModel extends SequelizeModel<AbuseMessageModel> {
2020-07-24 15:05:51 +02: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: Awaited<AccountModel>
2020-07-24 15:05:51 +02:00
@ForeignKey(() => AbuseModel)
@Column
abuseId: number
@BelongsTo(() => AbuseModel, {
foreignKey: {
name: 'abuseId',
allowNull: false
},
onDelete: 'cascade'
})
Abuse: Awaited<AbuseModel>
2020-07-24 15:05:51 +02:00
static listForApi (abuseId: number) {
const getQuery = (forCount: boolean) => {
const query: FindOptions = {
where: { abuseId },
order: getSort('createdAt')
}
2020-07-24 15:05:51 +02:00
if (forCount !== true) {
query.include = [
{
model: AccountModel.scope(AccountScopeNames.SUMMARY),
required: false
}
]
}
2020-07-24 15:05:51 +02:00
return query
2020-07-24 15:05:51 +02:00
}
return Promise.all([
AbuseMessageModel.count(getQuery(true)),
AbuseMessageModel.findAll(getQuery(false))
]).then(([ total, data ]) => ({ total, data }))
2020-07-24 15:05:51 +02: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 17:21:25 +02:00
createdAt: this.createdAt,
2020-07-24 15:05:51 +02:00
byModerator: this.byModerator,
message: this.message,
account
}
}
}