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