]>
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' | |
5 | import { buildVideoChannelAnnounceToFollowers } from '../../lib/activitypub/send' | |
4e50b6a1 | 6 | import { buildVideoAnnounceToFollowers } from '../../lib/index' |
3fd3ab2d C |
7 | import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares' |
8 | import { | |
9 | videoChannelsGetValidator, | |
10 | videoChannelsShareValidator, | |
11 | videosGetValidator, | |
12 | videosShareValidator | |
13 | } from '../../middlewares/validators' | |
14 | import { AccountModel } from '../../models/account/account' | |
15 | import { AccountFollowModel } from '../../models/account/account-follow' | |
16 | import { VideoModel } from '../../models/video/video' | |
17 | import { VideoChannelModel } from '../../models/video/video-channel' | |
18 | import { VideoChannelShareModel } from '../../models/video/video-channel-share' | |
19 | import { VideoShareModel } from '../../models/video/video-share' | |
e4f97bab C |
20 | |
21 | const activityPubClientRouter = express.Router() | |
22 | ||
23 | activityPubClientRouter.get('/account/:name', | |
a2431b7d | 24 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
4e50b6a1 | 25 | executeIfActivityPub(accountController) |
e4f97bab C |
26 | ) |
27 | ||
350e31d6 | 28 | activityPubClientRouter.get('/account/:name/followers', |
a2431b7d | 29 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
30 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) |
31 | ) | |
32 | ||
350e31d6 | 33 | activityPubClientRouter.get('/account/:name/following', |
a2431b7d | 34 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
35 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
36 | ) | |
37 | ||
20494f12 | 38 | activityPubClientRouter.get('/videos/watch/:id', |
a2431b7d | 39 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), |
4e50b6a1 C |
40 | executeIfActivityPub(videoController) |
41 | ) | |
42 | ||
43 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', | |
44 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | |
45 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | |
20494f12 C |
46 | ) |
47 | ||
48 | activityPubClientRouter.get('/video-channels/:id', | |
a2431b7d | 49 | executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)), |
20494f12 C |
50 | executeIfActivityPub(asyncMiddleware(videoChannelController)) |
51 | ) | |
52 | ||
4e50b6a1 C |
53 | activityPubClientRouter.get('/video-channels/:id/announces/:accountId', |
54 | executeIfActivityPub(asyncMiddleware(videoChannelsShareValidator)), | |
55 | executeIfActivityPub(asyncMiddleware(videoChannelAnnounceController)) | |
56 | ) | |
57 | ||
e4f97bab C |
58 | // --------------------------------------------------------------------------- |
59 | ||
60 | export { | |
61 | activityPubClientRouter | |
62 | } | |
63 | ||
64 | // --------------------------------------------------------------------------- | |
65 | ||
4e50b6a1 | 66 | function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 67 | const account: AccountModel = res.locals.account |
e4f97bab C |
68 | |
69 | return res.json(account.toActivityPubObject()).end() | |
70 | } | |
71 | ||
72 | async function accountFollowersController (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 | ||
3fd3ab2d | 78 | const result = await AccountFollowModel.listAcceptedFollowerUrlsForApi([ account.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 | } | |
83 | ||
84 | async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 85 | const account: AccountModel = res.locals.account |
e4f97bab | 86 | |
c46edbc2 | 87 | const page = req.query.page || 1 |
e4f97bab C |
88 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) |
89 | ||
3fd3ab2d | 90 | const result = await AccountFollowModel.listAcceptedFollowingUrlsForApi([ account.id ], undefined, start, count) |
e71bcc0f | 91 | const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) |
e4f97bab C |
92 | |
93 | return res.json(activityPubResult) | |
94 | } | |
20494f12 | 95 | |
4e50b6a1 | 96 | function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 97 | const video: VideoModel = res.locals.video |
20494f12 C |
98 | |
99 | return res.json(video.toActivityPubObject()) | |
100 | } | |
101 | ||
4e50b6a1 | 102 | async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 103 | const share = res.locals.videoShare as VideoShareModel |
4e50b6a1 C |
104 | const object = await buildVideoAnnounceToFollowers(share.Account, res.locals.video, undefined) |
105 | ||
106 | return res.json(object) | |
107 | } | |
108 | ||
109 | async function videoChannelAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 110 | const share = res.locals.videoChannelShare as VideoChannelShareModel |
4e50b6a1 C |
111 | const object = await buildVideoChannelAnnounceToFollowers(share.Account, share.VideoChannel, undefined) |
112 | ||
113 | return res.json(object) | |
114 | } | |
115 | ||
20494f12 | 116 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 117 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
20494f12 C |
118 | |
119 | return res.json(videoChannel.toActivityPubObject()) | |
120 | } |