X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-flag.ts;h=10f58ef27be981d833e6a8291f4fc7caf7d0f25f;hb=47d883de2efbc2e8b5f6f94ae18c15224cbe982b;hp=79ce6fb41e56e884cbdb5934a669dd3e25aa506e;hpb=88108880bbdba473cfe36ecbebc1c3c4f972e102;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-flag.ts b/server/lib/activitypub/process/process-flag.ts index 79ce6fb41..10f58ef27 100644 --- a/server/lib/activitypub/process/process-flag.ts +++ b/server/lib/activitypub/process/process-flag.ts @@ -1,16 +1,20 @@ -import { ActivityCreate, ActivityFlag, VideoAbuseState } 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/models' import { retryTransactionWrapper } from '../../../helpers/database-utils' import { logger } from '../../../helpers/logger' -import { sequelizeTypescript } from '../../../initializers' -import { ActorModel } from '../../../models/activitypub/actor' -import { VideoAbuseModel } from '../../../models/video/video-abuse' -import { getOrCreateVideoAndAccountAndChannel } from '../videos' -import { Notifier } from '../../notifier' -import { getAPId } from '../../../helpers/activitypub' - -async function processFlagActivity (activity: ActivityCreate | ActivityFlag, byActor: ActorModel) { - return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor) +import { sequelizeTypescript } from '../../../initializers/database' +import { getAPId } from '../../../lib/activitypub/activity' +import { APProcessorOptions } from '../../../types/activitypub-processor.model' +import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models' + +async function processFlagActivity (options: APProcessorOptions) { + const { activity, byActor } = options + + return retryTransactionWrapper(processCreateAbuse, activity, byActor) } // --------------------------------------------------------------------------- @@ -21,29 +25,81 @@ export { // --------------------------------------------------------------------------- -async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: ActorModel) { - const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject) - - logger.debug('Reporting remote abuse for video %s.', getAPId(flag.object)) +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 dislike with the non account actor ' + byActor.url) + if (!account) throw new Error('Cannot create abuse with the non account actor ' + byActor.url) - const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: flag.object }) + const reporterAccount = await AccountModel.load(account.id) - return sequelizeTypescript.transaction(async t => { - const videoAbuseData = { - reporterAccountId: account.id, - reason: flag.content, - videoId: video.id, - state: VideoAbuseState.PENDING - } + 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 { + const uri = getAPId(object) - const videoAbuseInstance = await VideoAbuseModel.create(videoAbuseData, { transaction: t }) - videoAbuseInstance.Video = video + logger.debug('Reporting remote abuse for object %s.', uri) - Notifier.Instance.notifyOnNewVideoAbuse(videoAbuseInstance) + await sequelizeTypescript.transaction(async t => { + const video = await VideoModel.loadByUrlAndPopulateAccount(uri, t) + let videoComment: MCommentOwnerVideo + let flaggedAccount: MAccountDefault - logger.info('Remote abuse for video uuid %s created', flag.object) - }) + 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 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 + }) + } + + return await createAccountAbuse({ + baseAbuse, + reporterAccount, + transaction: t, + accountInstance: flaggedAccount, + skipNotification: false + }) + }) + } catch (err) { + logger.debug('Cannot process report of %s', getAPId(object), { err }) + } + } }