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