]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
7e87f6f3baad1f95707e746292c474dd3bd0dc72
[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 { videoRedundancyGetValidator } 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(videosGetValidator)),
67 executeIfActivityPub(asyncMiddleware(videoController))
68 )
69 activityPubClientRouter.get('/videos/watch/:id/activity',
70 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
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(videoRedundancyGetValidator)),
117 executeIfActivityPub(asyncMiddleware(videoRedundancyController))
118 )
119
120 // ---------------------------------------------------------------------------
121
122 export {
123 activityPubClientRouter
124 }
125
126 // ---------------------------------------------------------------------------
127
128 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
129 const account: AccountModel = res.locals.account
130
131 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
132 }
133
134 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
135 const account: AccountModel = res.locals.account
136 const activityPubResult = await actorFollowers(req, account.Actor)
137
138 return activityPubResponse(activityPubContextify(activityPubResult), res)
139 }
140
141 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
142 const account: AccountModel = res.locals.account
143 const activityPubResult = await actorFollowing(req, account.Actor)
144
145 return activityPubResponse(activityPubContextify(activityPubResult), res)
146 }
147
148 function getAccountVideoRate (rateType: VideoRateType) {
149 return (req: express.Request, res: express.Response) => {
150 const accountVideoRate: AccountVideoRateModel = res.locals.accountVideoRate
151
152 const byActor = accountVideoRate.Account.Actor
153 const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
154 const APObject = rateType === 'like'
155 ? buildLikeActivity(url, byActor, accountVideoRate.Video)
156 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
157
158 return activityPubResponse(activityPubContextify(APObject), res)
159 }
160 }
161
162 async function videoController (req: express.Request, res: express.Response) {
163 const video: VideoModel = res.locals.video
164
165 if (video.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(video.url)
166
167 // We need captions to render AP object
168 video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
169
170 const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC)
171 const videoObject = audiencify(video.toActivityPubObject(), audience)
172
173 if (req.path.endsWith('/activity')) {
174 const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
175 return activityPubResponse(activityPubContextify(data), res)
176 }
177
178 return activityPubResponse(activityPubContextify(videoObject), res)
179 }
180
181 async function videoAnnounceController (req: express.Request, res: express.Response) {
182 const share = res.locals.videoShare as VideoShareModel
183
184 if (share.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(share.url)
185
186 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined)
187
188 return activityPubResponse(activityPubContextify(activity), res)
189 }
190
191 async function videoAnnouncesController (req: express.Request, res: express.Response) {
192 const video: VideoModel = res.locals.video
193
194 const handler = async (start: number, count: number) => {
195 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
196 return {
197 total: result.count,
198 data: result.rows.map(r => r.url)
199 }
200 }
201 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
202
203 return activityPubResponse(activityPubContextify(json), res)
204 }
205
206 async function videoLikesController (req: express.Request, res: express.Response) {
207 const video: VideoModel = res.locals.video
208 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
209
210 return activityPubResponse(activityPubContextify(json), res)
211 }
212
213 async function videoDislikesController (req: express.Request, res: express.Response) {
214 const video: VideoModel = res.locals.video
215 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
216
217 return activityPubResponse(activityPubContextify(json), res)
218 }
219
220 async function videoCommentsController (req: express.Request, res: express.Response) {
221 const video: VideoModel = res.locals.video
222
223 const handler = async (start: number, count: number) => {
224 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
225 return {
226 total: result.count,
227 data: result.rows.map(r => r.url)
228 }
229 }
230 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
231
232 return activityPubResponse(activityPubContextify(json), res)
233 }
234
235 async function videoChannelController (req: express.Request, res: express.Response) {
236 const videoChannel: VideoChannelModel = res.locals.videoChannel
237
238 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
239 }
240
241 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
242 const videoChannel: VideoChannelModel = res.locals.videoChannel
243 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
244
245 return activityPubResponse(activityPubContextify(activityPubResult), res)
246 }
247
248 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
249 const videoChannel: VideoChannelModel = res.locals.videoChannel
250 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
251
252 return activityPubResponse(activityPubContextify(activityPubResult), res)
253 }
254
255 async function videoCommentController (req: express.Request, res: express.Response) {
256 const videoComment: VideoCommentModel = res.locals.videoComment
257
258 if (videoComment.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoComment.url)
259
260 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
261 const isPublic = true // Comments are always public
262 const audience = getAudience(videoComment.Account.Actor, isPublic)
263
264 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
265
266 if (req.path.endsWith('/activity')) {
267 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
268 return activityPubResponse(activityPubContextify(data), res)
269 }
270
271 return activityPubResponse(activityPubContextify(videoCommentObject), res)
272 }
273
274 async function videoRedundancyController (req: express.Request, res: express.Response) {
275 const videoRedundancy: VideoRedundancyModel = res.locals.videoRedundancy
276 if (videoRedundancy.url.startsWith(CONFIG.WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
277
278 const serverActor = await getServerActor()
279
280 const audience = getAudience(serverActor)
281 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
282
283 if (req.path.endsWith('/activity')) {
284 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
285 return activityPubResponse(activityPubContextify(data), res)
286 }
287
288 return activityPubResponse(activityPubContextify(object), res)
289 }
290
291 // ---------------------------------------------------------------------------
292
293 async function actorFollowing (req: express.Request, actor: ActorModel) {
294 const handler = (start: number, count: number) => {
295 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
296 }
297
298 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page)
299 }
300
301 async function actorFollowers (req: express.Request, actor: ActorModel) {
302 const handler = (start: number, count: number) => {
303 return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
304 }
305
306 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.path, handler, req.query.page)
307 }
308
309 function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) {
310 const handler = async (start: number, count: number) => {
311 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
312 return {
313 total: result.count,
314 data: result.rows.map(r => r.url)
315 }
316 }
317 return activityPubCollectionPagination(url, handler, req.query.page)
318 }