From 16b90975941b78d01d7202d441bf731a10048c16 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 23 Nov 2017 16:55:13 +0100 Subject: Fetch video likes/dislikes too --- server/lib/activitypub/process/process-add.ts | 51 ++++++++++++++++++++-- server/lib/activitypub/process/process.ts | 6 ++- .../activitypub-http-fetcher-handler.ts | 2 +- 3 files changed, 54 insertions(+), 5 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/process/process-add.ts b/server/lib/activitypub/process/process-add.ts index 332c18cc0..433e68eb6 100644 --- a/server/lib/activitypub/process/process-add.ts +++ b/server/lib/activitypub/process/process-add.ts @@ -1,11 +1,13 @@ import * as Bluebird from 'bluebird' import { VideoTorrentObject } from '../../../../shared' import { ActivityAdd } from '../../../../shared/models/activitypub/activity' +import { VideoRateType } from '../../../../shared/models/videos/video-rate.type' import { retryTransactionWrapper } from '../../../helpers/database-utils' import { logger } from '../../../helpers/logger' import { database as db } from '../../../initializers' import { AccountInstance } from '../../../models/account/account-interface' import { VideoChannelInstance } from '../../../models/video/video-channel-interface' +import { VideoInstance } from '../../../models/video/video-interface' import { getOrCreateAccountAndServer } from '../account' import { getOrCreateVideoChannel } from '../video-channels' import { generateThumbnailFromUrl } from '../videos' @@ -35,13 +37,29 @@ export { // --------------------------------------------------------------------------- -function processAddVideo (account: AccountInstance, activity: ActivityAdd, videoChannel: VideoChannelInstance, video: VideoTorrentObject) { +async function processAddVideo ( + account: AccountInstance, + activity: ActivityAdd, + videoChannel: VideoChannelInstance, + videoToCreateData: VideoTorrentObject +) { const options = { - arguments: [ account, activity, videoChannel, video ], + arguments: [ account, activity, videoChannel, videoToCreateData ], errorMessage: 'Cannot insert the remote video with many retries.' } - return retryTransactionWrapper(addRemoteVideo, options) + const video = await retryTransactionWrapper(addRemoteVideo, options) + + // Process outside the transaction because we could fetch remote data + if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) { + await createRates(videoToCreateData.likes.orderedItems, video, 'like') + } + + if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) { + await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike') + } + + return video } function addRemoteVideo (account: AccountInstance, @@ -86,3 +104,30 @@ function addRemoteVideo (account: AccountInstance, return videoCreated }) } + +async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) { + let rateCounts = 0 + const tasks: Bluebird[] = [] + + for (const accountUrl of accountUrls) { + const account = await getOrCreateAccountAndServer(accountUrl) + const p = db.AccountVideoRate + .create({ + videoId: video.id, + accountId: account.id, + type: rate + }) + .then(() => rateCounts += 1) + + tasks.push(p) + } + + await Promise.all(tasks) + + logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid) + + // This is "likes" and "dislikes" + await video.increment(rate + 's', { by: rateCounts }) + + return +} diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts index 942bce0e6..40f19c701 100644 --- a/server/lib/activitypub/process/process.ts +++ b/server/lib/activitypub/process/process.ts @@ -31,7 +31,11 @@ async function processActivities (activities: Activity[], inboxAccount?: Account continue } - await activityProcessor(activity, inboxAccount) + try { + await activityProcessor(activity, inboxAccount) + } catch (err) { + logger.warn('Cannot process activity %s.', activity.type, err) + } } } diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts index bda319592..9adceab84 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts @@ -49,7 +49,7 @@ async function process (payload: ActivityPubHttpPayload, jobId: number) { } function onError (err: Error, jobId: number) { - logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err) + logger.error('Error when fetcher ActivityPub request in job %d.', jobId, err) return Promise.resolve() } -- cgit v1.2.3