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