]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-update.ts
Fix redundancy federation in some cases
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-update.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
50d6de9c
C
2import { ActivityAudience, ActivityUpdate } from '../../../../shared/models/activitypub'
3import { VideoPrivacy } from '../../../../shared/models/videos'
2422c46b 4import { AccountModel } from '../../../models/account/account'
3fd3ab2d 5import { VideoModel } from '../../../models/video/video'
3fd3ab2d 6import { VideoShareModel } from '../../../models/video/video-share'
892211e8 7import { getUpdateActivityPubUrl } from '../url'
e5565833
C
8import { broadcastToFollowers, sendVideoRelatedActivity } from './utils'
9import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience'
8e0fd45e 10import { logger } from '../../../helpers/logger'
418d092a 11import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
453e83ea 12import {
b5fecbf4 13 MAccountDefault,
453e83ea
C
14 MActor,
15 MActorLight,
b5fecbf4 16 MChannelDefault,
453e83ea
C
17 MVideoAP,
18 MVideoAPWithoutCaption,
19 MVideoPlaylistFull,
20 MVideoRedundancyVideo
26d6bf65 21} from '../../../types/models'
8dc8a34e 22import { getServerActor } from '@server/models/application/application'
453e83ea
C
23
24async function sendUpdateVideo (videoArg: MVideoAPWithoutCaption, t: Transaction, overrodeByActor?: MActor) {
25 const video = videoArg as MVideoAP
54141398 26
22a73cb8 27 if (!video.hasPrivacyForFederation()) return undefined
418d092a 28
8e0fd45e
C
29 logger.info('Creating job to update video %s.', video.url)
30
a1587156 31 const byActor = overrodeByActor || video.VideoChannel.Account.Actor
54141398
C
32
33 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
5cf84858
C
34
35 // Needed to build the AP object
2284f202 36 if (!video.VideoCaptions) {
e6122097 37 video.VideoCaptions = await video.$get('VideoCaptions', { transaction: t })
2284f202 38 }
5cf84858 39
54141398 40 const videoObject = video.toActivityPubObject()
2186386c 41 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
50d6de9c 42
c48e82b5 43 const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
54141398 44
c48e82b5
C
45 const actorsInvolved = await getActorsInvolvedInVideo(video, t)
46 if (overrodeByActor) actorsInvolved.push(overrodeByActor)
54141398 47
c48e82b5 48 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
54141398
C
49}
50
b5fecbf4 51async function sendUpdateActor (accountOrChannel: MChannelDefault | MAccountDefault, t: Transaction) {
2422c46b 52 const byActor = accountOrChannel.Actor
265ba139 53
8e0fd45e
C
54 logger.info('Creating job to update actor %s.', byActor.url)
55
265ba139 56 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
d5d9b6d7 57 const accountOrChannelObject = (accountOrChannel as any).toActivityPubObject() // FIXME: typescript bug?
2186386c 58 const audience = getAudience(byActor)
c48e82b5 59 const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
2422c46b 60
453e83ea 61 let actorsInvolved: MActor[]
2422c46b
C
62 if (accountOrChannel instanceof AccountModel) {
63 // Actors that shared my videos are involved too
df0b219d 64 actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
2422c46b
C
65 } else {
66 // Actors that shared videos of my channel are involved too
67 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
68 }
265ba139 69
265ba139
C
70 actorsInvolved.push(byActor)
71
c48e82b5
C
72 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
73}
74
453e83ea 75async function sendUpdateCacheFile (byActor: MActorLight, redundancyModel: MVideoRedundancyVideo) {
c48e82b5
C
76 logger.info('Creating job to update cache file %s.', redundancyModel.url)
77
9cfeb3cf
C
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)
c48e82b5 85
a2377d15
C
86 const activityBuilder = (audience: ActivityAudience) => {
87 const redundancyObject = redundancyModel.toActivityPubObject()
88 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
c48e82b5 89
a2377d15
C
90 return buildUpdateActivity(url, byActor, redundancyObject, audience)
91 }
c48e82b5 92
084a2cd0 93 return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'CacheFile' })
265ba139
C
94}
95
453e83ea 96async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, t: Transaction) {
418d092a
C
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
df0b219d 105 const object = await videoPlaylist.toActivityPubObject(null, t)
418d092a
C
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
54141398
C
118// ---------------------------------------------------------------------------
119
120export {
2422c46b 121 sendUpdateActor,
c48e82b5 122 sendUpdateVideo,
418d092a
C
123 sendUpdateCacheFile,
124 sendUpdateVideoPlaylist
54141398
C
125}
126
127// ---------------------------------------------------------------------------
128
453e83ea 129function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate {
2186386c 130 if (!audience) audience = getAudience(byActor)
50d6de9c 131
2186386c
C
132 return audiencify(
133 {
134 type: 'Update' as 'Update',
135 id: url,
136 actor: byActor.url,
453e83ea 137 object: audiencify(object, audience)
2186386c
C
138 },
139 audience
140 )
54141398 141}