Split files in activitypub server
[oweals/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, ROUTE_CACHE_LIFETIME } from '../../initializers'
7 import { buildVideoAnnounce } from '../../lib/activitypub/send'
8 import { audiencify, getAudience } from '../../lib/activitypub/audience'
9 import { createActivityData } from '../../lib/activitypub/send/send-create'
10 import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares'
11 import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators'
12 import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
13 import { AccountModel } from '../../models/account/account'
14 import { ActorModel } from '../../models/activitypub/actor'
15 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
16 import { VideoModel } from '../../models/video/video'
17 import { VideoChannelModel } from '../../models/video/video-channel'
18 import { VideoCommentModel } from '../../models/video/video-comment'
19 import { VideoShareModel } from '../../models/video/video-share'
20 import { cacheRoute } from '../../middlewares/cache'
21
22 const activityPubClientRouter = express.Router()
23
24 activityPubClientRouter.get('/accounts?/:name',
25   executeIfActivityPub(asyncMiddleware(localAccountValidator)),
26   executeIfActivityPub(accountController)
27 )
28 activityPubClientRouter.get('/accounts?/:name/followers',
29   executeIfActivityPub(asyncMiddleware(localAccountValidator)),
30   executeIfActivityPub(asyncMiddleware(accountFollowersController))
31 )
32 activityPubClientRouter.get('/accounts?/:name/following',
33   executeIfActivityPub(asyncMiddleware(localAccountValidator)),
34   executeIfActivityPub(asyncMiddleware(accountFollowingController))
35 )
36
37 activityPubClientRouter.get('/videos/watch/:id',
38   executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
39   executeIfActivityPub(asyncMiddleware(videosGetValidator)),
40   executeIfActivityPub(asyncMiddleware(videoController))
41 )
42 activityPubClientRouter.get('/videos/watch/:id/activity',
43   executeIfActivityPub(asyncMiddleware(videosGetValidator)),
44   executeIfActivityPub(asyncMiddleware(videoController))
45 )
46 activityPubClientRouter.get('/videos/watch/:id/announces',
47   executeIfActivityPub(asyncMiddleware(videosGetValidator)),
48   executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
49 )
50 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
51   executeIfActivityPub(asyncMiddleware(videosShareValidator)),
52   executeIfActivityPub(asyncMiddleware(videoAnnounceController))
53 )
54 activityPubClientRouter.get('/videos/watch/:id/likes',
55   executeIfActivityPub(asyncMiddleware(videosGetValidator)),
56   executeIfActivityPub(asyncMiddleware(videoLikesController))
57 )
58 activityPubClientRouter.get('/videos/watch/:id/dislikes',
59   executeIfActivityPub(asyncMiddleware(videosGetValidator)),
60   executeIfActivityPub(asyncMiddleware(videoDislikesController))
61 )
62 activityPubClientRouter.get('/videos/watch/:id/comments',
63   executeIfActivityPub(asyncMiddleware(videosGetValidator)),
64   executeIfActivityPub(asyncMiddleware(videoCommentsController))
65 )
66 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
67   executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
68   executeIfActivityPub(asyncMiddleware(videoCommentController))
69 )
70 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
71   executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
72   executeIfActivityPub(asyncMiddleware(videoCommentController))
73 )
74
75 activityPubClientRouter.get('/video-channels/:id',
76   executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
77   executeIfActivityPub(asyncMiddleware(videoChannelController))
78 )
79 activityPubClientRouter.get('/video-channels/:id/followers',
80   executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
81   executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
82 )
83 activityPubClientRouter.get('/video-channels/:id/following',
84   executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
85   executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
86 )
87
88 // ---------------------------------------------------------------------------
89
90 export {
91   activityPubClientRouter
92 }
93
94 // ---------------------------------------------------------------------------
95
96 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
97   const account: AccountModel = res.locals.account
98
99   return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
100 }
101
102 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
103   const account: AccountModel = res.locals.account
104   const activityPubResult = await actorFollowers(req, account.Actor)
105
106   return activityPubResponse(activityPubContextify(activityPubResult), res)
107 }
108
109 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
110   const account: AccountModel = res.locals.account
111   const activityPubResult = await actorFollowing(req, account.Actor)
112
113   return activityPubResponse(activityPubContextify(activityPubResult), res)
114 }
115
116 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
117   const video: VideoModel = res.locals.video
118
119   // We need more attributes
120   const videoAll = await VideoModel.loadAndPopulateAll(video.id)
121   const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC)
122   const videoObject = audiencify(videoAll.toActivityPubObject(), audience)
123
124   if (req.path.endsWith('/activity')) {
125     const data = await createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, undefined, audience)
126     return activityPubResponse(activityPubContextify(data), res)
127   }
128
129   return activityPubResponse(activityPubContextify(videoObject), res)
130 }
131
132 async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
133   const share = res.locals.videoShare as VideoShareModel
134   const object = await buildVideoAnnounce(share.Actor, share, res.locals.video, undefined)
135
136   return activityPubResponse(activityPubContextify(object), res)
137 }
138
139 async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
140   const video: VideoModel = res.locals.video
141
142   // We need more attributes
143   const videoAll = await VideoModel.loadAndPopulateAll(video.id)
144   const object = videoAll.toAnnouncesActivityPubObject()
145
146   return activityPubResponse(activityPubContextify(object), res)
147 }
148
149 async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
150   const video: VideoModel = res.locals.video
151
152   // We need more attributes
153   const videoAll = await VideoModel.loadAndPopulateAll(video.id)
154   const { likesObject } = videoAll.toRatesActivityPubObjects()
155
156   return activityPubResponse(activityPubContextify(likesObject), res)
157 }
158
159 async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
160   const video: VideoModel = res.locals.video
161
162   // We need more attributes
163   const videoAll = await VideoModel.loadAndPopulateAll(video.id)
164   const { dislikesObject } = videoAll.toRatesActivityPubObjects()
165
166   return activityPubResponse(activityPubContextify(dislikesObject), res)
167 }
168
169 async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
170   const video: VideoModel = res.locals.video
171
172   // We need more attributes
173   const videoAll = await VideoModel.loadAndPopulateAll(video.id)
174   const commentsObject = videoAll.toCommentsActivityPubObject()
175
176   return activityPubResponse(activityPubContextify(commentsObject), res)
177 }
178
179 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
180   const videoChannel: VideoChannelModel = res.locals.videoChannel
181
182   return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
183 }
184
185 async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
186   const videoChannel: VideoChannelModel = res.locals.videoChannel
187   const activityPubResult = await actorFollowers(req, videoChannel.Actor)
188
189   return activityPubResponse(activityPubContextify(activityPubResult), res)
190 }
191
192 async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
193   const videoChannel: VideoChannelModel = res.locals.videoChannel
194   const activityPubResult = await actorFollowing(req, videoChannel.Actor)
195
196   return activityPubResponse(activityPubContextify(activityPubResult), res)
197 }
198
199 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
200   const videoComment: VideoCommentModel = res.locals.videoComment
201
202   const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
203   const isPublic = true // Comments are always public
204   const audience = await getAudience(videoComment.Account.Actor, undefined, isPublic)
205
206   const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
207
208   if (req.path.endsWith('/activity')) {
209     const data = await createActivityData(videoComment.url, videoComment.Account.Actor, videoCommentObject, undefined, audience)
210     return activityPubResponse(activityPubContextify(data), res)
211   }
212
213   return activityPubResponse(activityPubContextify(videoCommentObject), res)
214 }
215
216 // ---------------------------------------------------------------------------
217
218 async function actorFollowing (req: express.Request, actor: ActorModel) {
219   const page = req.query.page || 1
220   const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
221
222   const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
223   return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
224 }
225
226 async function actorFollowers (req: express.Request, actor: ActorModel) {
227   const page = req.query.page || 1
228   const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
229
230   const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
231   return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
232 }
233
234 function activityPubResponse (data: any, res: express.Response) {
235   return res.type('application/activity+json; charset=utf-8')
236     .json(data)
237     .end()
238 }