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

104 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-07-01 09:05:30 -05: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 07:58:01 -05:00
import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
import { AbuseObject, AbuseState, ActivityCreate, ActivityFlag } from '../../../../shared'
2020-07-01 09:05:30 -05:00
import { getAPId } from '../../../helpers/activitypub'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
2020-05-07 07:58:24 -05:00
import { sequelizeTypescript } from '../../../initializers/database'
2020-06-18 03:45:25 -05:00
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
2020-07-01 09:05:30 -05:00
import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models'
2019-08-02 03:53:36 -05:00
async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
const { activity, byActor } = options
2020-07-01 09:05:30 -05:00
return retryTransactionWrapper(processCreateAbuse, activity, byActor)
}
// ---------------------------------------------------------------------------
export {
processFlagActivity
}
// ---------------------------------------------------------------------------
2020-07-01 09:05:30 -05: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 09:05:30 -05: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 02:40:21 -05:00
const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
2020-07-01 09:05:30 -05: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 02:40:21 -05:00
for (const object of objects) {
try {
2020-07-01 09:05:30 -05:00
const uri = getAPId(object)
2020-07-01 09:05:30 -05:00
logger.debug('Reporting remote abuse for object %s.', uri)
2019-07-29 04:59:29 -05:00
2020-07-01 09:05:30 -05:00
await sequelizeTypescript.transaction(async t => {
2019-07-29 04:59:29 -05:00
2020-07-01 09:05:30 -05:00
const video = await VideoModel.loadByUrlAndPopulateAccount(uri)
let videoComment: MCommentOwnerVideo
let flaggedAccount: MAccountDefault
if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri)
if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri)
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 02:40:21 -05:00
2020-07-01 09:05:30 -05:00
if (video) {
return createVideoAbuse({
baseAbuse,
startAt,
endAt,
reporterAccount,
transaction: t,
videoInstance: video
})
}
if (videoComment) {
return createVideoCommentAbuse({
baseAbuse,
reporterAccount,
transaction: t,
commentInstance: videoComment
})
}
2020-07-01 09:05:30 -05:00
return await createAccountAbuse({
baseAbuse,
reporterAccount,
transaction: t,
accountInstance: flaggedAccount
})
})
2019-08-30 02:40:21 -05:00
} catch (err) {
2020-07-01 09:05:30 -05:00
logger.debug('Cannot process report of %s', getAPId(object), { err })
2019-08-30 02:40:21 -05:00
}
}
}