Split types and typings
[oweals/peertube.git] / server / lib / activitypub / send / send-update.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityUpdate } from '../../../../shared/models/activitypub'
3 import { VideoPrivacy } from '../../../../shared/models/videos'
4 import { AccountModel } from '../../../models/account/account'
5 import { VideoModel } from '../../../models/video/video'
6 import { VideoShareModel } from '../../../models/video/video-share'
7 import { getUpdateActivityPubUrl } from '../url'
8 import { broadcastToFollowers, sendVideoRelatedActivity } from './utils'
9 import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience'
10 import { logger } from '../../../helpers/logger'
11 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
12 import {
13   MAccountDefault,
14   MActor,
15   MActorLight,
16   MChannelDefault,
17   MVideoAP,
18   MVideoAPWithoutCaption,
19   MVideoPlaylistFull,
20   MVideoRedundancyVideo
21 } from '../../../types/models'
22 import { getServerActor } from '@server/models/application/application'
23
24 async function sendUpdateVideo (videoArg: MVideoAPWithoutCaption, t: Transaction, overrodeByActor?: MActor) {
25   const video = videoArg as MVideoAP
26
27   if (!video.hasPrivacyForFederation()) return undefined
28
29   logger.info('Creating job to update video %s.', video.url)
30
31   const byActor = overrodeByActor || video.VideoChannel.Account.Actor
32
33   const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
34
35   // Needed to build the AP object
36   if (!video.VideoCaptions) {
37     video.VideoCaptions = await video.$get('VideoCaptions', { transaction: t })
38   }
39
40   const videoObject = video.toActivityPubObject()
41   const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
42
43   const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
44
45   const actorsInvolved = await getActorsInvolvedInVideo(video, t)
46   if (overrodeByActor) actorsInvolved.push(overrodeByActor)
47
48   return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
49 }
50
51 async function sendUpdateActor (accountOrChannel: MChannelDefault | MAccountDefault, t: Transaction) {
52   const byActor = accountOrChannel.Actor
53
54   logger.info('Creating job to update actor %s.', byActor.url)
55
56   const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
57   const accountOrChannelObject = (accountOrChannel as any).toActivityPubObject() // FIXME: typescript bug?
58   const audience = getAudience(byActor)
59   const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
60
61   let actorsInvolved: MActor[]
62   if (accountOrChannel instanceof AccountModel) {
63     // Actors that shared my videos are involved too
64     actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
65   } else {
66     // Actors that shared videos of my channel are involved too
67     actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
68   }
69
70   actorsInvolved.push(byActor)
71
72   return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
73 }
74
75 async function sendUpdateCacheFile (byActor: MActorLight, redundancyModel: MVideoRedundancyVideo) {
76   logger.info('Creating job to update cache file %s.', redundancyModel.url)
77
78   const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.getVideo().id)
79
80   const activityBuilder = (audience: ActivityAudience) => {
81     const redundancyObject = redundancyModel.toActivityPubObject()
82     const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
83
84     return buildUpdateActivity(url, byActor, redundancyObject, audience)
85   }
86
87   return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'CacheFile' })
88 }
89
90 async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, t: Transaction) {
91   if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
92
93   const byActor = videoPlaylist.OwnerAccount.Actor
94
95   logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
96
97   const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
98
99   const object = await videoPlaylist.toActivityPubObject(null, t)
100   const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
101
102   const updateActivity = buildUpdateActivity(url, byActor, object, audience)
103
104   const serverActor = await getServerActor()
105   const toFollowersOf = [ byActor, serverActor ]
106
107   if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
108
109   return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
110 }
111
112 // ---------------------------------------------------------------------------
113
114 export {
115   sendUpdateActor,
116   sendUpdateVideo,
117   sendUpdateCacheFile,
118   sendUpdateVideoPlaylist
119 }
120
121 // ---------------------------------------------------------------------------
122
123 function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate {
124   if (!audience) audience = getAudience(byActor)
125
126   return audiencify(
127     {
128       type: 'Update' as 'Update',
129       id: url,
130       actor: byActor.url,
131       object: audiencify(object, audience)
132     },
133     audience
134   )
135 }