]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Add shares forward and collection on videos/video channels
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
CommitLineData
e4f97bab
C
1// Intercept ActivityPub client requests
2import * as express from 'express'
e4f97bab 3import { pageToStartAndCount } from '../../helpers'
e4f97bab 4import { activityPubCollectionPagination } from '../../helpers/activitypub'
4e50b6a1
C
5
6import { database as db } from '../../initializers'
e71bcc0f 7import { ACTIVITY_PUB, CONFIG } from '../../initializers/constants'
4e50b6a1
C
8import { buildVideoChannelAnnounceToFollowers } from '../../lib/activitypub/send/send-announce'
9import { buildVideoAnnounceToFollowers } from '../../lib/index'
10import { executeIfActivityPub, localAccountValidator } from '../../middlewares'
e4f97bab 11import { asyncMiddleware } from '../../middlewares/async'
4e50b6a1
C
12import { videoChannelsGetValidator, videoChannelsShareValidator } from '../../middlewares/validators/video-channels'
13import { videosGetValidator, videosShareValidator } from '../../middlewares/validators/videos'
14import { AccountInstance, VideoChannelInstance } from '../../models'
15import { VideoChannelShareInstance } from '../../models/video/video-channel-share-interface'
20494f12 16import { VideoInstance } from '../../models/video/video-interface'
4e50b6a1 17import { VideoShareInstance } from '../../models/video/video-share-interface'
e4f97bab
C
18
19const activityPubClientRouter = express.Router()
20
21activityPubClientRouter.get('/account/:name',
22 executeIfActivityPub(localAccountValidator),
4e50b6a1 23 executeIfActivityPub(accountController)
e4f97bab
C
24)
25
350e31d6 26activityPubClientRouter.get('/account/:name/followers',
e4f97bab
C
27 executeIfActivityPub(localAccountValidator),
28 executeIfActivityPub(asyncMiddleware(accountFollowersController))
29)
30
350e31d6 31activityPubClientRouter.get('/account/:name/following',
e4f97bab
C
32 executeIfActivityPub(localAccountValidator),
33 executeIfActivityPub(asyncMiddleware(accountFollowingController))
34)
35
20494f12
C
36activityPubClientRouter.get('/videos/watch/:id',
37 executeIfActivityPub(videosGetValidator),
4e50b6a1
C
38 executeIfActivityPub(videoController)
39)
40
41activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
42 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
43 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
20494f12
C
44)
45
46activityPubClientRouter.get('/video-channels/:id',
47 executeIfActivityPub(videoChannelsGetValidator),
48 executeIfActivityPub(asyncMiddleware(videoChannelController))
49)
50
4e50b6a1
C
51activityPubClientRouter.get('/video-channels/:id/announces/:accountId',
52 executeIfActivityPub(asyncMiddleware(videoChannelsShareValidator)),
53 executeIfActivityPub(asyncMiddleware(videoChannelAnnounceController))
54)
55
e4f97bab
C
56// ---------------------------------------------------------------------------
57
58export {
59 activityPubClientRouter
60}
61
62// ---------------------------------------------------------------------------
63
4e50b6a1 64function 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
70async 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
82async 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 94function 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
100async 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
107async 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
114async 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}