1
0
Fork 0
peertube/server/models/video/video-abuse.ts

109 lines
2.4 KiB
TypeScript
Raw Normal View History

2018-02-01 05:08:10 -05:00
import { AfterCreate, AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2017-12-12 11:53:50 -05:00
import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
2018-03-12 06:29:46 -04:00
import { VideoAbuse } from '../../../shared/models/videos'
2017-12-12 11:53:50 -05:00
import { isVideoAbuseReasonValid } from '../../helpers/custom-validators/videos'
2018-02-01 05:08:10 -05:00
import { Emailer } from '../../lib/emailer'
2017-12-12 11:53:50 -05:00
import { AccountModel } from '../account/account'
import { getSort, throwIfNotValid } from '../utils'
import { VideoModel } from './video'
@Table({
tableName: 'videoAbuse',
indexes: [
2017-01-04 14:59:23 -05:00
{
2017-12-12 11:53:50 -05:00
fields: [ 'videoId' ]
2017-01-04 14:59:23 -05:00
},
{
2017-12-12 11:53:50 -05:00
fields: [ 'reporterAccountId' ]
2017-01-04 14:59:23 -05:00
}
2017-05-22 14:58:25 -04:00
]
2017-12-12 11:53:50 -05:00
})
export class VideoAbuseModel extends Model<VideoAbuseModel> {
2017-05-22 14:58:25 -04:00
2017-12-12 11:53:50 -05:00
@AllowNull(false)
@Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
@Column
reason: string
2017-11-16 11:04:19 -05:00
2017-12-12 11:53:50 -05:00
@CreatedAt
createdAt: Date
2017-11-16 11:04:19 -05:00
2017-12-12 11:53:50 -05:00
@UpdatedAt
updatedAt: Date
2017-05-22 14:58:25 -04:00
2017-12-12 11:53:50 -05:00
@ForeignKey(() => AccountModel)
@Column
reporterAccountId: number
2017-01-04 14:59:23 -05:00
2017-12-12 11:53:50 -05:00
@BelongsTo(() => AccountModel, {
2017-01-04 14:59:23 -05:00
foreignKey: {
2017-11-27 03:47:21 -05:00
allowNull: false
2017-01-04 14:59:23 -05:00
},
2017-12-12 11:53:50 -05:00
onDelete: 'cascade'
2017-01-04 14:59:23 -05:00
})
2017-12-12 11:53:50 -05:00
Account: AccountModel
@ForeignKey(() => VideoModel)
@Column
videoId: number
2017-01-04 14:59:23 -05:00
2017-12-12 11:53:50 -05:00
@BelongsTo(() => VideoModel, {
2017-01-04 14:59:23 -05:00
foreignKey: {
allowNull: false
},
2017-12-12 11:53:50 -05:00
onDelete: 'cascade'
2017-01-04 14:59:23 -05:00
})
2017-12-12 11:53:50 -05:00
Video: VideoModel
2018-02-01 05:08:10 -05:00
@AfterCreate
static sendEmailNotification (instance: VideoAbuseModel) {
return Emailer.Instance.addVideoAbuseReport(instance.videoId)
}
2017-12-12 11:53:50 -05:00
static listForApi (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
2018-02-19 03:41:03 -05:00
order: getSort(sort),
2017-12-12 11:53:50 -05:00
include: [
{
model: AccountModel,
2017-12-14 11:38:41 -05:00
required: true
2017-12-12 11:53:50 -05:00
},
{
model: VideoModel,
required: true
}
]
}
2017-01-04 14:59:23 -05:00
2017-12-12 11:53:50 -05:00
return VideoAbuseModel.findAndCountAll(query)
.then(({ rows, count }) => {
return { total: count, data: rows }
})
2017-01-04 14:59:23 -05:00
}
2018-03-12 06:29:46 -04:00
toFormattedJSON (): VideoAbuse {
2017-12-12 11:53:50 -05:00
return {
id: this.id,
reason: this.reason,
2018-03-12 06:29:46 -04:00
reporterAccount: this.Account.toFormattedJSON(),
video: {
id: this.Video.id,
uuid: this.Video.uuid,
url: this.Video.url,
name: this.Video.name
},
2017-12-12 11:53:50 -05:00
createdAt: this.createdAt
}
}
toActivityPubObject (): VideoAbuseObject {
return {
type: 'Flag' as 'Flag',
content: this.reason,
object: this.Video.url
}
}
2017-01-04 14:59:23 -05:00
}