]>
Commit | Line | Data |
---|---|---|
e4f97bab C |
1 | // Intercept ActivityPub client requests |
2 | import * as express from 'express' | |
2ccaeeb3 | 3 | import { VideoPrivacy } from '../../../shared/models/videos' |
d6e99e53 | 4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' |
da854ddd | 5 | import { pageToStartAndCount } from '../../helpers/core-utils' |
fd4484f1 | 6 | import { ACTIVITY_PUB, CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' |
07197db4 | 7 | import { buildVideoAnnounce } from '../../lib/activitypub/send' |
e251f170 | 8 | import { audiencify, getAudience } from '../../lib/activitypub/audience' |
296c0905 | 9 | import { createActivityData } from '../../lib/activitypub/send/send-create' |
3fd3ab2d | 10 | import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares' |
50d6de9c | 11 | import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators' |
da854ddd | 12 | import { videoCommentGetValidator } from '../../middlewares/validators/video-comments' |
3fd3ab2d | 13 | import { AccountModel } from '../../models/account/account' |
7006bc63 | 14 | import { ActorModel } from '../../models/activitypub/actor' |
50d6de9c | 15 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
3fd3ab2d C |
16 | import { VideoModel } from '../../models/video/video' |
17 | import { VideoChannelModel } from '../../models/video/video-channel' | |
da854ddd | 18 | import { VideoCommentModel } from '../../models/video/video-comment' |
3fd3ab2d | 19 | import { VideoShareModel } from '../../models/video/video-share' |
fd4484f1 | 20 | import { cacheRoute } from '../../middlewares/cache' |
e4f97bab C |
21 | |
22 | const activityPubClientRouter = express.Router() | |
23 | ||
108af661 | 24 | activityPubClientRouter.get('/accounts?/:name', |
a2431b7d | 25 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
4e50b6a1 | 26 | executeIfActivityPub(accountController) |
e4f97bab | 27 | ) |
7006bc63 | 28 | activityPubClientRouter.get('/accounts?/:name/followers', |
a2431b7d | 29 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
30 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) |
31 | ) | |
7006bc63 | 32 | activityPubClientRouter.get('/accounts?/:name/following', |
a2431b7d | 33 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
34 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
35 | ) | |
36 | ||
20494f12 | 37 | activityPubClientRouter.get('/videos/watch/:id', |
fd4484f1 | 38 | executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), |
a2431b7d | 39 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), |
da854ddd | 40 | executeIfActivityPub(asyncMiddleware(videoController)) |
4e50b6a1 | 41 | ) |
296c0905 C |
42 | activityPubClientRouter.get('/videos/watch/:id/activity', |
43 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | |
44 | executeIfActivityPub(asyncMiddleware(videoController)) | |
45 | ) | |
46531a0a C |
46 | activityPubClientRouter.get('/videos/watch/:id/announces', |
47 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | |
48 | executeIfActivityPub(asyncMiddleware(videoAnnouncesController)) | |
49 | ) | |
4e50b6a1 C |
50 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', |
51 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | |
52 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | |
20494f12 | 53 | ) |
46531a0a C |
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 | ) | |
da854ddd C |
66 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', |
67 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | |
68 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | |
69 | ) | |
296c0905 C |
70 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity', |
71 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | |
72 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | |
73 | ) | |
da854ddd | 74 | |
20494f12 | 75 | activityPubClientRouter.get('/video-channels/:id', |
a2431b7d | 76 | executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)), |
20494f12 C |
77 | executeIfActivityPub(asyncMiddleware(videoChannelController)) |
78 | ) | |
7006bc63 C |
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 | ) | |
20494f12 | 87 | |
e4f97bab C |
88 | // --------------------------------------------------------------------------- |
89 | ||
90 | export { | |
91 | activityPubClientRouter | |
92 | } | |
93 | ||
94 | // --------------------------------------------------------------------------- | |
95 | ||
4e50b6a1 | 96 | function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 97 | const account: AccountModel = res.locals.account |
e4f97bab | 98 | |
4b8f09fa | 99 | return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res) |
e4f97bab C |
100 | } |
101 | ||
102 | async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 103 | const account: AccountModel = res.locals.account |
7006bc63 | 104 | const activityPubResult = await actorFollowers(req, account.Actor) |
e4f97bab | 105 | |
4b8f09fa | 106 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
e4f97bab C |
107 | } |
108 | ||
109 | async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 110 | const account: AccountModel = res.locals.account |
7006bc63 | 111 | const activityPubResult = await actorFollowing(req, account.Actor) |
e4f97bab | 112 | |
4b8f09fa | 113 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
e4f97bab | 114 | } |
20494f12 | 115 | |
da854ddd | 116 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 117 | const video: VideoModel = res.locals.video |
20494f12 | 118 | |
da854ddd C |
119 | // We need more attributes |
120 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | |
2ccaeeb3 | 121 | const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC) |
d6e99e53 | 122 | const videoObject = audiencify(videoAll.toActivityPubObject(), audience) |
2ccaeeb3 | 123 | |
296c0905 C |
124 | if (req.path.endsWith('/activity')) { |
125 | const data = await createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, undefined, audience) | |
4b8f09fa | 126 | return activityPubResponse(activityPubContextify(data), res) |
296c0905 C |
127 | } |
128 | ||
4b8f09fa | 129 | return activityPubResponse(activityPubContextify(videoObject), res) |
20494f12 C |
130 | } |
131 | ||
4e50b6a1 | 132 | async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 133 | const share = res.locals.videoShare as VideoShareModel |
07197db4 | 134 | const object = await buildVideoAnnounce(share.Actor, share, res.locals.video, undefined) |
4e50b6a1 | 135 | |
4b8f09fa | 136 | return activityPubResponse(activityPubContextify(object), res) |
4e50b6a1 C |
137 | } |
138 | ||
46531a0a C |
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 | ||
4b8f09fa | 146 | return activityPubResponse(activityPubContextify(object), res) |
46531a0a C |
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 | ||
4b8f09fa | 156 | return activityPubResponse(activityPubContextify(likesObject), res) |
46531a0a C |
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 | ||
4b8f09fa | 166 | return activityPubResponse(activityPubContextify(dislikesObject), res) |
46531a0a C |
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 | ||
4b8f09fa | 176 | return activityPubResponse(activityPubContextify(commentsObject), res) |
46531a0a C |
177 | } |
178 | ||
20494f12 | 179 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 180 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
20494f12 | 181 | |
4b8f09fa | 182 | return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res) |
20494f12 | 183 | } |
da854ddd | 184 | |
7006bc63 C |
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 | ||
4b8f09fa | 189 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
7006bc63 C |
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 | ||
4b8f09fa | 196 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
7006bc63 C |
197 | } |
198 | ||
da854ddd C |
199 | async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) { |
200 | const videoComment: VideoCommentModel = res.locals.videoComment | |
201 | ||
d7e70384 | 202 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) |
d6e99e53 C |
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 | ||
296c0905 C |
208 | if (req.path.endsWith('/activity')) { |
209 | const data = await createActivityData(videoComment.url, videoComment.Account.Actor, videoCommentObject, undefined, audience) | |
4b8f09fa | 210 | return activityPubResponse(activityPubContextify(data), res) |
296c0905 C |
211 | } |
212 | ||
4b8f09fa | 213 | return activityPubResponse(activityPubContextify(videoCommentObject), res) |
da854ddd | 214 | } |
7006bc63 C |
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 | } | |
4b8f09fa C |
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 | } |