]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
Send server announce when users upload a video
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
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'
7 import { AccountInstance } from '../../models'
8 import { activityPubCollectionPagination } from '../../helpers/activitypub'
9 import { ACTIVITY_PUB } from '../../initializers/constants'
10 import { asyncMiddleware } from '../../middlewares/async'
11
12 const activityPubClientRouter = express.Router()
13
14 activityPubClientRouter.get('/account/:name',
15 executeIfActivityPub(localAccountValidator),
16 executeIfActivityPub(asyncMiddleware(accountController))
17 )
18
19 activityPubClientRouter.get('/account/:name/followers',
20 executeIfActivityPub(localAccountValidator),
21 executeIfActivityPub(asyncMiddleware(accountFollowersController))
22 )
23
24 activityPubClientRouter.get('/account/:name/following',
25 executeIfActivityPub(localAccountValidator),
26 executeIfActivityPub(asyncMiddleware(accountFollowingController))
27 )
28
29 // ---------------------------------------------------------------------------
30
31 export {
32 activityPubClientRouter
33 }
34
35 // ---------------------------------------------------------------------------
36
37 async function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
38 const account: AccountInstance = res.locals.account
39
40 return res.json(account.toActivityPubObject()).end()
41 }
42
43 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
44 const account: AccountInstance = res.locals.account
45
46 const page = req.params.page || 1
47 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
48
49 const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi([ account.id ], start, count)
50 const activityPubResult = activityPubCollectionPagination(req.url, page, result)
51
52 return res.json(activityPubResult)
53 }
54
55 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
56 const account: AccountInstance = res.locals.account
57
58 const page = req.params.page || 1
59 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
60
61 const result = await db.AccountFollow.listAcceptedFollowingUrlsForApi([ account.id ], start, count)
62 const activityPubResult = activityPubCollectionPagination(req.url, page, result)
63
64 return res.json(activityPubResult)
65 }