]>
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' |
da854ddd | 4 | import { getServerActor } from '../../../helpers/utils' |
50d6de9c | 5 | import { ActorModel } from '../../../models/activitypub/actor' |
3fd3ab2d C |
6 | import { VideoModel } from '../../../models/video/video' |
7 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | |
ea44f375 | 8 | import { VideoCommentModel } from '../../../models/video/video-comment' |
0032ebe9 | 9 | import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url' |
e251f170 | 10 | import { broadcastToActors, broadcastToFollowers, unicastTo } from './utils' |
0032ebe9 | 11 | import { |
94a5ff8a | 12 | audiencify, |
94a5ff8a C |
13 | getActorsInvolvedInVideo, |
14 | getAudience, | |
15 | getObjectFollowersAudience, | |
e251f170 C |
16 | getVideoAudience, |
17 | getVideoCommentAudience | |
18 | } from '../audience' | |
8e0fd45e | 19 | import { logger } from '../../../helpers/logger' |
54141398 | 20 | |
50d6de9c | 21 | async function sendCreateVideo (video: VideoModel, t: Transaction) { |
54b38063 | 22 | if (video.privacy === VideoPrivacy.PRIVATE) return undefined |
54141398 | 23 | |
8e0fd45e C |
24 | logger.info('Creating job to send video creation of %s.', video.url) |
25 | ||
e12a0092 | 26 | const byActor = video.VideoChannel.Account.Actor |
50d6de9c | 27 | const videoObject = video.toActivityPubObject() |
e12a0092 | 28 | |
2186386c C |
29 | const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC) |
30 | const data = createActivityData(video.url, byActor, videoObject, audience) | |
54141398 | 31 | |
50d6de9c | 32 | return broadcastToFollowers(data, byActor, [ byActor ], t) |
54141398 C |
33 | } |
34 | ||
50d6de9c | 35 | async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) { |
06a05d5f C |
36 | if (!video.VideoChannel.Account.Actor.serverId) return // Local |
37 | ||
54141398 | 38 | const url = getVideoAbuseActivityPubUrl(videoAbuse) |
40ff5707 | 39 | |
8e0fd45e C |
40 | logger.info('Creating job to send video abuse %s.', url) |
41 | ||
50d6de9c | 42 | const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] } |
2186386c | 43 | const data = createActivityData(url, byActor, videoAbuse.toActivityPubObject(), audience) |
40ff5707 | 44 | |
94a5ff8a | 45 | return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) |
40ff5707 C |
46 | } |
47 | ||
07197db4 | 48 | async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) { |
8e0fd45e C |
49 | logger.info('Creating job to send comment %s.', comment.url) |
50 | ||
07197db4 C |
51 | const isOrigin = comment.Video.isOwned() |
52 | ||
ea44f375 | 53 | const byActor = comment.Account.Actor |
d7e70384 C |
54 | const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t) |
55 | const commentObject = comment.toActivityPubObject(threadParentComments) | |
ea44f375 | 56 | |
93ef8a9d C |
57 | const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t) |
58 | actorsInvolvedInComment.push(byActor) | |
93ef8a9d | 59 | |
07197db4 | 60 | const parentsCommentActors = threadParentComments.map(c => c.Account.Actor) |
ea44f375 | 61 | |
07197db4 C |
62 | let audience: ActivityAudience |
63 | if (isOrigin) { | |
64 | audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin) | |
65 | } else { | |
66 | audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors)) | |
67 | } | |
93ef8a9d | 68 | |
2186386c | 69 | const data = createActivityData(comment.url, byActor, commentObject, audience) |
ea44f375 | 70 | |
93ef8a9d C |
71 | // This was a reply, send it to the parent actors |
72 | const actorsException = [ byActor ] | |
07197db4 | 73 | await broadcastToActors(data, byActor, parentsCommentActors, actorsException) |
93ef8a9d C |
74 | |
75 | // Broadcast to our followers | |
76 | await broadcastToFollowers(data, byActor, [ byActor ], t) | |
77 | ||
78 | // Send to actors involved in the comment | |
07197db4 C |
79 | if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException) |
80 | ||
81 | // Send to origin | |
82 | return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl) | |
ea44f375 C |
83 | } |
84 | ||
07197db4 | 85 | async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) { |
8e0fd45e C |
86 | logger.info('Creating job to send view of %s.', video.url) |
87 | ||
50d6de9c | 88 | const url = getVideoViewActivityPubUrl(byActor, video) |
ea44f375 | 89 | const viewActivityData = createViewActivityData(byActor, video) |
40ff5707 | 90 | |
50d6de9c | 91 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) |
54141398 | 92 | |
07197db4 C |
93 | // Send to origin |
94 | if (video.isOwned() === false) { | |
e251f170 | 95 | const audience = getVideoAudience(video, actorsInvolvedInVideo) |
2186386c | 96 | const data = createActivityData(url, byActor, viewActivityData, audience) |
54141398 | 97 | |
07197db4 C |
98 | return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) |
99 | } | |
40ff5707 | 100 | |
07197db4 C |
101 | // Send to followers |
102 | const audience = getObjectFollowersAudience(actorsInvolvedInVideo) | |
2186386c | 103 | const data = createActivityData(url, byActor, viewActivityData, audience) |
40ff5707 | 104 | |
50d6de9c C |
105 | // Use the server actor to send the view |
106 | const serverActor = await getServerActor() | |
93ef8a9d | 107 | const actorsException = [ byActor ] |
07197db4 | 108 | return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException) |
0032ebe9 C |
109 | } |
110 | ||
07197db4 | 111 | async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) { |
8e0fd45e C |
112 | logger.info('Creating job to dislike %s.', video.url) |
113 | ||
50d6de9c | 114 | const url = getVideoDislikeActivityPubUrl(byActor, video) |
ea44f375 | 115 | const dislikeActivityData = createDislikeActivityData(byActor, video) |
0032ebe9 | 116 | |
50d6de9c | 117 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) |
0032ebe9 | 118 | |
07197db4 C |
119 | // Send to origin |
120 | if (video.isOwned() === false) { | |
e251f170 | 121 | const audience = getVideoAudience(video, actorsInvolvedInVideo) |
2186386c | 122 | const data = createActivityData(url, byActor, dislikeActivityData, audience) |
40ff5707 | 123 | |
07197db4 C |
124 | return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) |
125 | } | |
0032ebe9 | 126 | |
07197db4 C |
127 | // Send to followers |
128 | const audience = getObjectFollowersAudience(actorsInvolvedInVideo) | |
2186386c | 129 | const data = createActivityData(url, byActor, dislikeActivityData, audience) |
0032ebe9 | 130 | |
93ef8a9d | 131 | const actorsException = [ byActor ] |
07197db4 | 132 | return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException) |
40ff5707 C |
133 | } |
134 | ||
2186386c C |
135 | function createActivityData (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate { |
136 | if (!audience) audience = getAudience(byActor) | |
137 | ||
138 | return audiencify( | |
139 | { | |
140 | type: 'Create' as 'Create', | |
141 | id: url + '/activity', | |
142 | actor: byActor.url, | |
143 | object: audiencify(object, audience) | |
144 | }, | |
145 | audience | |
146 | ) | |
54141398 C |
147 | } |
148 | ||
50d6de9c | 149 | function createDislikeActivityData (byActor: ActorModel, video: VideoModel) { |
3fd3ab2d | 150 | return { |
0032ebe9 | 151 | type: 'Dislike', |
50d6de9c | 152 | actor: byActor.url, |
0032ebe9 C |
153 | object: video.url |
154 | } | |
0032ebe9 C |
155 | } |
156 | ||
ea44f375 C |
157 | function createViewActivityData (byActor: ActorModel, video: VideoModel) { |
158 | return { | |
159 | type: 'View', | |
160 | actor: byActor.url, | |
161 | object: video.url | |
162 | } | |
163 | } | |
164 | ||
54141398 C |
165 | // --------------------------------------------------------------------------- |
166 | ||
167 | export { | |
50d6de9c | 168 | sendCreateVideo, |
54141398 | 169 | sendVideoAbuse, |
40ff5707 | 170 | createActivityData, |
07197db4 C |
171 | sendCreateView, |
172 | sendCreateDislike, | |
ea44f375 | 173 | createDislikeActivityData, |
07197db4 | 174 | sendCreateVideoComment |
54141398 | 175 | } |