2020-06-22 06:00:39 -05:00
|
|
|
import {
|
|
|
|
ActivityCreate,
|
|
|
|
ActivityFlag,
|
|
|
|
VideoAbuseState,
|
|
|
|
videoAbusePredefinedReasonsMap
|
|
|
|
} from '../../../../shared'
|
2019-01-15 04:14:12 -06:00
|
|
|
import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects'
|
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
2020-05-07 07:58:24 -05:00
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2019-01-15 04:14:12 -06:00
|
|
|
import { VideoAbuseModel } from '../../../models/video/video-abuse'
|
|
|
|
import { getOrCreateVideoAndAccountAndChannel } from '../videos'
|
|
|
|
import { Notifier } from '../../notifier'
|
|
|
|
import { getAPId } from '../../../helpers/activitypub'
|
2020-06-18 03:45:25 -05:00
|
|
|
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
|
|
|
|
import { MActorSignature, MVideoAbuseAccountVideo } from '../../../types/models'
|
2020-05-05 13:22:22 -05:00
|
|
|
import { AccountModel } from '@server/models/account/account'
|
2019-01-15 04:14:12 -06:00
|
|
|
|
2019-08-02 03:53:36 -05:00
|
|
|
async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
|
|
|
|
const { activity, byActor } = options
|
2019-01-15 04:14:12 -06:00
|
|
|
return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processFlagActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
|
2019-01-15 04:14:12 -06:00
|
|
|
const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject)
|
|
|
|
|
|
|
|
const account = byActor.Account
|
2019-07-02 04:21:14 -05:00
|
|
|
if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url)
|
2019-01-15 04:14:12 -06:00
|
|
|
|
2019-08-30 02:40:21 -05:00
|
|
|
const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
|
2019-01-15 04:14:12 -06:00
|
|
|
|
2019-08-30 02:40:21 -05:00
|
|
|
for (const object of objects) {
|
|
|
|
try {
|
|
|
|
logger.debug('Reporting remote abuse for video %s.', getAPId(object))
|
|
|
|
|
|
|
|
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: object })
|
2020-05-05 13:22:22 -05:00
|
|
|
const reporterAccount = await sequelizeTypescript.transaction(async t => AccountModel.load(account.id, t))
|
2020-06-22 06:00:39 -05:00
|
|
|
const tags = Array.isArray(flag.tag) ? flag.tag : []
|
|
|
|
const predefinedReasons = tags.map(tag => videoAbusePredefinedReasonsMap[tag.name])
|
|
|
|
.filter(v => !isNaN(v))
|
|
|
|
const startAt = flag.startAt
|
|
|
|
const endAt = flag.endAt
|
2019-01-15 04:14:12 -06:00
|
|
|
|
2020-05-05 13:22:22 -05:00
|
|
|
const videoAbuseInstance = await sequelizeTypescript.transaction(async t => {
|
2019-08-30 02:40:21 -05:00
|
|
|
const videoAbuseData = {
|
|
|
|
reporterAccountId: account.id,
|
|
|
|
reason: flag.content,
|
|
|
|
videoId: video.id,
|
2020-06-22 06:00:39 -05:00
|
|
|
state: VideoAbuseState.PENDING,
|
|
|
|
predefinedReasons,
|
|
|
|
startAt,
|
|
|
|
endAt
|
2019-08-30 02:40:21 -05:00
|
|
|
}
|
2019-01-15 04:14:12 -06:00
|
|
|
|
2020-05-05 13:22:22 -05:00
|
|
|
const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(videoAbuseData, { transaction: t })
|
2019-08-30 02:40:21 -05:00
|
|
|
videoAbuseInstance.Video = video
|
2020-05-05 13:22:22 -05:00
|
|
|
videoAbuseInstance.Account = reporterAccount
|
2019-07-29 04:59:29 -05:00
|
|
|
|
2019-08-30 02:40:21 -05:00
|
|
|
logger.info('Remote abuse for video uuid %s created', flag.object)
|
2019-07-29 04:59:29 -05:00
|
|
|
|
2019-08-30 02:40:21 -05:00
|
|
|
return videoAbuseInstance
|
|
|
|
})
|
|
|
|
|
2020-05-05 13:22:22 -05:00
|
|
|
const videoAbuseJSON = videoAbuseInstance.toFormattedJSON()
|
|
|
|
|
|
|
|
Notifier.Instance.notifyOnNewVideoAbuse({
|
|
|
|
videoAbuse: videoAbuseJSON,
|
|
|
|
videoAbuseInstance,
|
|
|
|
reporter: reporterAccount.Actor.getIdentifier()
|
|
|
|
})
|
2019-08-30 02:40:21 -05:00
|
|
|
} catch (err) {
|
|
|
|
logger.debug('Cannot process report of %s. (Maybe not a video abuse).', getAPId(object), { err })
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 04:14:12 -06:00
|
|
|
}
|