]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/send/send-update.ts
Fix delete activities
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-update.ts
index 42738f973e18485f9e94f69222c9511d1f6ab3f4..e8f11edd0ec2a21b32bc2e0498774acb8d61ace8 100644 (file)
@@ -1,55 +1,66 @@
 import { Transaction } from 'sequelize'
-import { ActivityUpdate } from '../../../../shared/models/activitypub/activity'
-import { getUpdateActivityPubUrl } from '../../../helpers/activitypub'
-import { database as db } from '../../../initializers'
-import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models'
-import { broadcastToFollowers, getAudience } from './misc'
+import { ActivityAudience, ActivityUpdate } from '../../../../shared/models/activitypub'
+import { VideoPrivacy } from '../../../../shared/models/videos'
+import { UserModel } from '../../../models/account/user'
+import { ActorModel } from '../../../models/activitypub/actor'
+import { VideoModel } from '../../../models/video/video'
+import { VideoShareModel } from '../../../models/video/video-share'
+import { getUpdateActivityPubUrl } from '../url'
+import { audiencify, broadcastToFollowers, getAudience } from './misc'
 
-async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
-  const byAccount = videoChannel.Account
+async function sendUpdateVideo (video: VideoModel, t: Transaction) {
+  const byActor = video.VideoChannel.Account.Actor
 
-  const url = getUpdateActivityPubUrl(videoChannel.url, videoChannel.updatedAt.toISOString())
-  const videoChannelObject = videoChannel.toActivityPubObject()
-  const data = await updateActivityData(url, byAccount, videoChannelObject)
+  const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
+  const videoObject = video.toActivityPubObject()
+  const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
+
+  const data = await updateActivityData(url, byActor, videoObject, t, audience)
 
-  const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
-  accountsInvolved.push(byAccount)
+  const actorsInvolved = await VideoShareModel.loadActorsByShare(video.id, t)
+  actorsInvolved.push(byActor)
 
-  return broadcastToFollowers(data, byAccount, accountsInvolved, t)
+  return broadcastToFollowers(data, byActor, actorsInvolved, t)
 }
 
-async function sendUpdateVideo (video: VideoInstance, t: Transaction) {
-  const byAccount = video.VideoChannel.Account
+async function sendUpdateUser (user: UserModel, t: Transaction) {
+  const byActor = user.Account.Actor
 
-  const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
-  const videoObject = video.toActivityPubObject()
-  const data = await updateActivityData(url, byAccount, videoObject)
+  const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
+  const accountObject = user.Account.toActivityPubObject()
+  const audience = await getAudience(byActor, t)
+  const data = await updateActivityData(url, byActor, accountObject, t, audience)
 
-  const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
-  accountsInvolved.push(byAccount)
+  const actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t)
+  actorsInvolved.push(byActor)
 
-  return broadcastToFollowers(data, byAccount, accountsInvolved, t)
+  return broadcastToFollowers(data, byActor, actorsInvolved, t)
 }
 
 // ---------------------------------------------------------------------------
 
 export {
-  sendUpdateVideoChannel,
+  sendUpdateUser,
   sendUpdateVideo
 }
 
 // ---------------------------------------------------------------------------
 
-async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
-  const { to, cc } = await getAudience(byAccount)
-  const activity: ActivityUpdate = {
-    type: 'Update',
-    id: url,
-    actor: byAccount.url,
-    to,
-    cc,
-    object
+async function updateActivityData (
+  url: string,
+  byActor: ActorModel,
+  object: any,
+  t: Transaction,
+  audience?: ActivityAudience
+): Promise<ActivityUpdate> {
+  if (!audience) {
+    audience = await getAudience(byActor, t)
   }
 
-  return activity
+  return audiencify({
+    type: 'Update',
+    id: url,
+    actor: byActor.url,
+    object: audiencify(object, audience)
+  }, audience)
 }