]>
Commit | Line | Data |
---|---|---|
e4f97bab C |
1 | // Intercept ActivityPub client requests |
2 | import * as express from 'express' | |
3 | ||
4 | import { database as db } from '../../initializers' | |
5 | import { executeIfActivityPub, localAccountValidator } from '../../middlewares' | |
6 | import { pageToStartAndCount } from '../../helpers' | |
20494f12 | 7 | import { AccountInstance, VideoChannelInstance } from '../../models' |
e4f97bab | 8 | import { activityPubCollectionPagination } from '../../helpers/activitypub' |
e71bcc0f | 9 | import { ACTIVITY_PUB, CONFIG } from '../../initializers/constants' |
e4f97bab | 10 | import { asyncMiddleware } from '../../middlewares/async' |
20494f12 C |
11 | import { videosGetValidator } from '../../middlewares/validators/videos' |
12 | import { VideoInstance } from '../../models/video/video-interface' | |
13 | import { videoChannelsGetValidator } from '../../middlewares/validators/video-channels' | |
e4f97bab C |
14 | |
15 | const activityPubClientRouter = express.Router() | |
16 | ||
17 | activityPubClientRouter.get('/account/:name', | |
18 | executeIfActivityPub(localAccountValidator), | |
19 | executeIfActivityPub(asyncMiddleware(accountController)) | |
20 | ) | |
21 | ||
350e31d6 | 22 | activityPubClientRouter.get('/account/:name/followers', |
e4f97bab C |
23 | executeIfActivityPub(localAccountValidator), |
24 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) | |
25 | ) | |
26 | ||
350e31d6 | 27 | activityPubClientRouter.get('/account/:name/following', |
e4f97bab C |
28 | executeIfActivityPub(localAccountValidator), |
29 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) | |
30 | ) | |
31 | ||
20494f12 C |
32 | activityPubClientRouter.get('/videos/watch/:id', |
33 | executeIfActivityPub(videosGetValidator), | |
34 | executeIfActivityPub(asyncMiddleware(videoController)) | |
35 | ) | |
36 | ||
37 | activityPubClientRouter.get('/video-channels/:id', | |
38 | executeIfActivityPub(videoChannelsGetValidator), | |
39 | executeIfActivityPub(asyncMiddleware(videoChannelController)) | |
40 | ) | |
41 | ||
e4f97bab C |
42 | // --------------------------------------------------------------------------- |
43 | ||
44 | export { | |
45 | activityPubClientRouter | |
46 | } | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | async function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
51 | const account: AccountInstance = res.locals.account | |
52 | ||
53 | return res.json(account.toActivityPubObject()).end() | |
54 | } | |
55 | ||
56 | async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
57 | const account: AccountInstance = res.locals.account | |
58 | ||
59 | const page = req.params.page || 1 | |
60 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) | |
61 | ||
efc32059 | 62 | const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi([ account.id ], start, count) |
e71bcc0f | 63 | const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) |
e4f97bab C |
64 | |
65 | return res.json(activityPubResult) | |
66 | } | |
67 | ||
68 | async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
69 | const account: AccountInstance = res.locals.account | |
70 | ||
71 | const page = req.params.page || 1 | |
72 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) | |
73 | ||
efc32059 | 74 | const result = await db.AccountFollow.listAcceptedFollowingUrlsForApi([ account.id ], start, count) |
e71bcc0f | 75 | const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) |
e4f97bab C |
76 | |
77 | return res.json(activityPubResult) | |
78 | } | |
20494f12 C |
79 | |
80 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
81 | const video: VideoInstance = res.locals.video | |
82 | ||
83 | return res.json(video.toActivityPubObject()) | |
84 | } | |
85 | ||
86 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
87 | const videoChannel: VideoChannelInstance = res.locals.videoChannel | |
88 | ||
89 | return res.json(videoChannel.toActivityPubObject()) | |
90 | } |