]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
Fix resolution for portrait videos
[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, activityPubContextify } 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',
40 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
41 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
42 )
43 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
44 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
45 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
46 )
47 activityPubClientRouter.get('/videos/watch/:id/likes',
48 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
49 executeIfActivityPub(asyncMiddleware(videoLikesController))
50 )
51 activityPubClientRouter.get('/videos/watch/:id/dislikes',
52 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
53 executeIfActivityPub(asyncMiddleware(videoDislikesController))
54 )
55 activityPubClientRouter.get('/videos/watch/:id/comments',
56 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
57 executeIfActivityPub(asyncMiddleware(videoCommentsController))
58 )
59 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
60 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
61 executeIfActivityPub(asyncMiddleware(videoCommentController))
62 )
63
64 activityPubClientRouter.get('/video-channels/:id',
65 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
66 executeIfActivityPub(asyncMiddleware(videoChannelController))
67 )
68 activityPubClientRouter.get('/video-channels/:id/followers',
69 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
70 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
71 )
72 activityPubClientRouter.get('/video-channels/:id/following',
73 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
74 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
75 )
76
77 // ---------------------------------------------------------------------------
78
79 export {
80 activityPubClientRouter
81 }
82
83 // ---------------------------------------------------------------------------
84
85 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
86 const account: AccountModel = res.locals.account
87
88 return res.json(activityPubContextify(account.toActivityPubObject()))
89 .end()
90 }
91
92 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
93 const account: AccountModel = res.locals.account
94 const activityPubResult = await actorFollowers(req, account.Actor)
95
96 return res.json(activityPubContextify(activityPubResult))
97 }
98
99 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
100 const account: AccountModel = res.locals.account
101 const activityPubResult = await actorFollowing(req, account.Actor)
102
103 return res.json(activityPubContextify(activityPubResult))
104 }
105
106 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
107 const video: VideoModel = res.locals.video
108
109 // We need more attributes
110 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
111 const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC)
112 const videoObject = audiencify(videoAll.toActivityPubObject(), audience)
113
114 return res.json(activityPubContextify(videoObject))
115 }
116
117 async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
118 const share = res.locals.videoShare as VideoShareModel
119 const object = await buildVideoAnnounceToFollowers(share.Actor, share, res.locals.video, undefined)
120
121 return res.json(activityPubContextify(object))
122 }
123
124 async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
125 const video: VideoModel = res.locals.video
126
127 // We need more attributes
128 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
129 const object = videoAll.toAnnouncesActivityPubObject()
130
131 return res.json(activityPubContextify(object))
132 }
133
134 async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
135 const video: VideoModel = res.locals.video
136
137 // We need more attributes
138 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
139 const { likesObject } = videoAll.toRatesActivityPubObjects()
140
141 return res.json(activityPubContextify(likesObject))
142 }
143
144 async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
145 const video: VideoModel = res.locals.video
146
147 // We need more attributes
148 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
149 const { dislikesObject } = videoAll.toRatesActivityPubObjects()
150
151 return res.json(activityPubContextify(dislikesObject))
152 }
153
154 async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
155 const video: VideoModel = res.locals.video
156
157 // We need more attributes
158 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
159 const commentsObject = videoAll.toCommentsActivityPubObject()
160
161 return res.json(activityPubContextify(commentsObject))
162 }
163
164 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
165 const videoChannel: VideoChannelModel = res.locals.videoChannel
166
167 return res.json(activityPubContextify(videoChannel.toActivityPubObject()))
168 }
169
170 async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
171 const videoChannel: VideoChannelModel = res.locals.videoChannel
172 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
173
174 return res.json(activityPubContextify(activityPubResult))
175 }
176
177 async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
178 const videoChannel: VideoChannelModel = res.locals.videoChannel
179 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
180
181 return res.json(activityPubContextify(activityPubResult))
182 }
183
184 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
185 const videoComment: VideoCommentModel = res.locals.videoComment
186
187 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
188 const isPublic = true // Comments are always public
189 const audience = await getAudience(videoComment.Account.Actor, undefined, isPublic)
190
191 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
192
193 return res.json(activityPubContextify(videoCommentObject))
194 }
195
196 // ---------------------------------------------------------------------------
197
198 async function actorFollowing (req: express.Request, actor: ActorModel) {
199 const page = req.query.page || 1
200 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
201
202 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
203 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
204 }
205
206 async function actorFollowers (req: express.Request, actor: ActorModel) {
207 const page = req.query.page || 1
208 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
209
210 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
211 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
212 }