]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
Remove unused webserver configuration
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
1 // Intercept ActivityPub client requests
2 import * as express from 'express'
3 import { VideoPrivacy } from '../../../shared/models/videos'
4 import { activityPubCollectionPagination } from '../../helpers/activitypub'
5 import { pageToStartAndCount } from '../../helpers/core-utils'
6 import { ACTIVITY_PUB, CONFIG } from '../../initializers'
7 import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send'
8 import { audiencify, getAudience } from '../../lib/activitypub/send/misc'
9 import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares'
10 import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators'
11 import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
12 import { AccountModel } from '../../models/account/account'
13 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
14 import { VideoModel } from '../../models/video/video'
15 import { VideoChannelModel } from '../../models/video/video-channel'
16 import { VideoCommentModel } from '../../models/video/video-comment'
17 import { VideoShareModel } from '../../models/video/video-share'
18
19 const activityPubClientRouter = express.Router()
20
21 activityPubClientRouter.get('/accounts?/:name',
22 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
23 executeIfActivityPub(accountController)
24 )
25
26 activityPubClientRouter.get('/accounts/:name/followers',
27 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
28 executeIfActivityPub(asyncMiddleware(accountFollowersController))
29 )
30
31 activityPubClientRouter.get('/accounts/:name/following',
32 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
33 executeIfActivityPub(asyncMiddleware(accountFollowingController))
34 )
35
36 activityPubClientRouter.get('/videos/watch/:id',
37 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
38 executeIfActivityPub(asyncMiddleware(videoController))
39 )
40
41 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
42 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
43 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
44 )
45
46 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
47 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
48 executeIfActivityPub(asyncMiddleware(videoCommentController))
49 )
50
51 activityPubClientRouter.get('/video-channels/:id',
52 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
53 executeIfActivityPub(asyncMiddleware(videoChannelController))
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: AccountModel = res.locals.account
66
67 return res.json(account.toActivityPubObject())
68 .end()
69 }
70
71 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
72 const account: AccountModel = res.locals.account
73
74 const page = req.query.page || 1
75 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
76
77 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ account.Actor.id ], undefined, start, count)
78 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
79
80 return res.json(activityPubResult)
81 }
82
83 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
84 const account: AccountModel = res.locals.account
85
86 const page = req.query.page || 1
87 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
88
89 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ account.Actor.id ], undefined, start, count)
90 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
91
92 return res.json(activityPubResult)
93 }
94
95 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
96 const video: VideoModel = res.locals.video
97
98 // We need more attributes
99 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
100 const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC)
101
102 return res.json(audiencify(videoAll.toActivityPubObject(), audience))
103 }
104
105 async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
106 const share = res.locals.videoShare as VideoShareModel
107 const object = await buildVideoAnnounceToFollowers(share.Actor, res.locals.video, undefined)
108
109 return res.json(object)
110 }
111
112 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
113 const videoChannel: VideoChannelModel = res.locals.videoChannel
114
115 return res.json(videoChannel.toActivityPubObject())
116 }
117
118 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
119 const videoComment: VideoCommentModel = res.locals.videoComment
120
121 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
122 return res.json(videoComment.toActivityPubObject(threadParentComments))
123 }