Move models to typescript-sequelize
[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 { AccountModel } from '../../../models/account/account'
4 import { VideoModel } from '../../../models/video/video'
5 import { getVideoLikeActivityPubUrl } from '../url'
6 import {
7   broadcastToFollowers,
8   getAccountsInvolvedInVideo,
9   getAudience,
10   getOriginVideoAudience,
11   getObjectFollowersAudience,
12   unicastTo
13 } from './misc'
14
15 async function sendLikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
16   const url = getVideoLikeActivityPubUrl(byAccount, video)
17
18   const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
19   const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
20   const data = await likeActivityData(url, byAccount, video, t, audience)
21
22   return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
23 }
24
25 async function sendLikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
26   const url = getVideoLikeActivityPubUrl(byAccount, video)
27
28   const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
29   const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
30   const data = await likeActivityData(url, byAccount, video, t, audience)
31
32   const followersException = [ byAccount ]
33   return broadcastToFollowers(data, byAccount, accountsInvolvedInVideo, t, followersException)
34 }
35
36 async function likeActivityData (
37   url: string,
38   byAccount: AccountModel,
39   video: VideoModel,
40   t: Transaction,
41   audience?: ActivityAudience
42 ): Promise<ActivityLike> {
43   if (!audience) {
44     audience = await getAudience(byAccount, t)
45   }
46
47   return {
48     type: 'Like',
49     id: url,
50     actor: byAccount.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 }