From 8e13fa7d09e9925b4559cbba6c5d72c5ff1bd391 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 15 Nov 2017 15:12:23 +0100 Subject: Add video abuse to activity pub --- server/lib/activitypub/process-create.ts | 43 +++++++++++++++++++++++++++----- server/lib/activitypub/send-request.ts | 25 +++++++++++++++---- 2 files changed, 57 insertions(+), 11 deletions(-) (limited to 'server/lib/activitypub') diff --git a/server/lib/activitypub/process-create.ts b/server/lib/activitypub/process-create.ts index 471674ead..8d842b822 100644 --- a/server/lib/activitypub/process-create.ts +++ b/server/lib/activitypub/process-create.ts @@ -1,11 +1,9 @@ -import { ActivityCreate, VideoChannelObject, VideoTorrentObject } from '../../../shared' -import { ActivityAdd } from '../../../shared/models/activitypub/activity' -import { generateThumbnailFromUrl, logger, retryTransactionWrapper } from '../../helpers' +import { ActivityCreate, VideoChannelObject } from '../../../shared' +import { VideoAbuseObject } from '../../../shared/models/activitypub/objects/video-abuse-object' +import { logger, retryTransactionWrapper } from '../../helpers' +import { getActivityPubUrl, getOrCreateAccount } from '../../helpers/activitypub' import { database as db } from '../../initializers' -import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' -import Bluebird = require('bluebird') import { AccountInstance } from '../../models/account/account-interface' -import { getActivityPubUrl, getOrCreateAccount } from '../../helpers/activitypub' async function processCreateActivity (activity: ActivityCreate) { const activityObject = activity.object @@ -14,6 +12,8 @@ async function processCreateActivity (activity: ActivityCreate) { if (activityType === 'VideoChannel') { return processCreateVideoChannel(account, activityObject as VideoChannelObject) + } else if (activityType === 'Flag') { + return processCreateVideoAbuse(account, activityObject as VideoAbuseObject) } logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) @@ -62,3 +62,34 @@ async function addRemoteVideoChannel (account: AccountInstance, videoChannelToCr logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid) } + +function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { + const options = { + arguments: [ account, videoAbuseToCreateData ], + errorMessage: 'Cannot insert the remote video abuse with many retries.' + } + + return retryTransactionWrapper(addRemoteVideoAbuse, options) +} + +async function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { + logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) + + return db.sequelize.transaction(async t => { + const video = await db.Video.loadByUrl(videoAbuseToCreateData.object, t) + if (!video) { + logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) + return + } + + const videoAbuseData = { + reporterAccountId: account.id, + reason: videoAbuseToCreateData.content, + videoId: video.id + } + + await db.VideoAbuse.create(videoAbuseData) + + logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) + }) +} diff --git a/server/lib/activitypub/send-request.ts b/server/lib/activitypub/send-request.ts index f942a2eba..1a6cebc03 100644 --- a/server/lib/activitypub/send-request.ts +++ b/server/lib/activitypub/send-request.ts @@ -9,6 +9,8 @@ import { import { httpRequestJobScheduler } from '../jobs' import { signObject, activityPubContextify } from '../../helpers' import { Activity } from '../../../shared' +import { VideoAbuseInstance } from '../../models/video/video-abuse-interface' +import { getActivityPubUrl } from '../../helpers/activitypub' async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { const videoChannelObject = videoChannel.toActivityPubObject() @@ -56,16 +58,28 @@ async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transac return broadcastToFollowers(data, account, t) } +async function sendVideoAbuse ( + fromAccount: AccountInstance, + videoAbuse: VideoAbuseInstance, + video: VideoInstance, + t: Sequelize.Transaction +) { + const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString()) + const data = await createActivityData(url, fromAccount, video.url) + + return unicastTo(data, video.VideoChannel.Account.sharedInboxUrl, t) +} + async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) { const data = await acceptActivityData(fromAccount) - return unicastTo(data, toAccount, t) + return unicastTo(data, toAccount.inboxUrl, t) } async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) { const data = await followActivityData(toAccount.url, fromAccount) - return unicastTo(data, toAccount, t) + return unicastTo(data, toAccount.inboxUrl, t) } // --------------------------------------------------------------------------- @@ -79,7 +93,8 @@ export { sendDeleteVideo, sendDeleteAccount, sendAccept, - sendFollow + sendFollow, + sendVideoAbuse } // --------------------------------------------------------------------------- @@ -95,9 +110,9 @@ async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload) } -async function unicastTo (data: any, toAccount: AccountInstance, t: Sequelize.Transaction) { +async function unicastTo (data: any, toAccountUrl: string, t: Sequelize.Transaction) { const jobPayload = { - uris: [ toAccount.inboxUrl ], + uris: [ toAccountUrl ], body: data } -- cgit v1.2.3