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