]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-update.ts
a68f03edf35df20e22ae678959d90d4f749bc664
[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 { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoChannelModel } from '../../../models/video/video-channel'
8 import { VideoShareModel } from '../../../models/video/video-share'
9 import { getUpdateActivityPubUrl } from '../url'
10 import { broadcastToFollowers, sendVideoRelatedActivity } from './utils'
11 import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience'
12 import { logger } from '../../../helpers/logger'
13 import { VideoCaptionModel } from '../../../models/video/video-caption'
14 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
15
16 async function sendUpdateVideo (video: VideoModel, t: Transaction, overrodeByActor?: ActorModel) {
17 logger.info('Creating job to update video %s.', video.url)
18
19 const byActor = overrodeByActor ? overrodeByActor : video.VideoChannel.Account.Actor
20
21 const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
22
23 // Needed to build the AP object
24 if (!video.VideoCaptions) video.VideoCaptions = await video.$get('VideoCaptions') as VideoCaptionModel[]
25
26 const videoObject = video.toActivityPubObject()
27 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
28
29 const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
30
31 const actorsInvolved = await getActorsInvolvedInVideo(video, t)
32 if (overrodeByActor) actorsInvolved.push(overrodeByActor)
33
34 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
35 }
36
37 async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelModel, t: Transaction) {
38 const byActor = accountOrChannel.Actor
39
40 logger.info('Creating job to update actor %s.', byActor.url)
41
42 const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
43 const accountOrChannelObject = accountOrChannel.toActivityPubObject()
44 const audience = getAudience(byActor)
45 const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
46
47 let actorsInvolved: ActorModel[]
48 if (accountOrChannel instanceof AccountModel) {
49 // Actors that shared my videos are involved too
50 actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t)
51 } else {
52 // Actors that shared videos of my channel are involved too
53 actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
54 }
55
56 actorsInvolved.push(byActor)
57
58 return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
59 }
60
61 async function sendUpdateCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel) {
62 logger.info('Creating job to update cache file %s.', redundancyModel.url)
63
64 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.VideoFile.Video.id)
65
66 const activityBuilder = (audience: ActivityAudience) => {
67 const redundancyObject = redundancyModel.toActivityPubObject()
68 const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
69
70 return buildUpdateActivity(url, byActor, redundancyObject, audience)
71 }
72
73 return sendVideoRelatedActivity(activityBuilder, { byActor, video })
74 }
75
76 // ---------------------------------------------------------------------------
77
78 export {
79 sendUpdateActor,
80 sendUpdateVideo,
81 sendUpdateCacheFile
82 }
83
84 // ---------------------------------------------------------------------------
85
86 function buildUpdateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityUpdate {
87 if (!audience) audience = getAudience(byActor)
88
89 return audiencify(
90 {
91 type: 'Update' as 'Update',
92 id: url,
93 actor: byActor.url,
94 object: audiencify(object, audience
95 )
96 },
97 audience
98 )
99 }