]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/send/send-create.ts
Put activity pub sends inside transactions
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-create.ts
index e5fb212b741f0e4bba0a1b815186b02d18f274d0..a34d3776caa9ec8160b2359a1f5fcea81a5b6cc7 100644 (file)
@@ -1,17 +1,23 @@
 import { Transaction } from 'sequelize'
-import { ActivityCreate } from '../../../../shared/models/activitypub/activity'
+import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub/activity'
+import { getServerAccount } from '../../../helpers/utils'
 import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models'
 import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface'
-import { broadcastToFollowers, getAudience, unicastTo } from './misc'
-import { getVideoAbuseActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
-import { getServerAccount } from '../../../helpers/utils'
-import { database as db } from '../../../initializers'
+import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
+import {
+  broadcastToFollowers,
+  getAccountsInvolvedInVideo,
+  getAudience,
+  getObjectFollowersAudience,
+  getOriginVideoAudience,
+  unicastTo
+} from './misc'
 
 async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
   const byAccount = videoChannel.Account
 
   const videoChannelObject = videoChannel.toActivityPubObject()
-  const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject)
+  const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject, t)
 
   return broadcastToFollowers(data, byAccount, [ byAccount ], t)
 }
@@ -20,7 +26,7 @@ async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbus
   const url = getVideoAbuseActivityPubUrl(videoAbuse)
 
   const audience = { to: [ video.VideoChannel.Account.url ], cc: [] }
-  const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject(), audience)
+  const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject(), t, audience)
 
   return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
 }
@@ -29,8 +35,9 @@ async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoI
   const url = getVideoViewActivityPubUrl(byAccount, video)
   const viewActivity = createViewActivityData(byAccount, video)
 
-  const audience = { to: [ video.VideoChannel.Account.url ], cc: [ video.VideoChannel.Account.url + '/followers' ] }
-  const data = await createActivityData(url, byAccount, viewActivity, audience)
+  const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
+  const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
+  const data = await createActivityData(url, byAccount, viewActivity, t, audience)
 
   return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
 }
@@ -39,24 +46,42 @@ async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video
   const url = getVideoViewActivityPubUrl(byAccount, video)
   const viewActivity = createViewActivityData(byAccount, video)
 
-  const audience = { to: [ video.VideoChannel.Account.url + '/followers' ], cc: [] }
-  const data = await createActivityData(url, byAccount, viewActivity, audience)
+  const accountsToForwardView = await getAccountsInvolvedInVideo(video, t)
+  const audience = getObjectFollowersAudience(accountsToForwardView)
+  const data = await createActivityData(url, byAccount, viewActivity, t, audience)
 
+  // Use the server account to send the view, because it could be an unregistered account
   const serverAccount = await getServerAccount()
-  const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id)
-  accountsToForwardView.push(video.VideoChannel.Account)
+  const followersException = [ byAccount ]
+  return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
+}
+
+async function sendCreateDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
+  const url = getVideoDislikeActivityPubUrl(byAccount, video)
+  const dislikeActivity = createDislikeActivityData(byAccount, video)
+
+  const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
+  const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
+  const data = await createActivityData(url, byAccount, dislikeActivity, t, audience)
+
+  return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
+}
 
-  // Don't forward view to server that sent it to us
-  const index = accountsToForwardView.findIndex(a => a.id === byAccount.id)
-  if (index) accountsToForwardView.splice(index, 1)
+async function sendCreateDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
+  const url = getVideoDislikeActivityPubUrl(byAccount, video)
+  const dislikeActivity = createDislikeActivityData(byAccount, video)
+
+  const accountsToForwardView = await getAccountsInvolvedInVideo(video, t)
+  const audience = getObjectFollowersAudience(accountsToForwardView)
+  const data = await createActivityData(url, byAccount, dislikeActivity, t, audience)
 
   const followersException = [ byAccount ]
-  return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
+  return broadcastToFollowers(data, byAccount, accountsToForwardView, t, followersException)
 }
 
-async function createActivityData (url: string, byAccount: AccountInstance, object: any, audience?: { to: string[], cc: string[] }) {
+async function createActivityData (url: string, byAccount: AccountInstance, object: any, t: Transaction, audience?: ActivityAudience) {
   if (!audience) {
-    audience = await getAudience(byAccount)
+    audience = await getAudience(byAccount, t)
   }
 
   const activity: ActivityCreate = {
@@ -71,6 +96,16 @@ async function createActivityData (url: string, byAccount: AccountInstance, obje
   return activity
 }
 
+function createDislikeActivityData (byAccount: AccountInstance, video: VideoInstance) {
+  const obj = {
+    type: 'Dislike',
+    actor: byAccount.url,
+    object: video.url
+  }
+
+  return obj
+}
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -78,7 +113,10 @@ export {
   sendVideoAbuse,
   createActivityData,
   sendCreateViewToOrigin,
-  sendCreateViewToVideoFollowers
+  sendCreateViewToVideoFollowers,
+  sendCreateDislikeToOrigin,
+  sendCreateDislikeToVideoFollowers,
+  createDislikeActivityData
 }
 
 // ---------------------------------------------------------------------------