]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { Transaction } from 'sequelize' |
57e4e1c1 C |
2 | import { getServerActor } from '@server/models/application/application' |
3 | import { ActivityAudience, ActivityUpdate, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' | |
4 | import { logger } from '../../../helpers/logger' | |
2422c46b | 5 | import { AccountModel } from '../../../models/account/account' |
3fd3ab2d | 6 | import { VideoModel } from '../../../models/video/video' |
3fd3ab2d | 7 | import { VideoShareModel } from '../../../models/video/video-share' |
453e83ea | 8 | import { |
b5fecbf4 | 9 | MAccountDefault, |
453e83ea C |
10 | MActor, |
11 | MActorLight, | |
b5fecbf4 | 12 | MChannelDefault, |
453e83ea C |
13 | MVideoAP, |
14 | MVideoAPWithoutCaption, | |
15 | MVideoPlaylistFull, | |
16 | MVideoRedundancyVideo | |
26d6bf65 | 17 | } from '../../../types/models' |
57e4e1c1 C |
18 | import { audiencify, getAudience } from '../audience' |
19 | import { getUpdateActivityPubUrl } from '../url' | |
20 | import { getActorsInvolvedInVideo } from './shared' | |
21 | import { broadcastToFollowers, sendVideoRelatedActivity } from './shared/send-utils' | |
453e83ea | 22 | |
a219c910 | 23 | async function sendUpdateVideo (videoArg: MVideoAPWithoutCaption, transaction: Transaction, overriddenByActor?: MActor) { |
453e83ea | 24 | const video = videoArg as MVideoAP |
54141398 | 25 | |
22a73cb8 | 26 | if (!video.hasPrivacyForFederation()) return undefined |
418d092a | 27 | |
8e0fd45e C |
28 | logger.info('Creating job to update video %s.', video.url) |
29 | ||
a219c910 | 30 | const byActor = overriddenByActor || video.VideoChannel.Account.Actor |
54141398 C |
31 | |
32 | const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString()) | |
5cf84858 C |
33 | |
34 | // Needed to build the AP object | |
2284f202 | 35 | if (!video.VideoCaptions) { |
a219c910 | 36 | video.VideoCaptions = await video.$get('VideoCaptions', { transaction }) |
2284f202 | 37 | } |
5cf84858 | 38 | |
54141398 | 39 | const videoObject = video.toActivityPubObject() |
2186386c | 40 | const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC) |
50d6de9c | 41 | |
c48e82b5 | 42 | const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience) |
54141398 | 43 | |
a219c910 C |
44 | const actorsInvolved = await getActorsInvolvedInVideo(video, transaction) |
45 | if (overriddenByActor) actorsInvolved.push(overriddenByActor) | |
54141398 | 46 | |
a219c910 C |
47 | return broadcastToFollowers({ |
48 | data: updateActivity, | |
49 | byActor, | |
50 | toFollowersOf: actorsInvolved, | |
51 | contextType: 'Video', | |
52 | transaction | |
53 | }) | |
54141398 C |
54 | } |
55 | ||
a219c910 | 56 | async function sendUpdateActor (accountOrChannel: MChannelDefault | MAccountDefault, transaction: Transaction) { |
2422c46b | 57 | const byActor = accountOrChannel.Actor |
265ba139 | 58 | |
8e0fd45e C |
59 | logger.info('Creating job to update actor %s.', byActor.url) |
60 | ||
265ba139 | 61 | const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString()) |
d5d9b6d7 | 62 | const accountOrChannelObject = (accountOrChannel as any).toActivityPubObject() // FIXME: typescript bug? |
2186386c | 63 | const audience = getAudience(byActor) |
c48e82b5 | 64 | const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience) |
2422c46b | 65 | |
453e83ea | 66 | let actorsInvolved: MActor[] |
2422c46b C |
67 | if (accountOrChannel instanceof AccountModel) { |
68 | // Actors that shared my videos are involved too | |
a219c910 | 69 | actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, transaction) |
2422c46b C |
70 | } else { |
71 | // Actors that shared videos of my channel are involved too | |
a219c910 | 72 | actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, transaction) |
2422c46b | 73 | } |
265ba139 | 74 | |
265ba139 C |
75 | actorsInvolved.push(byActor) |
76 | ||
a219c910 C |
77 | return broadcastToFollowers({ |
78 | data: updateActivity, | |
79 | byActor, | |
80 | toFollowersOf: actorsInvolved, | |
81 | transaction, | |
82 | contextType: 'Actor' | |
83 | }) | |
c48e82b5 C |
84 | } |
85 | ||
453e83ea | 86 | async function sendUpdateCacheFile (byActor: MActorLight, redundancyModel: MVideoRedundancyVideo) { |
c48e82b5 C |
87 | logger.info('Creating job to update cache file %s.', redundancyModel.url) |
88 | ||
9cfeb3cf C |
89 | const associatedVideo = redundancyModel.getVideo() |
90 | if (!associatedVideo) { | |
91 | logger.warn('Cannot send update activity for redundancy %s: no video files associated.', redundancyModel.url) | |
92 | return | |
93 | } | |
94 | ||
95 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(associatedVideo.id) | |
c48e82b5 | 96 | |
a2377d15 C |
97 | const activityBuilder = (audience: ActivityAudience) => { |
98 | const redundancyObject = redundancyModel.toActivityPubObject() | |
99 | const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString()) | |
c48e82b5 | 100 | |
a2377d15 C |
101 | return buildUpdateActivity(url, byActor, redundancyObject, audience) |
102 | } | |
c48e82b5 | 103 | |
084a2cd0 | 104 | return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'CacheFile' }) |
265ba139 C |
105 | } |
106 | ||
a219c910 | 107 | async function sendUpdateVideoPlaylist (videoPlaylist: MVideoPlaylistFull, transaction: Transaction) { |
418d092a C |
108 | if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined |
109 | ||
110 | const byActor = videoPlaylist.OwnerAccount.Actor | |
111 | ||
112 | logger.info('Creating job to update video playlist %s.', videoPlaylist.url) | |
113 | ||
114 | const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString()) | |
115 | ||
a219c910 | 116 | const object = await videoPlaylist.toActivityPubObject(null, transaction) |
418d092a C |
117 | const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC) |
118 | ||
119 | const updateActivity = buildUpdateActivity(url, byActor, object, audience) | |
120 | ||
121 | const serverActor = await getServerActor() | |
122 | const toFollowersOf = [ byActor, serverActor ] | |
123 | ||
124 | if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor) | |
125 | ||
a219c910 C |
126 | return broadcastToFollowers({ |
127 | data: updateActivity, | |
128 | byActor, | |
129 | toFollowersOf, | |
130 | transaction, | |
131 | contextType: 'Playlist' | |
132 | }) | |
418d092a C |
133 | } |
134 | ||
54141398 C |
135 | // --------------------------------------------------------------------------- |
136 | ||
137 | export { | |
2422c46b | 138 | sendUpdateActor, |
c48e82b5 | 139 | sendUpdateVideo, |
418d092a C |
140 | sendUpdateCacheFile, |
141 | sendUpdateVideoPlaylist | |
54141398 C |
142 | } |
143 | ||
144 | // --------------------------------------------------------------------------- | |
145 | ||
453e83ea | 146 | function buildUpdateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityUpdate { |
2186386c | 147 | if (!audience) audience = getAudience(byActor) |
50d6de9c | 148 | |
2186386c C |
149 | return audiencify( |
150 | { | |
151 | type: 'Update' as 'Update', | |
152 | id: url, | |
153 | actor: byActor.url, | |
ab18fadf | 154 | object: audiencify(object, audience) |
2186386c C |
155 | }, |
156 | audience | |
157 | ) | |
54141398 | 158 | } |