]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { Transaction } from 'sequelize' |
50d6de9c C |
2 | import { ActivityAudience, ActivityUpdate } from '../../../../shared/models/activitypub' |
3 | import { VideoPrivacy } from '../../../../shared/models/videos' | |
2422c46b | 4 | import { AccountModel } from '../../../models/account/account' |
50d6de9c | 5 | import { ActorModel } from '../../../models/activitypub/actor' |
3fd3ab2d | 6 | import { VideoModel } from '../../../models/video/video' |
2422c46b | 7 | import { VideoChannelModel } from '../../../models/video/video-channel' |
3fd3ab2d | 8 | import { VideoShareModel } from '../../../models/video/video-share' |
892211e8 | 9 | import { getUpdateActivityPubUrl } from '../url' |
e5565833 C |
10 | import { broadcastToFollowers, sendVideoRelatedActivity } from './utils' |
11 | import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience' | |
8e0fd45e | 12 | import { logger } from '../../../helpers/logger' |
5cf84858 | 13 | import { VideoCaptionModel } from '../../../models/video/video-caption' |
c48e82b5 | 14 | import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' |
418d092a C |
15 | import { VideoPlaylistModel } from '../../../models/video/video-playlist' |
16 | import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model' | |
17 | import { getServerActor } from '../../../helpers/utils' | |
54141398 | 18 | |
5cf84858 | 19 | async function sendUpdateVideo (video: VideoModel, t: Transaction, overrodeByActor?: ActorModel) { |
418d092a C |
20 | if (video.privacy === VideoPrivacy.PRIVATE) return undefined |
21 | ||
8e0fd45e C |
22 | logger.info('Creating job to update video %s.', video.url) |
23 | ||
5cf84858 | 24 | const byActor = overrodeByActor ? overrodeByActor : video.VideoChannel.Account.Actor |
54141398 C |
25 | |
26 | const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString()) | |
5cf84858 C |
27 | |
28 | // Needed to build the AP object | |
29 | if (!video.VideoCaptions) video.VideoCaptions = await video.$get('VideoCaptions') as VideoCaptionModel[] | |
30 | ||
54141398 | 31 | const videoObject = video.toActivityPubObject() |
2186386c | 32 | const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC) |
50d6de9c | 33 | |
c48e82b5 | 34 | const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience) |
54141398 | 35 | |
c48e82b5 C |
36 | const actorsInvolved = await getActorsInvolvedInVideo(video, t) |
37 | if (overrodeByActor) actorsInvolved.push(overrodeByActor) | |
54141398 | 38 | |
c48e82b5 | 39 | return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t) |
54141398 C |
40 | } |
41 | ||
2422c46b C |
42 | async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelModel, t: Transaction) { |
43 | const byActor = accountOrChannel.Actor | |
265ba139 | 44 | |
8e0fd45e C |
45 | logger.info('Creating job to update actor %s.', byActor.url) |
46 | ||
265ba139 | 47 | const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString()) |
2422c46b | 48 | const accountOrChannelObject = accountOrChannel.toActivityPubObject() |
2186386c | 49 | const audience = getAudience(byActor) |
c48e82b5 | 50 | const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience) |
2422c46b C |
51 | |
52 | let actorsInvolved: ActorModel[] | |
53 | if (accountOrChannel instanceof AccountModel) { | |
54 | // Actors that shared my videos are involved too | |
df0b219d | 55 | actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t) |
2422c46b C |
56 | } else { |
57 | // Actors that shared videos of my channel are involved too | |
58 | actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t) | |
59 | } | |
265ba139 | 60 | |
265ba139 C |
61 | actorsInvolved.push(byActor) |
62 | ||
c48e82b5 C |
63 | return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t) |
64 | } | |
65 | ||
66 | async function sendUpdateCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel) { | |
67 | logger.info('Creating job to update cache file %s.', redundancyModel.url) | |
68 | ||
09209296 | 69 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.getVideo().id) |
c48e82b5 | 70 | |
a2377d15 C |
71 | const activityBuilder = (audience: ActivityAudience) => { |
72 | const redundancyObject = redundancyModel.toActivityPubObject() | |
73 | const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString()) | |
c48e82b5 | 74 | |
a2377d15 C |
75 | return buildUpdateActivity(url, byActor, redundancyObject, audience) |
76 | } | |
c48e82b5 | 77 | |
a2377d15 | 78 | return sendVideoRelatedActivity(activityBuilder, { byActor, video }) |
265ba139 C |
79 | } |
80 | ||
418d092a C |
81 | async function sendUpdateVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) { |
82 | if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined | |
83 | ||
84 | const byActor = videoPlaylist.OwnerAccount.Actor | |
85 | ||
86 | logger.info('Creating job to update video playlist %s.', videoPlaylist.url) | |
87 | ||
88 | const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString()) | |
89 | ||
df0b219d | 90 | const object = await videoPlaylist.toActivityPubObject(null, t) |
418d092a C |
91 | const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC) |
92 | ||
93 | const updateActivity = buildUpdateActivity(url, byActor, object, audience) | |
94 | ||
95 | const serverActor = await getServerActor() | |
96 | const toFollowersOf = [ byActor, serverActor ] | |
97 | ||
98 | if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor) | |
99 | ||
100 | return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t) | |
101 | } | |
102 | ||
54141398 C |
103 | // --------------------------------------------------------------------------- |
104 | ||
105 | export { | |
2422c46b | 106 | sendUpdateActor, |
c48e82b5 | 107 | sendUpdateVideo, |
418d092a C |
108 | sendUpdateCacheFile, |
109 | sendUpdateVideoPlaylist | |
54141398 C |
110 | } |
111 | ||
112 | // --------------------------------------------------------------------------- | |
113 | ||
c48e82b5 | 114 | function buildUpdateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityUpdate { |
2186386c | 115 | if (!audience) audience = getAudience(byActor) |
50d6de9c | 116 | |
2186386c C |
117 | return audiencify( |
118 | { | |
119 | type: 'Update' as 'Update', | |
120 | id: url, | |
121 | actor: byActor.url, | |
122 | object: audiencify(object, audience | |
123 | ) | |
124 | }, | |
125 | audience | |
126 | ) | |
54141398 | 127 | } |