]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { Transaction } from 'sequelize' |
3fd3ab2d | 2 | import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub' |
50d6de9c | 3 | import { VideoPrivacy } from '../../../../shared/models/videos' |
50d6de9c | 4 | import { ActorModel } from '../../../models/activitypub/actor' |
3fd3ab2d | 5 | import { VideoModel } from '../../../models/video/video' |
ea44f375 | 6 | import { VideoCommentModel } from '../../../models/video/video-comment' |
a2377d15 C |
7 | import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils' |
8 | import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience' | |
8e0fd45e | 9 | import { logger } from '../../../helpers/logger' |
c48e82b5 | 10 | import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' |
418d092a C |
11 | import { VideoPlaylistModel } from '../../../models/video/video-playlist' |
12 | import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model' | |
13 | import { getServerActor } from '../../../helpers/utils' | |
54141398 | 14 | |
50d6de9c | 15 | async function sendCreateVideo (video: VideoModel, t: Transaction) { |
54b38063 | 16 | if (video.privacy === VideoPrivacy.PRIVATE) return undefined |
54141398 | 17 | |
8e0fd45e C |
18 | logger.info('Creating job to send video creation of %s.', video.url) |
19 | ||
e12a0092 | 20 | const byActor = video.VideoChannel.Account.Actor |
50d6de9c | 21 | const videoObject = video.toActivityPubObject() |
e12a0092 | 22 | |
2186386c | 23 | const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC) |
c48e82b5 | 24 | const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience) |
54141398 | 25 | |
c48e82b5 | 26 | return broadcastToFollowers(createActivity, byActor, [ byActor ], t) |
54141398 C |
27 | } |
28 | ||
09209296 | 29 | async function sendCreateCacheFile (byActor: ActorModel, video: VideoModel, fileRedundancy: VideoRedundancyModel) { |
c48e82b5 C |
30 | logger.info('Creating job to send file cache of %s.', fileRedundancy.url) |
31 | ||
a2377d15 C |
32 | return sendVideoRelatedCreateActivity({ |
33 | byActor, | |
34 | video, | |
35 | url: fileRedundancy.url, | |
09209296 | 36 | object: fileRedundancy.toActivityPubObject() |
a2377d15 | 37 | }) |
40ff5707 C |
38 | } |
39 | ||
418d092a C |
40 | async function sendCreateVideoPlaylist (playlist: VideoPlaylistModel, t: Transaction) { |
41 | if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined | |
42 | ||
43 | logger.info('Creating job to send create video playlist of %s.', playlist.url) | |
44 | ||
45 | const byActor = playlist.OwnerAccount.Actor | |
46 | const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC) | |
47 | ||
df0b219d | 48 | const object = await playlist.toActivityPubObject(null, t) |
418d092a C |
49 | const createActivity = buildCreateActivity(playlist.url, byActor, object, audience) |
50 | ||
51 | const serverActor = await getServerActor() | |
52 | const toFollowersOf = [ byActor, serverActor ] | |
53 | ||
54 | if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor) | |
55 | ||
56 | return broadcastToFollowers(createActivity, byActor, toFollowersOf, t) | |
57 | } | |
58 | ||
07197db4 | 59 | async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) { |
8e0fd45e C |
60 | logger.info('Creating job to send comment %s.', comment.url) |
61 | ||
07197db4 C |
62 | const isOrigin = comment.Video.isOwned() |
63 | ||
ea44f375 | 64 | const byActor = comment.Account.Actor |
d7e70384 C |
65 | const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t) |
66 | const commentObject = comment.toActivityPubObject(threadParentComments) | |
ea44f375 | 67 | |
93ef8a9d | 68 | const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t) |
a2377d15 | 69 | // Add the actor that commented too |
93ef8a9d | 70 | actorsInvolvedInComment.push(byActor) |
93ef8a9d | 71 | |
07197db4 | 72 | const parentsCommentActors = threadParentComments.map(c => c.Account.Actor) |
ea44f375 | 73 | |
07197db4 C |
74 | let audience: ActivityAudience |
75 | if (isOrigin) { | |
76 | audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin) | |
77 | } else { | |
a2377d15 | 78 | audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors)) |
07197db4 | 79 | } |
93ef8a9d | 80 | |
c48e82b5 | 81 | const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience) |
ea44f375 | 82 | |
93ef8a9d C |
83 | // This was a reply, send it to the parent actors |
84 | const actorsException = [ byActor ] | |
c48e82b5 | 85 | await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException) |
93ef8a9d C |
86 | |
87 | // Broadcast to our followers | |
c48e82b5 | 88 | await broadcastToFollowers(createActivity, byActor, [ byActor ], t) |
93ef8a9d C |
89 | |
90 | // Send to actors involved in the comment | |
c48e82b5 | 91 | if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException) |
07197db4 C |
92 | |
93 | // Send to origin | |
c48e82b5 | 94 | return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl) |
ea44f375 C |
95 | } |
96 | ||
c48e82b5 | 97 | function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate { |
2186386c C |
98 | if (!audience) audience = getAudience(byActor) |
99 | ||
100 | return audiencify( | |
101 | { | |
102 | type: 'Create' as 'Create', | |
103 | id: url + '/activity', | |
104 | actor: byActor.url, | |
105 | object: audiencify(object, audience) | |
106 | }, | |
107 | audience | |
108 | ) | |
54141398 C |
109 | } |
110 | ||
111 | // --------------------------------------------------------------------------- | |
112 | ||
113 | export { | |
50d6de9c | 114 | sendCreateVideo, |
c48e82b5 | 115 | buildCreateActivity, |
c48e82b5 | 116 | sendCreateVideoComment, |
418d092a | 117 | sendCreateVideoPlaylist, |
c48e82b5 | 118 | sendCreateCacheFile |
54141398 | 119 | } |
a2377d15 C |
120 | |
121 | // --------------------------------------------------------------------------- | |
122 | ||
123 | async function sendVideoRelatedCreateActivity (options: { | |
124 | byActor: ActorModel, | |
125 | video: VideoModel, | |
126 | url: string, | |
127 | object: any, | |
128 | transaction?: Transaction | |
129 | }) { | |
130 | const activityBuilder = (audience: ActivityAudience) => { | |
131 | return buildCreateActivity(options.url, options.byActor, options.object, audience) | |
132 | } | |
133 | ||
134 | return sendVideoRelatedActivity(activityBuilder, options) | |
135 | } |