]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/send/send-like.ts
Fix incorrect IDs in AP federation
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-like.ts
index 1a35d0db063067d532d40543bbf13c86fb8dfe5d..ed6dfcf56d398f46291ad43ef08dc375a9f012be 100644 (file)
@@ -1,63 +1,40 @@
 import { Transaction } from 'sequelize'
 import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub'
-import { AccountModel } from '../../../models/account/account'
-import { VideoModel } from '../../../models/video/video'
-import { getVideoLikeActivityPubUrl } from '../url'
-import {
-  broadcastToFollowers,
-  getAccountsInvolvedInVideo,
-  getAudience,
-  getOriginVideoAudience,
-  getObjectFollowersAudience,
-  unicastTo
-} from './misc'
-
-async function sendLikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
-  const url = getVideoLikeActivityPubUrl(byAccount, video)
-
-  const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
-  const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
-  const data = await likeActivityData(url, byAccount, video, t, audience)
-
-  return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
-}
-
-async function sendLikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
-  const url = getVideoLikeActivityPubUrl(byAccount, video)
+import { getVideoLikeActivityPubUrlByLocalActor } from '../url'
+import { sendVideoRelatedActivity } from './utils'
+import { audiencify, getAudience } from '../audience'
+import { logger } from '../../../helpers/logger'
+import { MActor, MActorAudience, MVideoAccountLight, MVideoUrl } from '../../../types/models'
 
-  const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
-  const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
-  const data = await likeActivityData(url, byAccount, video, t, audience)
+function sendLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
+  logger.info('Creating job to like %s.', video.url)
 
-  const followersException = [ byAccount ]
-  return broadcastToFollowers(data, byAccount, accountsInvolvedInVideo, t, followersException)
-}
+  const activityBuilder = (audience: ActivityAudience) => {
+    const url = getVideoLikeActivityPubUrlByLocalActor(byActor, video)
 
-async function likeActivityData (
-  url: string,
-  byAccount: AccountModel,
-  video: VideoModel,
-  t: Transaction,
-  audience?: ActivityAudience
-): Promise<ActivityLike> {
-  if (!audience) {
-    audience = await getAudience(byAccount, t)
+    return buildLikeActivity(url, byActor, video, audience)
   }
 
-  return {
-    type: 'Like',
-    id: url,
-    actor: byAccount.url,
-    to: audience.to,
-    cc: audience.cc,
-    object: video.url
-  }
+  return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction: t })
+}
+
+function buildLikeActivity (url: string, byActor: MActorAudience, video: MVideoUrl, audience?: ActivityAudience): ActivityLike {
+  if (!audience) audience = getAudience(byActor)
+
+  return audiencify(
+    {
+      id: url,
+      type: 'Like' as 'Like',
+      actor: byActor.url,
+      object: video.url
+    },
+    audience
+  )
 }
 
 // ---------------------------------------------------------------------------
 
 export {
-  sendLikeToOrigin,
-  sendLikeToVideoFollowers,
-  likeActivityData
+  sendLike,
+  buildLikeActivity
 }