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'
|
2019-01-15 11:14:12 +01:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
2020-05-07 14:58:24 +02:00
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2022-03-23 14:24:50 +01:00
|
|
|
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-01-15 11:14:12 +01:00
|
|
|
|
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)
|
2019-01-15 11:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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)
|
2019-01-15 11:14:12 +01:00
|
|
|
|
|
|
|
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-01-15 11:14:12 +01:00
|
|
|
|
2019-08-30 09:40:21 +02:00
|
|
|
const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
|
2019-01-15 11:14:12 +01:00
|
|
|
|
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)
|
2019-01-15 11:14:12 +01:00
|
|
|
|
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-05-05 20:22:22 +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
|
|
|
})
|
2020-05-05 20:22:22 +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
|
|
|
}
|
|
|
|
}
|
2019-01-15 11:14:12 +01:00
|
|
|
}
|