]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Fetch outbox to grab old activities tests
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
CommitLineData
e4f97bab
C
1// Intercept ActivityPub client requests
2import * as express from 'express'
3
4import { database as db } from '../../initializers'
5import { executeIfActivityPub, localAccountValidator } from '../../middlewares'
6import { pageToStartAndCount } from '../../helpers'
20494f12 7import { AccountInstance, VideoChannelInstance } from '../../models'
e4f97bab 8import { activityPubCollectionPagination } from '../../helpers/activitypub'
e71bcc0f 9import { ACTIVITY_PUB, CONFIG } from '../../initializers/constants'
e4f97bab 10import { asyncMiddleware } from '../../middlewares/async'
20494f12
C
11import { videosGetValidator } from '../../middlewares/validators/videos'
12import { VideoInstance } from '../../models/video/video-interface'
13import { videoChannelsGetValidator } from '../../middlewares/validators/video-channels'
e4f97bab
C
14
15const activityPubClientRouter = express.Router()
16
17activityPubClientRouter.get('/account/:name',
18 executeIfActivityPub(localAccountValidator),
19 executeIfActivityPub(asyncMiddleware(accountController))
20)
21
350e31d6 22activityPubClientRouter.get('/account/:name/followers',
e4f97bab
C
23 executeIfActivityPub(localAccountValidator),
24 executeIfActivityPub(asyncMiddleware(accountFollowersController))
25)
26
350e31d6 27activityPubClientRouter.get('/account/:name/following',
e4f97bab
C
28 executeIfActivityPub(localAccountValidator),
29 executeIfActivityPub(asyncMiddleware(accountFollowingController))
30)
31
20494f12
C
32activityPubClientRouter.get('/videos/watch/:id',
33 executeIfActivityPub(videosGetValidator),
34 executeIfActivityPub(asyncMiddleware(videoController))
35)
36
37activityPubClientRouter.get('/video-channels/:id',
38 executeIfActivityPub(videoChannelsGetValidator),
39 executeIfActivityPub(asyncMiddleware(videoChannelController))
40)
41
e4f97bab
C
42// ---------------------------------------------------------------------------
43
44export {
45 activityPubClientRouter
46}
47
48// ---------------------------------------------------------------------------
49
50async 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
56async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
57 const account: AccountInstance = res.locals.account
58
c46edbc2 59 const page = req.query.page || 1
e4f97bab
C
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
68async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
69 const account: AccountInstance = res.locals.account
70
c46edbc2 71 const page = req.query.page || 1
e4f97bab
C
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
80async 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
86async 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}