]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
Add shares forward and collection on videos/video channels
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
1 // Intercept ActivityPub client requests
2 import * as express from 'express'
3 import { pageToStartAndCount } from '../../helpers'
4 import { activityPubCollectionPagination } from '../../helpers/activitypub'
5
6 import { database as db } from '../../initializers'
7 import { ACTIVITY_PUB, CONFIG } from '../../initializers/constants'
8 import { buildVideoChannelAnnounceToFollowers } from '../../lib/activitypub/send/send-announce'
9 import { buildVideoAnnounceToFollowers } from '../../lib/index'
10 import { executeIfActivityPub, localAccountValidator } from '../../middlewares'
11 import { asyncMiddleware } from '../../middlewares/async'
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'
16 import { VideoInstance } from '../../models/video/video-interface'
17 import { VideoShareInstance } from '../../models/video/video-share-interface'
18
19 const activityPubClientRouter = express.Router()
20
21 activityPubClientRouter.get('/account/:name',
22 executeIfActivityPub(localAccountValidator),
23 executeIfActivityPub(accountController)
24 )
25
26 activityPubClientRouter.get('/account/:name/followers',
27 executeIfActivityPub(localAccountValidator),
28 executeIfActivityPub(asyncMiddleware(accountFollowersController))
29 )
30
31 activityPubClientRouter.get('/account/:name/following',
32 executeIfActivityPub(localAccountValidator),
33 executeIfActivityPub(asyncMiddleware(accountFollowingController))
34 )
35
36 activityPubClientRouter.get('/videos/watch/:id',
37 executeIfActivityPub(videosGetValidator),
38 executeIfActivityPub(videoController)
39 )
40
41 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
42 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
43 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
44 )
45
46 activityPubClientRouter.get('/video-channels/:id',
47 executeIfActivityPub(videoChannelsGetValidator),
48 executeIfActivityPub(asyncMiddleware(videoChannelController))
49 )
50
51 activityPubClientRouter.get('/video-channels/:id/announces/:accountId',
52 executeIfActivityPub(asyncMiddleware(videoChannelsShareValidator)),
53 executeIfActivityPub(asyncMiddleware(videoChannelAnnounceController))
54 )
55
56 // ---------------------------------------------------------------------------
57
58 export {
59 activityPubClientRouter
60 }
61
62 // ---------------------------------------------------------------------------
63
64 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
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
73 const page = req.query.page || 1
74 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
75
76 const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi([ account.id ], start, count)
77 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
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
85 const page = req.query.page || 1
86 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
87
88 const result = await db.AccountFollow.listAcceptedFollowingUrlsForApi([ account.id ], start, count)
89 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
90
91 return res.json(activityPubResult)
92 }
93
94 function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
95 const video: VideoInstance = res.locals.video
96
97 return res.json(video.toActivityPubObject())
98 }
99
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
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 }