1
0
Fork 0
peertube/server/lib/activitypub/process/process-flag.ts

106 lines
3.6 KiB
TypeScript
Raw Normal View History

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