Activity Pub improvements
[oweals/peertube.git] / server / lib / activitypub / send / send-like.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub'
3 import { ActorModel } from '../../../models/activitypub/actor'
4 import { VideoModel } from '../../../models/video/video'
5 import { getVideoLikeActivityPubUrl } from '../url'
6 import {
7   audiencify,
8   broadcastToFollowers,
9   getActorsInvolvedInVideo,
10   getAudience,
11   getObjectFollowersAudience,
12   getOriginVideoAudience,
13   unicastTo
14 } from './misc'
15
16 async function sendLikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
17   const url = getVideoLikeActivityPubUrl(byActor, video)
18
19   const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
20   const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
21   const data = await likeActivityData(url, byActor, video, t, audience)
22
23   return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
24 }
25
26 async function sendLikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
27   const url = getVideoLikeActivityPubUrl(byActor, video)
28
29   const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
30   const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
31   const data = await likeActivityData(url, byActor, video, t, audience)
32
33   const followersException = [ byActor ]
34   return broadcastToFollowers(data, byActor, accountsInvolvedInVideo, t, followersException)
35 }
36
37 async function likeActivityData (
38   url: string,
39   byActor: ActorModel,
40   video: VideoModel,
41   t: Transaction,
42   audience?: ActivityAudience
43 ): Promise<ActivityLike> {
44   if (!audience) {
45     audience = await getAudience(byActor, t)
46   }
47
48   return audiencify({
49     type: 'Like',
50     id: url,
51     actor: byActor.url,
52     object: video.url
53   }, audience)
54 }
55
56 // ---------------------------------------------------------------------------
57
58 export {
59   sendLikeToOrigin,
60   sendLikeToVideoFollowers,
61   likeActivityData
62 }