diff options
Diffstat (limited to 'server/controllers/activitypub/client.ts')
-rw-r--r-- | server/controllers/activitypub/client.ts | 86 |
1 files changed, 50 insertions, 36 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 767fde5d9..1c780783c 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts | |||
@@ -1,9 +1,8 @@ | |||
1 | // Intercept ActivityPub client requests | 1 | // Intercept ActivityPub client requests |
2 | import * as express from 'express' | 2 | import * as express from 'express' |
3 | import { VideoPrivacy } from '../../../shared/models/videos' | 3 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' |
4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' | 4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' |
5 | import { pageToStartAndCount } from '../../helpers/core-utils' | 5 | import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' |
6 | import { ACTIVITY_PUB, CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' | ||
7 | import { buildVideoAnnounce } from '../../lib/activitypub/send' | 6 | import { buildVideoAnnounce } from '../../lib/activitypub/send' |
8 | import { audiencify, getAudience } from '../../lib/activitypub/audience' | 7 | import { audiencify, getAudience } from '../../lib/activitypub/audience' |
9 | import { createActivityData } from '../../lib/activitypub/send/send-create' | 8 | import { createActivityData } from '../../lib/activitypub/send/send-create' |
@@ -18,6 +17,14 @@ import { VideoChannelModel } from '../../models/video/video-channel' | |||
18 | import { VideoCommentModel } from '../../models/video/video-comment' | 17 | import { VideoCommentModel } from '../../models/video/video-comment' |
19 | import { VideoShareModel } from '../../models/video/video-share' | 18 | import { VideoShareModel } from '../../models/video/video-share' |
20 | import { cacheRoute } from '../../middlewares/cache' | 19 | import { cacheRoute } from '../../middlewares/cache' |
20 | import { activityPubResponse } from './utils' | ||
21 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | ||
22 | import { | ||
23 | getVideoCommentsActivityPubUrl, | ||
24 | getVideoDislikesActivityPubUrl, | ||
25 | getVideoLikesActivityPubUrl, | ||
26 | getVideoSharesActivityPubUrl | ||
27 | } from '../../lib/activitypub' | ||
21 | 28 | ||
22 | const activityPubClientRouter = express.Router() | 29 | const activityPubClientRouter = express.Router() |
23 | 30 | ||
@@ -116,10 +123,8 @@ async function accountFollowingController (req: express.Request, res: express.Re | |||
116 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { | 123 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { |
117 | const video: VideoModel = res.locals.video | 124 | const video: VideoModel = res.locals.video |
118 | 125 | ||
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) | 126 | const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC) |
122 | const videoObject = audiencify(videoAll.toActivityPubObject(), audience) | 127 | const videoObject = audiencify(video.toActivityPubObject(), audience) |
123 | 128 | ||
124 | if (req.path.endsWith('/activity')) { | 129 | if (req.path.endsWith('/activity')) { |
125 | const data = await createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, undefined, audience) | 130 | const data = await createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, undefined, audience) |
@@ -139,41 +144,45 @@ async function videoAnnounceController (req: express.Request, res: express.Respo | |||
139 | async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) { | 144 | async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) { |
140 | const video: VideoModel = res.locals.video | 145 | const video: VideoModel = res.locals.video |
141 | 146 | ||
142 | // We need more attributes | 147 | const handler = async (start: number, count: number) => { |
143 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | 148 | const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) |
144 | const object = videoAll.toAnnouncesActivityPubObject() | 149 | return { |
150 | total: result.count, | ||
151 | data: result.rows.map(r => r.url) | ||
152 | } | ||
153 | } | ||
154 | const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page) | ||
145 | 155 | ||
146 | return activityPubResponse(activityPubContextify(object), res) | 156 | return activityPubResponse(activityPubContextify(json), res) |
147 | } | 157 | } |
148 | 158 | ||
149 | async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) { | 159 | async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) { |
150 | const video: VideoModel = res.locals.video | 160 | const video: VideoModel = res.locals.video |
161 | const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) | ||
151 | 162 | ||
152 | // We need more attributes | 163 | return activityPubResponse(activityPubContextify(json), res) |
153 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | ||
154 | const { likesObject } = videoAll.toRatesActivityPubObjects() | ||
155 | |||
156 | return activityPubResponse(activityPubContextify(likesObject), res) | ||
157 | } | 164 | } |
158 | 165 | ||
159 | async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) { | 166 | async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) { |
160 | const video: VideoModel = res.locals.video | 167 | const video: VideoModel = res.locals.video |
168 | const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) | ||
161 | 169 | ||
162 | // We need more attributes | 170 | return activityPubResponse(activityPubContextify(json), res) |
163 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | ||
164 | const { dislikesObject } = videoAll.toRatesActivityPubObjects() | ||
165 | |||
166 | return activityPubResponse(activityPubContextify(dislikesObject), res) | ||
167 | } | 171 | } |
168 | 172 | ||
169 | async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) { | 173 | async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) { |
170 | const video: VideoModel = res.locals.video | 174 | const video: VideoModel = res.locals.video |
171 | 175 | ||
172 | // We need more attributes | 176 | const handler = async (start: number, count: number) => { |
173 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | 177 | const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count) |
174 | const commentsObject = videoAll.toCommentsActivityPubObject() | 178 | return { |
179 | total: result.count, | ||
180 | data: result.rows.map(r => r.url) | ||
181 | } | ||
182 | } | ||
183 | const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page) | ||
175 | 184 | ||
176 | return activityPubResponse(activityPubContextify(commentsObject), res) | 185 | return activityPubResponse(activityPubContextify(json), res) |
177 | } | 186 | } |
178 | 187 | ||
179 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { | 188 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
@@ -216,23 +225,28 @@ async function videoCommentController (req: express.Request, res: express.Respon | |||
216 | // --------------------------------------------------------------------------- | 225 | // --------------------------------------------------------------------------- |
217 | 226 | ||
218 | async function actorFollowing (req: express.Request, actor: ActorModel) { | 227 | async function actorFollowing (req: express.Request, actor: ActorModel) { |
219 | const page = req.query.page || 1 | 228 | const handler = (start: number, count: number) => { |
220 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) | 229 | return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) |
230 | } | ||
221 | 231 | ||
222 | const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) | 232 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) |
223 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) | ||
224 | } | 233 | } |
225 | 234 | ||
226 | async function actorFollowers (req: express.Request, actor: ActorModel) { | 235 | async function actorFollowers (req: express.Request, actor: ActorModel) { |
227 | const page = req.query.page || 1 | 236 | const handler = (start: number, count: number) => { |
228 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) | 237 | return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count) |
238 | } | ||
229 | 239 | ||
230 | const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count) | 240 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) |
231 | return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) | ||
232 | } | 241 | } |
233 | 242 | ||
234 | function activityPubResponse (data: any, res: express.Response) { | 243 | function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) { |
235 | return res.type('application/activity+json; charset=utf-8') | 244 | const handler = async (start: number, count: number) => { |
236 | .json(data) | 245 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) |
237 | .end() | 246 | return { |
247 | total: result.count, | ||
248 | data: result.rows.map(r => r.Account.Actor.url) | ||
249 | } | ||
250 | } | ||
251 | return activityPubCollectionPagination(url, handler, req.query.page) | ||
238 | } | 252 | } |