]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-like.ts
Federate likes/dislikes
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-like.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityLike } from '../../../../shared/models/activitypub/activity'
3 import { getServerAccount } from '../../../helpers/utils'
4 import { AccountInstance, VideoInstance } from '../../../models'
5 import { getVideoLikeActivityPubUrl } from '../url'
6 import {
7 broadcastToFollowers,
8 getAccountsToForwardVideoAction,
9 getAudience,
10 getOriginVideoAudience,
11 getVideoFollowersAudience,
12 unicastTo
13 } from './misc'
14
15 async function sendLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
16 const url = getVideoLikeActivityPubUrl(byAccount, video)
17
18 const audience = getOriginVideoAudience(video)
19 const data = await likeActivityData(url, byAccount, video, audience)
20
21 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
22 }
23
24 async function sendLikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
25 const url = getVideoLikeActivityPubUrl(byAccount, video)
26
27 const audience = getVideoFollowersAudience(video)
28 const data = await likeActivityData(url, byAccount, video, audience)
29
30 const accountsToForwardView = await getAccountsToForwardVideoAction(byAccount, video)
31 const serverAccount = await getServerAccount()
32
33 const followersException = [ byAccount ]
34 return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
35 }
36
37 async function likeActivityData (url: string, byAccount: AccountInstance, video: VideoInstance, audience?: { to: string[], cc: string[] }) {
38 if (!audience) {
39 audience = await getAudience(byAccount)
40 }
41
42 const activity: ActivityLike = {
43 type: 'Like',
44 id: url,
45 actor: byAccount.url,
46 to: audience.to,
47 cc: audience.cc,
48 object: video.url
49 }
50
51 return activity
52 }
53
54 // ---------------------------------------------------------------------------
55
56 export {
57 sendLikeToOrigin,
58 sendLikeToVideoFollowers,
59 likeActivityData
60 }