]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-like.ts
Put activity pub sends inside transactions
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-like.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub/activity'
3 import { AccountInstance, VideoInstance } from '../../../models'
4 import { getVideoLikeActivityPubUrl } from '../url'
5 import {
6 broadcastToFollowers,
7 getAccountsInvolvedInVideo,
8 getAudience,
9 getOriginVideoAudience,
10 getObjectFollowersAudience,
11 unicastTo
12 } from './misc'
13
14 async function sendLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
15 const url = getVideoLikeActivityPubUrl(byAccount, video)
16
17 const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
18 const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
19 const data = await likeActivityData(url, byAccount, video, t, 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 accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
28 const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
29 const data = await likeActivityData(url, byAccount, video, t, audience)
30
31 const followersException = [ byAccount ]
32 return broadcastToFollowers(data, byAccount, accountsInvolvedInVideo, t, followersException)
33 }
34
35 async function likeActivityData (
36 url: string,
37 byAccount: AccountInstance,
38 video: VideoInstance,
39 t: Transaction,
40 audience?: ActivityAudience
41 ) {
42 if (!audience) {
43 audience = await getAudience(byAccount, t)
44 }
45
46 const activity: ActivityLike = {
47 type: 'Like',
48 id: url,
49 actor: byAccount.url,
50 to: audience.to,
51 cc: audience.cc,
52 object: video.url
53 }
54
55 return activity
56 }
57
58 // ---------------------------------------------------------------------------
59
60 export {
61 sendLikeToOrigin,
62 sendLikeToVideoFollowers,
63 likeActivityData
64 }