]>
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' |
3fd3ab2d | 6 | import { ACTIVITY_PUB, CONFIG } from '../../initializers' |
50d6de9c | 7 | import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send' |
2ccaeeb3 | 8 | import { audiencify, getAudience } from '../../lib/activitypub/send/misc' |
3fd3ab2d | 9 | import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares' |
50d6de9c | 10 | import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators' |
da854ddd | 11 | import { videoCommentGetValidator } from '../../middlewares/validators/video-comments' |
3fd3ab2d | 12 | import { AccountModel } from '../../models/account/account' |
7006bc63 | 13 | import { ActorModel } from '../../models/activitypub/actor' |
50d6de9c | 14 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
3fd3ab2d C |
15 | import { VideoModel } from '../../models/video/video' |
16 | import { VideoChannelModel } from '../../models/video/video-channel' | |
da854ddd | 17 | import { VideoCommentModel } from '../../models/video/video-comment' |
3fd3ab2d | 18 | import { VideoShareModel } from '../../models/video/video-share' |
e4f97bab C |
19 | |
20 | const activityPubClientRouter = express.Router() | |
21 | ||
108af661 | 22 | activityPubClientRouter.get('/accounts?/:name', |
a2431b7d | 23 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
4e50b6a1 | 24 | executeIfActivityPub(accountController) |
e4f97bab | 25 | ) |
7006bc63 | 26 | activityPubClientRouter.get('/accounts?/:name/followers', |
a2431b7d | 27 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
28 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) |
29 | ) | |
7006bc63 | 30 | activityPubClientRouter.get('/accounts?/:name/following', |
a2431b7d | 31 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
e4f97bab C |
32 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
33 | ) | |
34 | ||
20494f12 | 35 | activityPubClientRouter.get('/videos/watch/:id', |
a2431b7d | 36 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), |
da854ddd | 37 | executeIfActivityPub(asyncMiddleware(videoController)) |
4e50b6a1 | 38 | ) |
46531a0a C |
39 | activityPubClientRouter.get('/videos/watch/:id/announces', |
40 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | |
41 | executeIfActivityPub(asyncMiddleware(videoAnnouncesController)) | |
42 | ) | |
4e50b6a1 C |
43 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', |
44 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | |
45 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | |
20494f12 | 46 | ) |
46531a0a C |
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 | ) | |
da854ddd C |
59 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', |
60 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | |
61 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | |
62 | ) | |
63 | ||
20494f12 | 64 | activityPubClientRouter.get('/video-channels/:id', |
a2431b7d | 65 | executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)), |
20494f12 C |
66 | executeIfActivityPub(asyncMiddleware(videoChannelController)) |
67 | ) | |
7006bc63 C |
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 | ) | |
20494f12 | 76 | |
e4f97bab C |
77 | // --------------------------------------------------------------------------- |
78 | ||
79 | export { | |
80 | activityPubClientRouter | |
81 | } | |
82 | ||
83 | // --------------------------------------------------------------------------- | |
84 | ||
4e50b6a1 | 85 | function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 86 | const account: AccountModel = res.locals.account |
e4f97bab | 87 | |
d6e99e53 | 88 | return res.json(activityPubContextify(account.toActivityPubObject())) |
da854ddd | 89 | .end() |
e4f97bab C |
90 | } |
91 | ||
92 | async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 93 | const account: AccountModel = res.locals.account |
7006bc63 | 94 | const activityPubResult = await actorFollowers(req, account.Actor) |
e4f97bab | 95 | |
d6e99e53 | 96 | return res.json(activityPubContextify(activityPubResult)) |
e4f97bab C |
97 | } |
98 | ||
99 | async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 100 | const account: AccountModel = res.locals.account |
7006bc63 | 101 | const activityPubResult = await actorFollowing(req, account.Actor) |
e4f97bab | 102 | |
d6e99e53 | 103 | return res.json(activityPubContextify(activityPubResult)) |
e4f97bab | 104 | } |
20494f12 | 105 | |
da854ddd | 106 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 107 | const video: VideoModel = res.locals.video |
20494f12 | 108 | |
da854ddd C |
109 | // We need more attributes |
110 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | |
2ccaeeb3 | 111 | const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC) |
d6e99e53 | 112 | const videoObject = audiencify(videoAll.toActivityPubObject(), audience) |
2ccaeeb3 | 113 | |
d6e99e53 | 114 | return res.json(activityPubContextify(videoObject)) |
20494f12 C |
115 | } |
116 | ||
4e50b6a1 | 117 | async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 118 | const share = res.locals.videoShare as VideoShareModel |
4ba3b8ea | 119 | const object = await buildVideoAnnounceToFollowers(share.Actor, share, res.locals.video, undefined) |
4e50b6a1 | 120 | |
d6e99e53 | 121 | return res.json(activityPubContextify(object)) |
4e50b6a1 C |
122 | } |
123 | ||
46531a0a C |
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 | ||
20494f12 | 164 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 165 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
20494f12 | 166 | |
d6e99e53 | 167 | return res.json(activityPubContextify(videoChannel.toActivityPubObject())) |
20494f12 | 168 | } |
da854ddd | 169 | |
7006bc63 C |
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 | ||
d6e99e53 | 174 | return res.json(activityPubContextify(activityPubResult)) |
7006bc63 C |
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 | ||
d6e99e53 | 181 | return res.json(activityPubContextify(activityPubResult)) |
7006bc63 C |
182 | } |
183 | ||
da854ddd C |
184 | async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) { |
185 | const videoComment: VideoCommentModel = res.locals.videoComment | |
186 | ||
d7e70384 | 187 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) |
d6e99e53 C |
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)) | |
da854ddd | 194 | } |
7006bc63 C |
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 | } |