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'
22 const activityPubClientRouter = express.Router()
24 activityPubClientRouter.get('/accounts?/:name',
25 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
26 executeIfActivityPub(accountController)
28 activityPubClientRouter.get('/accounts?/:name/followers',
29 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
30 executeIfActivityPub(asyncMiddleware(accountFollowersController))
32 activityPubClientRouter.get('/accounts?/:name/following',
33 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
34 executeIfActivityPub(asyncMiddleware(accountFollowingController))
37 activityPubClientRouter.get('/videos/watch/:id',
38 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
39 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
40 executeIfActivityPub(asyncMiddleware(videoController))
42 activityPubClientRouter.get('/videos/watch/:id/activity',
43 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
44 executeIfActivityPub(asyncMiddleware(videoController))
46 activityPubClientRouter.get('/videos/watch/:id/announces',
47 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
48 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
50 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
51 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
52 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
54 activityPubClientRouter.get('/videos/watch/:id/likes',
55 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
56 executeIfActivityPub(asyncMiddleware(videoLikesController))
58 activityPubClientRouter.get('/videos/watch/:id/dislikes',
59 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
60 executeIfActivityPub(asyncMiddleware(videoDislikesController))
62 activityPubClientRouter.get('/videos/watch/:id/comments',
63 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
64 executeIfActivityPub(asyncMiddleware(videoCommentsController))
66 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
67 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
68 executeIfActivityPub(asyncMiddleware(videoCommentController))
70 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
71 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
72 executeIfActivityPub(asyncMiddleware(videoCommentController))
75 activityPubClientRouter.get('/video-channels/:id',
76 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
77 executeIfActivityPub(asyncMiddleware(videoChannelController))
79 activityPubClientRouter.get('/video-channels/:id/followers',
80 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
81 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
83 activityPubClientRouter.get('/video-channels/:id/following',
84 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
85 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
88 // ---------------------------------------------------------------------------
91 activityPubClientRouter
94 // ---------------------------------------------------------------------------
96 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
97 const account: AccountModel = res.locals.account
99 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
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)
106 return activityPubResponse(activityPubContextify(activityPubResult), res)
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)
113 return activityPubResponse(activityPubContextify(activityPubResult), res)
116 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
117 const video: VideoModel = res.locals.video
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)
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)
129 return activityPubResponse(activityPubContextify(videoObject), res)
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)
136 return activityPubResponse(activityPubContextify(object), res)
139 async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
140 const video: VideoModel = res.locals.video
142 // We need more attributes
143 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
144 const object = videoAll.toAnnouncesActivityPubObject()
146 return activityPubResponse(activityPubContextify(object), res)
149 async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
150 const video: VideoModel = res.locals.video
152 // We need more attributes
153 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
154 const { likesObject } = videoAll.toRatesActivityPubObjects()
156 return activityPubResponse(activityPubContextify(likesObject), res)
159 async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
160 const video: VideoModel = res.locals.video
162 // We need more attributes
163 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
164 const { dislikesObject } = videoAll.toRatesActivityPubObjects()
166 return activityPubResponse(activityPubContextify(dislikesObject), res)
169 async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
170 const video: VideoModel = res.locals.video
172 // We need more attributes
173 const videoAll = await VideoModel.loadAndPopulateAll(video.id)
174 const commentsObject = videoAll.toCommentsActivityPubObject()
176 return activityPubResponse(activityPubContextify(commentsObject), res)
179 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
180 const videoChannel: VideoChannelModel = res.locals.videoChannel
182 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
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)
189 return activityPubResponse(activityPubContextify(activityPubResult), res)
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)
196 return activityPubResponse(activityPubContextify(activityPubResult), res)
199 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
200 const videoComment: VideoCommentModel = res.locals.videoComment
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)
206 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
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)
213 return activityPubResponse(activityPubContextify(videoCommentObject), res)
216 // ---------------------------------------------------------------------------
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)
222 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
223 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
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)
230 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
231 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
234 function activityPubResponse (data: any, res: express.Response) {
235 return res.type('application/activity+json; charset=utf-8')