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