]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
Server shares user videos
[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, VideoChannelInstance } from '../../models'
8 import { activityPubCollectionPagination } from '../../helpers/activitypub'
9 import { ACTIVITY_PUB } from '../../initializers/constants'
10 import { asyncMiddleware } from '../../middlewares/async'
11 import { videosGetValidator } from '../../middlewares/validators/videos'
12 import { VideoInstance } from '../../models/video/video-interface'
13 import { videoChannelsGetValidator } from '../../middlewares/validators/video-channels'
14
15 const activityPubClientRouter = express.Router()
16
17 activityPubClientRouter.get('/account/:name',
18 executeIfActivityPub(localAccountValidator),
19 executeIfActivityPub(asyncMiddleware(accountController))
20 )
21
22 activityPubClientRouter.get('/account/:name/followers',
23 executeIfActivityPub(localAccountValidator),
24 executeIfActivityPub(asyncMiddleware(accountFollowersController))
25 )
26
27 activityPubClientRouter.get('/account/:name/following',
28 executeIfActivityPub(localAccountValidator),
29 executeIfActivityPub(asyncMiddleware(accountFollowingController))
30 )
31
32 activityPubClientRouter.get('/videos/watch/:id',
33 executeIfActivityPub(videosGetValidator),
34 executeIfActivityPub(asyncMiddleware(videoController))
35 )
36
37 activityPubClientRouter.get('/video-channels/:id',
38 executeIfActivityPub(videoChannelsGetValidator),
39 executeIfActivityPub(asyncMiddleware(videoChannelController))
40 )
41
42 // ---------------------------------------------------------------------------
43
44 export {
45 activityPubClientRouter
46 }
47
48 // ---------------------------------------------------------------------------
49
50 async 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
56 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
57 const account: AccountInstance = res.locals.account
58
59 const page = req.params.page || 1
60 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
61
62 const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi([ account.id ], start, count)
63 const activityPubResult = activityPubCollectionPagination(req.url, page, result)
64
65 return res.json(activityPubResult)
66 }
67
68 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
69 const account: AccountInstance = res.locals.account
70
71 const page = req.params.page || 1
72 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
73
74 const result = await db.AccountFollow.listAcceptedFollowingUrlsForApi([ account.id ], start, count)
75 const activityPubResult = activityPubCollectionPagination(req.url, page, result)
76
77 return res.json(activityPubResult)
78 }
79
80 async 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
86 async 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 }