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