7e0c73796525fe322d6e5867ee0a1f72501ebbe2
[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   broadcastToFollowers,
8   getActorsInvolvedInVideo,
9   getAudience,
10   getObjectFollowersAudience,
11   getOriginVideoAudience,
12   unicastTo
13 } from './misc'
14
15 async function sendLikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
16   const url = getVideoLikeActivityPubUrl(byActor, video)
17
18   const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
19   const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
20   const data = await likeActivityData(url, byActor, video, t, audience)
21
22   return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
23 }
24
25 async function sendLikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
26   const url = getVideoLikeActivityPubUrl(byActor, video)
27
28   const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
29   const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
30   const data = await likeActivityData(url, byActor, video, t, audience)
31
32   const followersException = [ byActor ]
33   return broadcastToFollowers(data, byActor, accountsInvolvedInVideo, t, followersException)
34 }
35
36 async function likeActivityData (
37   url: string,
38   byActor: ActorModel,
39   video: VideoModel,
40   t: Transaction,
41   audience?: ActivityAudience
42 ): Promise<ActivityLike> {
43   if (!audience) {
44     audience = await getAudience(byActor, t)
45   }
46
47   return {
48     type: 'Like',
49     id: url,
50     actor: byActor.url,
51     to: audience.to,
52     cc: audience.cc,
53     object: video.url
54   }
55 }
56
57 // ---------------------------------------------------------------------------
58
59 export {
60   sendLikeToOrigin,
61   sendLikeToVideoFollowers,
62   likeActivityData
63 }