]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-update.ts
Fix redundancy federation in some cases
[github/Chocobozzz/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 associatedVideo = redundancyModel.getVideo()
79 if (!associatedVideo) {
80 logger.warn('Cannot send update activity for redundancy %s: no video files associated.', redundancyModel.url)
81 return
82 }
83
84 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(associatedVideo.id)
85
86 const activityBuilder = (audience: ActivityAudience) => {
87 const redundancyObject = redundancyModel.toActivityPubObject()
88 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
89
90 return buildUpdateActivity(url, byActor, redundancyObject, audience)
91 }
92
93 return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'CacheFile' })
94 }
95
96 async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, t: Transaction) {
97 if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
98
99 const byActor = videoPlaylist.OwnerAccount.Actor
100
101 logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
102
103 const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
104
105 const object = await videoPlaylist.toActivityPubObject(null, t)
106 const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
107
108 const updateActivity = buildUpdateActivity(url, byActor, object, audience)
109
110 const serverActor = await getServerActor()
111 const toFollowersOf = [ byActor, serverActor ]
112
113 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
114
115 return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
116 }
117
118 // ---------------------------------------------------------------------------
119
120 export {
121 sendUpdateActor,
122 sendUpdateVideo,
123 sendUpdateCacheFile,
124 sendUpdateVideoPlaylist
125 }
126
127 // ---------------------------------------------------------------------------
128
129 function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate {
130 if (!audience) audience = getAudience(byActor)
131
132 return audiencify(
133 {
134 type: 'Update' as 'Update',
135 id: url,
136 actor: byActor.url,
137 object: audiencify(object, audience)
138 },
139 audience
140 )
141 }