X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-flag.ts;h=fd3e46e2b4a4333995fa1c2d3092b07f0102fafc;hb=fa66c9a601d69f6d57c956a3513e8bbed7ee9616;hp=1d7132a3a7010f8a04ac94747ae12af42e20070f;hpb=1ebddadd0704812a4600c39cabe2268321e88331;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-flag.ts b/server/lib/activitypub/process/process-flag.ts index 1d7132a3a..fd3e46e2b 100644 --- a/server/lib/activitypub/process/process-flag.ts +++ b/server/lib/activitypub/process/process-flag.ts @@ -1,24 +1,20 @@ -import { - ActivityCreate, - ActivityFlag, - VideoAbuseState, - videoAbusePredefinedReasonsMap -} from '../../../../shared' -import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects' +import { createAccountAbuse, createVideoAbuse, createVideoCommentAbuse } from '@server/lib/moderation' +import { AccountModel } from '@server/models/account/account' +import { VideoModel } from '@server/models/video/video' +import { VideoCommentModel } from '@server/models/video/video-comment' +import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse' +import { AbuseObject, AbuseState, ActivityCreate, ActivityFlag } from '../../../../shared' +import { getAPId } from '../../../helpers/activitypub' import { retryTransactionWrapper } from '../../../helpers/database-utils' import { logger } from '../../../helpers/logger' import { sequelizeTypescript } from '../../../initializers/database' -import { VideoAbuseModel } from '../../../models/video/video-abuse' -import { getOrCreateVideoAndAccountAndChannel } from '../videos' -import { Notifier } from '../../notifier' -import { getAPId } from '../../../helpers/activitypub' import { APProcessorOptions } from '../../../types/activitypub-processor.model' -import { MActorSignature, MVideoAbuseAccountVideo } from '../../../types/models' -import { AccountModel } from '@server/models/account/account' +import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models' async function processFlagActivity (options: APProcessorOptions) { const { activity, byActor } = options - return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor) + + return retryTransactionWrapper(processCreateAbuse, activity, byActor) } // --------------------------------------------------------------------------- @@ -29,55 +25,81 @@ export { // --------------------------------------------------------------------------- -async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) { - const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject) +async function processCreateAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) { + const flag = activity.type === 'Flag' ? activity : (activity.object as AbuseObject) const account = byActor.Account - if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url) + if (!account) throw new Error('Cannot create abuse with the non account actor ' + byActor.url) + + const reporterAccount = await AccountModel.load(account.id) const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ] + const tags = Array.isArray(flag.tag) ? flag.tag : [] + const predefinedReasons = tags.map(tag => abusePredefinedReasonsMap[tag.name]) + .filter(v => !isNaN(v)) + + const startAt = flag.startAt + const endAt = flag.endAt + for (const object of objects) { try { - logger.debug('Reporting remote abuse for video %s.', getAPId(object)) - - const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: object }) - const reporterAccount = await sequelizeTypescript.transaction(async t => AccountModel.load(account.id, t)) - 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 - - const videoAbuseInstance = await sequelizeTypescript.transaction(async t => { - const videoAbuseData = { - reporterAccountId: account.id, - reason: flag.content, - videoId: video.id, - state: VideoAbuseState.PENDING, - predefinedReasons, - startAt, - endAt - } + const uri = getAPId(object) - const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(videoAbuseData, { transaction: t }) - videoAbuseInstance.Video = video - videoAbuseInstance.Account = reporterAccount + logger.debug('Reporting remote abuse for object %s.', uri) - logger.info('Remote abuse for video uuid %s created', flag.object) + await sequelizeTypescript.transaction(async t => { + const video = await VideoModel.loadByUrlAndPopulateAccount(uri, t) + let videoComment: MCommentOwnerVideo + let flaggedAccount: MAccountDefault - return videoAbuseInstance - }) + if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri, t) + if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri, t) + + if (!video && !videoComment && !flaggedAccount) { + logger.warn('Cannot flag unknown entity %s.', object) + return + } - const videoAbuseJSON = videoAbuseInstance.toFormattedJSON() + const baseAbuse = { + reporterAccountId: reporterAccount.id, + reason: flag.content, + state: AbuseState.PENDING, + predefinedReasons + } + + if (video) { + return createVideoAbuse({ + baseAbuse, + startAt, + endAt, + reporterAccount, + transaction: t, + videoInstance: video, + skipNotification: false + }) + } + + if (videoComment) { + return createVideoCommentAbuse({ + baseAbuse, + reporterAccount, + transaction: t, + commentInstance: videoComment, + skipNotification: false + }) + } - Notifier.Instance.notifyOnNewVideoAbuse({ - videoAbuse: videoAbuseJSON, - videoAbuseInstance, - reporter: reporterAccount.Actor.getIdentifier() + return await createAccountAbuse({ + baseAbuse, + reporterAccount, + transaction: t, + accountInstance: flaggedAccount, + skipNotification: false + }) }) } catch (err) { - logger.debug('Cannot process report of %s. (Maybe not a video abuse).', getAPId(object), { err }) + logger.debug('Cannot process report of %s', getAPId(object), { err }) } } }