]>
Commit | Line | Data |
---|---|---|
e4f97bab C |
1 | // Intercept ActivityPub client requests |
2 | import * as express from 'express' | |
3fd3ab2d C |
3 | import { activityPubCollectionPagination, pageToStartAndCount } from '../../helpers' |
4 | import { ACTIVITY_PUB, CONFIG } from '../../initializers' | |
50d6de9c | 5 | import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send' |
3fd3ab2d | 6 | import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares' |
50d6de9c | 7 | import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators' |
3fd3ab2d | 8 | import { AccountModel } from '../../models/account/account' |
50d6de9c | 9 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
3fd3ab2d C |
10 | import { VideoModel } from '../../models/video/video' |
11 | import { VideoChannelModel } from '../../models/video/video-channel' | |
3fd3ab2d | 12 | import { VideoShareModel } from '../../models/video/video-share' |
e4f97bab C |
13 | |
14 | const activityPubClientRouter = express.Router() | |
15 | ||
16 | activityPubClientRouter.get('/account/:name', | |
a2431b7d | 17 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
4e50b6a1 | 18 | executeIfActivityPub(accountController) |
e4f97bab C |
19 | ) |
20 | ||
350e31d6 | 21 | activityPubClientRouter.get('/account/:name/followers', |
a2431b7d | 22 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
23 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) |
24 | ) | |
25 | ||
350e31d6 | 26 | activityPubClientRouter.get('/account/:name/following', |
a2431b7d | 27 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
28 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
29 | ) | |
30 | ||
20494f12 | 31 | activityPubClientRouter.get('/videos/watch/:id', |
a2431b7d | 32 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), |
4e50b6a1 C |
33 | executeIfActivityPub(videoController) |
34 | ) | |
35 | ||
36 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', | |
37 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | |
38 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | |
20494f12 C |
39 | ) |
40 | ||
41 | activityPubClientRouter.get('/video-channels/:id', | |
a2431b7d | 42 | executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)), |
20494f12 C |
43 | executeIfActivityPub(asyncMiddleware(videoChannelController)) |
44 | ) | |
45 | ||
e4f97bab C |
46 | // --------------------------------------------------------------------------- |
47 | ||
48 | export { | |
49 | activityPubClientRouter | |
50 | } | |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | ||
4e50b6a1 | 54 | function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 55 | const account: AccountModel = res.locals.account |
e4f97bab C |
56 | |
57 | return res.json(account.toActivityPubObject()).end() | |
58 | } | |
59 | ||
60 | async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 61 | const account: AccountModel = res.locals.account |
e4f97bab | 62 | |
c46edbc2 | 63 | const page = req.query.page || 1 |
e4f97bab C |
64 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) |
65 | ||
50d6de9c | 66 | const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ account.Actor.id ], undefined, start, count) |
e71bcc0f | 67 | const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) |
e4f97bab C |
68 | |
69 | return res.json(activityPubResult) | |
70 | } | |
71 | ||
72 | async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 73 | const account: AccountModel = res.locals.account |
e4f97bab | 74 | |
c46edbc2 | 75 | const page = req.query.page || 1 |
e4f97bab C |
76 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) |
77 | ||
50d6de9c | 78 | const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ account.Actor.id ], undefined, start, count) |
e71bcc0f | 79 | const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) |
e4f97bab C |
80 | |
81 | return res.json(activityPubResult) | |
82 | } | |
20494f12 | 83 | |
4e50b6a1 | 84 | function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 85 | const video: VideoModel = res.locals.video |
20494f12 C |
86 | |
87 | return res.json(video.toActivityPubObject()) | |
88 | } | |
89 | ||
4e50b6a1 | 90 | async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 91 | const share = res.locals.videoShare as VideoShareModel |
50d6de9c | 92 | const object = await buildVideoAnnounceToFollowers(share.Actor, res.locals.video, undefined) |
4e50b6a1 C |
93 | |
94 | return res.json(object) | |
95 | } | |
96 | ||
20494f12 | 97 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 98 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
20494f12 C |
99 | |
100 | return res.json(videoChannel.toActivityPubObject()) | |
101 | } |