PeerTube/server/lib/activitypub/process/process-flag.ts

106 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-07-01 16:05:30 +02:00
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'
2020-08-06 14:58:01 +02:00
import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
2021-12-24 10:14:47 +01:00
import { AbuseObject, AbuseState, ActivityCreate, ActivityFlag } from '@shared/models'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
2020-05-07 14:58:24 +02:00
import { sequelizeTypescript } from '../../../initializers/database'
import { getAPId } from '../../../lib/activitypub/activity'
2020-06-18 10:45:25 +02:00
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
2020-07-01 16:05:30 +02:00
import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models'
2019-08-02 10:53:36 +02:00
async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
const { activity, byActor } = options
2020-07-01 16:05:30 +02:00
return retryTransactionWrapper(processCreateAbuse, activity, byActor)
}
// ---------------------------------------------------------------------------
export {
processFlagActivity
}
// ---------------------------------------------------------------------------
2020-07-01 16:05:30 +02:00
async function processCreateAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
const flag = activity.type === 'Flag' ? activity : (activity.object as AbuseObject)
const account = byActor.Account
2020-07-01 16:05:30 +02:00
if (!account) throw new Error('Cannot create abuse with the non account actor ' + byActor.url)
const reporterAccount = await AccountModel.load(account.id)
2019-08-30 09:40:21 +02:00
const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
2020-07-01 16:05:30 +02:00
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
2019-08-30 09:40:21 +02:00
for (const object of objects) {
try {
2020-07-01 16:05:30 +02:00
const uri = getAPId(object)
2020-07-01 16:05:30 +02:00
logger.debug('Reporting remote abuse for object %s.', uri)
2019-07-29 11:59:29 +02:00
2020-07-01 16:05:30 +02:00
await sequelizeTypescript.transaction(async t => {
2021-02-25 15:55:31 +01:00
const video = await VideoModel.loadByUrlAndPopulateAccount(uri, t)
2020-07-01 16:05:30 +02:00
let videoComment: MCommentOwnerVideo
let flaggedAccount: MAccountDefault
2021-02-25 15:55:31 +01:00
if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri, t)
if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri, t)
2020-07-01 16:05:30 +02:00
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
}
2019-08-30 09:40:21 +02:00
2020-07-01 16:05:30 +02:00
if (video) {
return createVideoAbuse({
baseAbuse,
startAt,
endAt,
reporterAccount,
transaction: t,
2021-12-09 14:27:32 +01:00
videoInstance: video,
skipNotification: false
2020-07-01 16:05:30 +02:00
})
}
if (videoComment) {
return createVideoCommentAbuse({
baseAbuse,
reporterAccount,
transaction: t,
2021-12-09 14:27:32 +01:00
commentInstance: videoComment,
skipNotification: false
2020-07-01 16:05:30 +02:00
})
}
2020-07-01 16:05:30 +02:00
return await createAccountAbuse({
baseAbuse,
reporterAccount,
transaction: t,
2021-12-09 14:27:32 +01:00
accountInstance: flaggedAccount,
skipNotification: false
2020-07-01 16:05:30 +02:00
})
})
2019-08-30 09:40:21 +02:00
} catch (err) {
2020-07-01 16:05:30 +02:00
logger.debug('Cannot process report of %s', getAPId(object), { err })
2019-08-30 09:40:21 +02:00
}
}
}