]>
Commit | Line | Data |
---|---|---|
e4f97bab C |
1 | // Intercept ActivityPub client requests |
2 | import * as express from 'express' | |
8fffe21a | 3 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' |
d6e99e53 | 4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' |
8fffe21a | 5 | import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' |
c48e82b5 | 6 | import { buildAnnounceWithVideoAudience } from '../../lib/activitypub/send' |
e251f170 | 7 | import { audiencify, getAudience } from '../../lib/activitypub/audience' |
c48e82b5 | 8 | import { buildCreateActivity } from '../../lib/activitypub/send/send-create' |
96f29c0f C |
9 | import { |
10 | asyncMiddleware, | |
11 | executeIfActivityPub, | |
12 | localAccountValidator, | |
13 | localVideoChannelValidator, | |
14 | videosCustomGetValidator | |
15 | } from '../../middlewares' | |
6e46de09 | 16 | import { videoCommentGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators' |
3fd3ab2d | 17 | import { AccountModel } from '../../models/account/account' |
7006bc63 | 18 | import { ActorModel } from '../../models/activitypub/actor' |
50d6de9c | 19 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
3fd3ab2d C |
20 | import { VideoModel } from '../../models/video/video' |
21 | import { VideoChannelModel } from '../../models/video/video-channel' | |
da854ddd | 22 | import { VideoCommentModel } from '../../models/video/video-comment' |
3fd3ab2d | 23 | import { VideoShareModel } from '../../models/video/video-share' |
98d3324d | 24 | import { cacheRoute } from '../../middlewares/cache' |
8fffe21a C |
25 | import { activityPubResponse } from './utils' |
26 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | |
27 | import { | |
28 | getVideoCommentsActivityPubUrl, | |
29 | getVideoDislikesActivityPubUrl, | |
30 | getVideoLikesActivityPubUrl, | |
31 | getVideoSharesActivityPubUrl | |
32 | } from '../../lib/activitypub' | |
40e87e9e | 33 | import { VideoCaptionModel } from '../../models/video/video-caption' |
c48e82b5 C |
34 | import { videoRedundancyGetValidator } from '../../middlewares/validators/redundancy' |
35 | import { getServerActor } from '../../helpers/utils' | |
e4f97bab C |
36 | |
37 | const activityPubClientRouter = express.Router() | |
38 | ||
108af661 | 39 | activityPubClientRouter.get('/accounts?/:name', |
a2431b7d | 40 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
4e50b6a1 | 41 | executeIfActivityPub(accountController) |
e4f97bab | 42 | ) |
7006bc63 | 43 | activityPubClientRouter.get('/accounts?/:name/followers', |
a2431b7d | 44 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
45 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) |
46 | ) | |
7006bc63 | 47 | activityPubClientRouter.get('/accounts?/:name/following', |
a2431b7d | 48 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
49 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
50 | ) | |
51 | ||
20494f12 | 52 | activityPubClientRouter.get('/videos/watch/:id', |
98d3324d | 53 | executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), |
a2431b7d | 54 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), |
da854ddd | 55 | executeIfActivityPub(asyncMiddleware(videoController)) |
4e50b6a1 | 56 | ) |
296c0905 C |
57 | activityPubClientRouter.get('/videos/watch/:id/activity', |
58 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | |
59 | executeIfActivityPub(asyncMiddleware(videoController)) | |
60 | ) | |
46531a0a | 61 | activityPubClientRouter.get('/videos/watch/:id/announces', |
96f29c0f | 62 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), |
46531a0a C |
63 | executeIfActivityPub(asyncMiddleware(videoAnnouncesController)) |
64 | ) | |
4e50b6a1 C |
65 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', |
66 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | |
67 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | |
20494f12 | 68 | ) |
46531a0a | 69 | activityPubClientRouter.get('/videos/watch/:id/likes', |
96f29c0f | 70 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), |
46531a0a C |
71 | executeIfActivityPub(asyncMiddleware(videoLikesController)) |
72 | ) | |
73 | activityPubClientRouter.get('/videos/watch/:id/dislikes', | |
96f29c0f | 74 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), |
46531a0a C |
75 | executeIfActivityPub(asyncMiddleware(videoDislikesController)) |
76 | ) | |
77 | activityPubClientRouter.get('/videos/watch/:id/comments', | |
96f29c0f | 78 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), |
46531a0a C |
79 | executeIfActivityPub(asyncMiddleware(videoCommentsController)) |
80 | ) | |
da854ddd C |
81 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', |
82 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | |
83 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | |
84 | ) | |
296c0905 C |
85 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity', |
86 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | |
87 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | |
88 | ) | |
da854ddd | 89 | |
8a19bee1 C |
90 | activityPubClientRouter.get('/video-channels/:name', |
91 | executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), | |
20494f12 C |
92 | executeIfActivityPub(asyncMiddleware(videoChannelController)) |
93 | ) | |
8a19bee1 C |
94 | activityPubClientRouter.get('/video-channels/:name/followers', |
95 | executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), | |
7006bc63 C |
96 | executeIfActivityPub(asyncMiddleware(videoChannelFollowersController)) |
97 | ) | |
8a19bee1 C |
98 | activityPubClientRouter.get('/video-channels/:name/following', |
99 | executeIfActivityPub(asyncMiddleware(localVideoChannelValidator)), | |
7006bc63 C |
100 | executeIfActivityPub(asyncMiddleware(videoChannelFollowingController)) |
101 | ) | |
20494f12 | 102 | |
c48e82b5 C |
103 | activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?', |
104 | executeIfActivityPub(asyncMiddleware(videoRedundancyGetValidator)), | |
105 | executeIfActivityPub(asyncMiddleware(videoRedundancyController)) | |
106 | ) | |
107 | ||
e4f97bab C |
108 | // --------------------------------------------------------------------------- |
109 | ||
110 | export { | |
111 | activityPubClientRouter | |
112 | } | |
113 | ||
114 | // --------------------------------------------------------------------------- | |
115 | ||
4e50b6a1 | 116 | function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 117 | const account: AccountModel = res.locals.account |
e4f97bab | 118 | |
4b8f09fa | 119 | return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res) |
e4f97bab C |
120 | } |
121 | ||
122 | async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 123 | const account: AccountModel = res.locals.account |
7006bc63 | 124 | const activityPubResult = await actorFollowers(req, account.Actor) |
e4f97bab | 125 | |
4b8f09fa | 126 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
e4f97bab C |
127 | } |
128 | ||
129 | async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 130 | const account: AccountModel = res.locals.account |
7006bc63 | 131 | const activityPubResult = await actorFollowing(req, account.Actor) |
e4f97bab | 132 | |
4b8f09fa | 133 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
e4f97bab | 134 | } |
20494f12 | 135 | |
da854ddd | 136 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 137 | const video: VideoModel = res.locals.video |
20494f12 | 138 | |
40e87e9e C |
139 | // We need captions to render AP object |
140 | video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id) | |
141 | ||
2186386c | 142 | const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC) |
8fffe21a | 143 | const videoObject = audiencify(video.toActivityPubObject(), audience) |
2ccaeeb3 | 144 | |
296c0905 | 145 | if (req.path.endsWith('/activity')) { |
c48e82b5 | 146 | const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience) |
4b8f09fa | 147 | return activityPubResponse(activityPubContextify(data), res) |
296c0905 C |
148 | } |
149 | ||
4b8f09fa | 150 | return activityPubResponse(activityPubContextify(videoObject), res) |
20494f12 C |
151 | } |
152 | ||
4e50b6a1 | 153 | async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 154 | const share = res.locals.videoShare as VideoShareModel |
c48e82b5 | 155 | const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined) |
4e50b6a1 | 156 | |
c48e82b5 | 157 | return activityPubResponse(activityPubContextify(activity), res) |
4e50b6a1 C |
158 | } |
159 | ||
46531a0a C |
160 | async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) { |
161 | const video: VideoModel = res.locals.video | |
162 | ||
8fffe21a C |
163 | const handler = async (start: number, count: number) => { |
164 | const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) | |
165 | return { | |
166 | total: result.count, | |
167 | data: result.rows.map(r => r.url) | |
168 | } | |
169 | } | |
170 | const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page) | |
46531a0a | 171 | |
8fffe21a | 172 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
173 | } |
174 | ||
175 | async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
176 | const video: VideoModel = res.locals.video | |
8fffe21a | 177 | const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) |
46531a0a | 178 | |
8fffe21a | 179 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
180 | } |
181 | ||
182 | async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
183 | const video: VideoModel = res.locals.video | |
8fffe21a | 184 | const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) |
46531a0a | 185 | |
8fffe21a | 186 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
187 | } |
188 | ||
189 | async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
190 | const video: VideoModel = res.locals.video | |
191 | ||
8fffe21a C |
192 | const handler = async (start: number, count: number) => { |
193 | const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count) | |
194 | return { | |
195 | total: result.count, | |
196 | data: result.rows.map(r => r.url) | |
197 | } | |
198 | } | |
199 | const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page) | |
46531a0a | 200 | |
8fffe21a | 201 | return activityPubResponse(activityPubContextify(json), res) |
46531a0a C |
202 | } |
203 | ||
20494f12 | 204 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 205 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
20494f12 | 206 | |
4b8f09fa | 207 | return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res) |
20494f12 | 208 | } |
da854ddd | 209 | |
7006bc63 C |
210 | async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { |
211 | const videoChannel: VideoChannelModel = res.locals.videoChannel | |
212 | const activityPubResult = await actorFollowers(req, videoChannel.Actor) | |
213 | ||
4b8f09fa | 214 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
7006bc63 C |
215 | } |
216 | ||
217 | async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
218 | const videoChannel: VideoChannelModel = res.locals.videoChannel | |
219 | const activityPubResult = await actorFollowing(req, videoChannel.Actor) | |
220 | ||
4b8f09fa | 221 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
7006bc63 C |
222 | } |
223 | ||
da854ddd C |
224 | async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) { |
225 | const videoComment: VideoCommentModel = res.locals.videoComment | |
226 | ||
d7e70384 | 227 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) |
d6e99e53 | 228 | const isPublic = true // Comments are always public |
2186386c | 229 | const audience = getAudience(videoComment.Account.Actor, isPublic) |
d6e99e53 C |
230 | |
231 | const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience) | |
232 | ||
296c0905 | 233 | if (req.path.endsWith('/activity')) { |
c48e82b5 | 234 | const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience) |
4b8f09fa | 235 | return activityPubResponse(activityPubContextify(data), res) |
296c0905 C |
236 | } |
237 | ||
4b8f09fa | 238 | return activityPubResponse(activityPubContextify(videoCommentObject), res) |
da854ddd | 239 | } |
7006bc63 | 240 | |
c48e82b5 C |
241 | async function videoRedundancyController (req: express.Request, res: express.Response) { |
242 | const videoRedundancy = res.locals.videoRedundancy | |
243 | const serverActor = await getServerActor() | |
244 | ||
245 | const audience = getAudience(serverActor) | |
246 | const object = audiencify(videoRedundancy.toActivityPubObject(), audience) | |
247 | ||
248 | if (req.path.endsWith('/activity')) { | |
249 | const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience) | |
250 | return activityPubResponse(activityPubContextify(data), res) | |
251 | } | |
252 | ||
253 | return activityPubResponse(activityPubContextify(object), res) | |
254 | } | |
255 | ||
7006bc63 C |
256 | // --------------------------------------------------------------------------- |
257 | ||
258 | async function actorFollowing (req: express.Request, actor: ActorModel) { | |
8fffe21a C |
259 | const handler = (start: number, count: number) => { |
260 | return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) | |
261 | } | |
7006bc63 | 262 | |
8fffe21a | 263 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) |
7006bc63 C |
264 | } |
265 | ||
266 | async function actorFollowers (req: express.Request, actor: ActorModel) { | |
8fffe21a C |
267 | const handler = (start: number, count: number) => { |
268 | return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count) | |
269 | } | |
7006bc63 | 270 | |
8fffe21a | 271 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) |
7006bc63 | 272 | } |
4b8f09fa | 273 | |
8fffe21a C |
274 | function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) { |
275 | const handler = async (start: number, count: number) => { | |
276 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) | |
277 | return { | |
278 | total: result.count, | |
279 | data: result.rows.map(r => r.Account.Actor.url) | |
280 | } | |
281 | } | |
282 | return activityPubCollectionPagination(url, handler, req.query.page) | |
4b8f09fa | 283 | } |