]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
ba659984f36edfc1bef1c7913f53601e5dbbbec7
[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 { ActorModel } from '../../models/activitypub/actor'
14 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
15 import { VideoModel } from '../../models/video/video'
16 import { VideoChannelModel } from '../../models/video/video-channel'
17 import { VideoCommentModel } from '../../models/video/video-comment'
18 import { VideoShareModel } from '../../models/video/video-share'
19
20 const activityPubClientRouter = express.Router()
21
22 activityPubClientRouter.get('/accounts?/:name',
23 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
24 executeIfActivityPub(accountController)
25 )
26 activityPubClientRouter.get('/accounts?/:name/followers',
27 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
28 executeIfActivityPub(asyncMiddleware(accountFollowersController))
29 )
30 activityPubClientRouter.get('/accounts?/:name/following',
31 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
32 executeIfActivityPub(asyncMiddleware(accountFollowingController))
33 )
34
35 activityPubClientRouter.get('/videos/watch/:id',
36 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
37 executeIfActivityPub(asyncMiddleware(videoController))
38 )
39 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
40 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
41 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
42 )
43 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
44 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
45 executeIfActivityPub(asyncMiddleware(videoCommentController))
46 )
47
48 activityPubClientRouter.get('/video-channels/:id',
49 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
50 executeIfActivityPub(asyncMiddleware(videoChannelController))
51 )
52 activityPubClientRouter.get('/video-channels/:id/followers',
53 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
54 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
55 )
56 activityPubClientRouter.get('/video-channels/:id/following',
57 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
58 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
59 )
60
61 // ---------------------------------------------------------------------------
62
63 export {
64 activityPubClientRouter
65 }
66
67 // ---------------------------------------------------------------------------
68
69 function 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
76 async 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
83 async 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
90 async 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
100 async 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
107 async 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
113 async 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
120 async 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
127 async 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
136 async 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
144 async 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 }