1 // Intercept ActivityPub client requests
2 import * as express from 'express'
3 import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
4 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
5 import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
6 import { buildVideoAnnounce } from '../../lib/activitypub/send'
7 import { audiencify, getAudience } from '../../lib/activitypub/audience'
8 import { createActivityData } from '../../lib/activitypub/send/send-create'
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 import { cacheRoute } from '../../middlewares/cache'
20 import { activityPubResponse } from './utils'
21 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
23 getVideoCommentsActivityPubUrl,
24 getVideoDislikesActivityPubUrl,
25 getVideoLikesActivityPubUrl,
26 getVideoSharesActivityPubUrl
27 } from '../../lib/activitypub'
28 import { VideoCaptionModel } from '../../models/video/video-caption'
30 const activityPubClientRouter = express.Router()
32 activityPubClientRouter.get('/accounts?/:name',
33 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
34 executeIfActivityPub(accountController)
36 activityPubClientRouter.get('/accounts?/:name/followers',
37 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
38 executeIfActivityPub(asyncMiddleware(accountFollowersController))
40 activityPubClientRouter.get('/accounts?/:name/following',
41 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
42 executeIfActivityPub(asyncMiddleware(accountFollowingController))
45 activityPubClientRouter.get('/videos/watch/:id',
46 executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
47 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
48 executeIfActivityPub(asyncMiddleware(videoController))
50 activityPubClientRouter.get('/videos/watch/:id/activity',
51 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
52 executeIfActivityPub(asyncMiddleware(videoController))
54 activityPubClientRouter.get('/videos/watch/:id/announces',
55 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
56 executeIfActivityPub(asyncMiddleware(videoAnnouncesController))
58 activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
59 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
60 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
62 activityPubClientRouter.get('/videos/watch/:id/likes',
63 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
64 executeIfActivityPub(asyncMiddleware(videoLikesController))
66 activityPubClientRouter.get('/videos/watch/:id/dislikes',
67 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
68 executeIfActivityPub(asyncMiddleware(videoDislikesController))
70 activityPubClientRouter.get('/videos/watch/:id/comments',
71 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
72 executeIfActivityPub(asyncMiddleware(videoCommentsController))
74 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
75 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
76 executeIfActivityPub(asyncMiddleware(videoCommentController))
78 activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
79 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
80 executeIfActivityPub(asyncMiddleware(videoCommentController))
83 activityPubClientRouter.get('/video-channels/:id',
84 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
85 executeIfActivityPub(asyncMiddleware(videoChannelController))
87 activityPubClientRouter.get('/video-channels/:id/followers',
88 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
89 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
91 activityPubClientRouter.get('/video-channels/:id/following',
92 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
93 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
96 // ---------------------------------------------------------------------------
99 activityPubClientRouter
102 // ---------------------------------------------------------------------------
104 function accountController (req: express.Request, res: express.Response, next: express.NextFunction) {
105 const account: AccountModel = res.locals.account
107 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
110 async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
111 const account: AccountModel = res.locals.account
112 const activityPubResult = await actorFollowers(req, account.Actor)
114 return activityPubResponse(activityPubContextify(activityPubResult), res)
117 async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
118 const account: AccountModel = res.locals.account
119 const activityPubResult = await actorFollowing(req, account.Actor)
121 return activityPubResponse(activityPubContextify(activityPubResult), res)
124 async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) {
125 const video: VideoModel = res.locals.video
127 // We need captions to render AP object
128 video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
130 const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC)
131 const videoObject = audiencify(video.toActivityPubObject(), audience)
133 if (req.path.endsWith('/activity')) {
134 const data = createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
135 return activityPubResponse(activityPubContextify(data), res)
138 return activityPubResponse(activityPubContextify(videoObject), res)
141 async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
142 const share = res.locals.videoShare as VideoShareModel
143 const object = await buildVideoAnnounce(share.Actor, share, res.locals.video, undefined)
145 return activityPubResponse(activityPubContextify(object), res)
148 async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
149 const video: VideoModel = res.locals.video
151 const handler = async (start: number, count: number) => {
152 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
155 data: result.rows.map(r => r.url)
158 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
160 return activityPubResponse(activityPubContextify(json), res)
163 async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
164 const video: VideoModel = res.locals.video
165 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
167 return activityPubResponse(activityPubContextify(json), res)
170 async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) {
171 const video: VideoModel = res.locals.video
172 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
174 return activityPubResponse(activityPubContextify(json), res)
177 async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) {
178 const video: VideoModel = res.locals.video
180 const handler = async (start: number, count: number) => {
181 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
184 data: result.rows.map(r => r.url)
187 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
189 return activityPubResponse(activityPubContextify(json), res)
192 async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) {
193 const videoChannel: VideoChannelModel = res.locals.videoChannel
195 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
198 async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
199 const videoChannel: VideoChannelModel = res.locals.videoChannel
200 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
202 return activityPubResponse(activityPubContextify(activityPubResult), res)
205 async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
206 const videoChannel: VideoChannelModel = res.locals.videoChannel
207 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
209 return activityPubResponse(activityPubContextify(activityPubResult), res)
212 async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
213 const videoComment: VideoCommentModel = res.locals.videoComment
215 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
216 const isPublic = true // Comments are always public
217 const audience = getAudience(videoComment.Account.Actor, isPublic)
219 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
221 if (req.path.endsWith('/activity')) {
222 const data = createActivityData(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
223 return activityPubResponse(activityPubContextify(data), res)
226 return activityPubResponse(activityPubContextify(videoCommentObject), res)
229 // ---------------------------------------------------------------------------
231 async function actorFollowing (req: express.Request, actor: ActorModel) {
232 const handler = (start: number, count: number) => {
233 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
236 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page)
239 async function actorFollowers (req: express.Request, actor: ActorModel) {
240 const handler = (start: number, count: number) => {
241 return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
244 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page)
247 function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) {
248 const handler = async (start: number, count: number) => {
249 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
252 data: result.rows.map(r => r.Account.Actor.url)
255 return activityPubCollectionPagination(url, handler, req.query.page)