]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-add.ts
Fetch video likes/dislikes too
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-add.ts
index f064c1ab61953e56b9772e2fcf9226c06c843a6b..433e68eb6c8a6dadffc01284515e5ebb069fa5ae 100644 (file)
@@ -1,17 +1,22 @@
 import * as Bluebird from 'bluebird'
 import { VideoTorrentObject } from '../../../../shared'
 import { ActivityAdd } from '../../../../shared/models/activitypub/activity'
-import { generateThumbnailFromUrl, getOrCreateAccount, logger, retryTransactionWrapper } from '../../../helpers'
-import { getOrCreateVideoChannel } from '../../../helpers/activitypub'
+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'
 import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
 
 async function processAddActivity (activity: ActivityAdd) {
   const activityObject = activity.object
   const activityType = activityObject.type
-  const account = await getOrCreateAccount(activity.actor)
+  const account = await getOrCreateAccountAndServer(activity.actor)
 
   if (activityType === 'Video') {
     const videoChannelUrl = activity.target
@@ -32,22 +37,36 @@ 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,
-  activity: ActivityAdd,
-  videoChannel: VideoChannelInstance,
-  videoToCreateData: VideoTorrentObject
-) {
-  logger.debug('Adding remote video %s.', videoToCreateData.url)
+function addRemoteVideo (account: AccountInstance,
+                         activity: ActivityAdd,
+                         videoChannel: VideoChannelInstance,
+                         videoToCreateData: VideoTorrentObject) {
+  logger.debug('Adding remote video %s.', videoToCreateData.id)
 
   return db.sequelize.transaction(async t => {
     const sequelizeOptions = {
@@ -85,3 +104,30 @@ function addRemoteVideo (
     return videoCreated
   })
 }
+
+async function createRates (accountUrls: string[], video: VideoInstance, rate: VideoRateType) {
+  let rateCounts = 0
+  const tasks: Bluebird<any>[] = []
+
+  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
+}