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