diff options
author | Chocobozzz <me@florianbigard.com> | 2019-08-22 10:43:11 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-08-22 10:43:11 +0200 |
commit | 5c5e587307a27e173333789b5b5167d35f468b01 (patch) | |
tree | 94e3721caf2e11d38fd5f4112c0fc98da89ac535 /server/controllers/activitypub | |
parent | 1b42d73f44811eec1b7ddd72dd0d640a57c3376c (diff) | |
parent | b5fecbf44192144d1ca27c23a0b53922de288c10 (diff) | |
download | PeerTube-5c5e587307a27e173333789b5b5167d35f468b01.tar.gz PeerTube-5c5e587307a27e173333789b5b5167d35f468b01.tar.zst PeerTube-5c5e587307a27e173333789b5b5167d35f468b01.zip |
Merge branch 'feature/strong-model-types' into develop
Diffstat (limited to 'server/controllers/activitypub')
-rw-r--r-- | server/controllers/activitypub/client.ts | 39 | ||||
-rw-r--r-- | server/controllers/activitypub/inbox.ts | 5 | ||||
-rw-r--r-- | server/controllers/activitypub/outbox.ts | 10 |
3 files changed, 25 insertions, 29 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 11504b354..453ced8bf 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts | |||
@@ -16,7 +16,6 @@ import { | |||
16 | } from '../../middlewares' | 16 | } from '../../middlewares' |
17 | import { getAccountVideoRateValidator, videoCommentGetValidator } from '../../middlewares/validators' | 17 | import { getAccountVideoRateValidator, videoCommentGetValidator } from '../../middlewares/validators' |
18 | import { AccountModel } from '../../models/account/account' | 18 | import { AccountModel } from '../../models/account/account' |
19 | import { ActorModel } from '../../models/activitypub/actor' | ||
20 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' | 19 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
21 | import { VideoModel } from '../../models/video/video' | 20 | import { VideoModel } from '../../models/video/video' |
22 | import { VideoCommentModel } from '../../models/video/video-comment' | 21 | import { VideoCommentModel } from '../../models/video/video-comment' |
@@ -38,6 +37,7 @@ import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike' | |||
38 | import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' | 37 | import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' |
39 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | 38 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
40 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' | 39 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' |
40 | import { MAccountId, MActorId, MVideo, MVideoAPWithoutCaption } from '@server/typings/models' | ||
41 | 41 | ||
42 | const activityPubClientRouter = express.Router() | 42 | const activityPubClientRouter = express.Router() |
43 | 43 | ||
@@ -148,7 +148,7 @@ activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistT | |||
148 | 148 | ||
149 | activityPubClientRouter.get('/video-playlists/:playlistId', | 149 | activityPubClientRouter.get('/video-playlists/:playlistId', |
150 | executeIfActivityPub, | 150 | executeIfActivityPub, |
151 | asyncMiddleware(videoPlaylistsGetValidator), | 151 | asyncMiddleware(videoPlaylistsGetValidator('all')), |
152 | asyncMiddleware(videoPlaylistController) | 152 | asyncMiddleware(videoPlaylistController) |
153 | ) | 153 | ) |
154 | activityPubClientRouter.get('/video-playlists/:playlistId/:videoId', | 154 | activityPubClientRouter.get('/video-playlists/:playlistId/:videoId', |
@@ -208,18 +208,19 @@ function getAccountVideoRate (rateType: VideoRateType) { | |||
208 | 208 | ||
209 | async function videoController (req: express.Request, res: express.Response) { | 209 | async function videoController (req: express.Request, res: express.Response) { |
210 | // We need more attributes | 210 | // We need more attributes |
211 | const video = await VideoModel.loadForGetAPI({ id: res.locals.video.id }) | 211 | const video = await VideoModel.loadForGetAPI({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption |
212 | 212 | ||
213 | if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url) | 213 | if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url) |
214 | 214 | ||
215 | // We need captions to render AP object | 215 | // We need captions to render AP object |
216 | video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id) | 216 | const captions = await VideoCaptionModel.listVideoCaptions(video.id) |
217 | const videoWithCaptions = Object.assign(video, { VideoCaptions: captions }) | ||
217 | 218 | ||
218 | const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC) | 219 | const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC) |
219 | const videoObject = audiencify(video.toActivityPubObject(), audience) | 220 | const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience) |
220 | 221 | ||
221 | if (req.path.endsWith('/activity')) { | 222 | if (req.path.endsWith('/activity')) { |
222 | const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience) | 223 | const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience) |
223 | return activityPubResponse(activityPubContextify(data), res) | 224 | return activityPubResponse(activityPubContextify(data), res) |
224 | } | 225 | } |
225 | 226 | ||
@@ -231,13 +232,13 @@ async function videoAnnounceController (req: express.Request, res: express.Respo | |||
231 | 232 | ||
232 | if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url) | 233 | if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url) |
233 | 234 | ||
234 | const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined) | 235 | const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined) |
235 | 236 | ||
236 | return activityPubResponse(activityPubContextify(activity), res) | 237 | return activityPubResponse(activityPubContextify(activity), res) |
237 | } | 238 | } |
238 | 239 | ||
239 | async function videoAnnouncesController (req: express.Request, res: express.Response) { | 240 | async function videoAnnouncesController (req: express.Request, res: express.Response) { |
240 | const video = res.locals.video | 241 | const video = res.locals.onlyVideo |
241 | 242 | ||
242 | const handler = async (start: number, count: number) => { | 243 | const handler = async (start: number, count: number) => { |
243 | const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) | 244 | const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) |
@@ -252,21 +253,21 @@ async function videoAnnouncesController (req: express.Request, res: express.Resp | |||
252 | } | 253 | } |
253 | 254 | ||
254 | async function videoLikesController (req: express.Request, res: express.Response) { | 255 | async function videoLikesController (req: express.Request, res: express.Response) { |
255 | const video = res.locals.video | 256 | const video = res.locals.onlyVideo |
256 | const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) | 257 | const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) |
257 | 258 | ||
258 | return activityPubResponse(activityPubContextify(json), res) | 259 | return activityPubResponse(activityPubContextify(json), res) |
259 | } | 260 | } |
260 | 261 | ||
261 | async function videoDislikesController (req: express.Request, res: express.Response) { | 262 | async function videoDislikesController (req: express.Request, res: express.Response) { |
262 | const video = res.locals.video | 263 | const video = res.locals.onlyVideo |
263 | const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) | 264 | const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) |
264 | 265 | ||
265 | return activityPubResponse(activityPubContextify(json), res) | 266 | return activityPubResponse(activityPubContextify(json), res) |
266 | } | 267 | } |
267 | 268 | ||
268 | async function videoCommentsController (req: express.Request, res: express.Response) { | 269 | async function videoCommentsController (req: express.Request, res: express.Response) { |
269 | const video = res.locals.video | 270 | const video = res.locals.onlyVideo |
270 | 271 | ||
271 | const handler = async (start: number, count: number) => { | 272 | const handler = async (start: number, count: number) => { |
272 | const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count) | 273 | const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count) |
@@ -301,7 +302,7 @@ async function videoChannelFollowingController (req: express.Request, res: expre | |||
301 | } | 302 | } |
302 | 303 | ||
303 | async function videoCommentController (req: express.Request, res: express.Response) { | 304 | async function videoCommentController (req: express.Request, res: express.Response) { |
304 | const videoComment = res.locals.videoComment | 305 | const videoComment = res.locals.videoCommentFull |
305 | 306 | ||
306 | if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url) | 307 | if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url) |
307 | 308 | ||
@@ -337,7 +338,7 @@ async function videoRedundancyController (req: express.Request, res: express.Res | |||
337 | } | 338 | } |
338 | 339 | ||
339 | async function videoPlaylistController (req: express.Request, res: express.Response) { | 340 | async function videoPlaylistController (req: express.Request, res: express.Response) { |
340 | const playlist = res.locals.videoPlaylist | 341 | const playlist = res.locals.videoPlaylistFull |
341 | 342 | ||
342 | // We need more attributes | 343 | // We need more attributes |
343 | playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId) | 344 | playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId) |
@@ -350,7 +351,7 @@ async function videoPlaylistController (req: express.Request, res: express.Respo | |||
350 | } | 351 | } |
351 | 352 | ||
352 | async function videoPlaylistElementController (req: express.Request, res: express.Response) { | 353 | async function videoPlaylistElementController (req: express.Request, res: express.Response) { |
353 | const videoPlaylistElement = res.locals.videoPlaylistElement | 354 | const videoPlaylistElement = res.locals.videoPlaylistElementAP |
354 | 355 | ||
355 | const json = videoPlaylistElement.toActivityPubObject() | 356 | const json = videoPlaylistElement.toActivityPubObject() |
356 | return activityPubResponse(activityPubContextify(json), res) | 357 | return activityPubResponse(activityPubContextify(json), res) |
@@ -358,7 +359,7 @@ async function videoPlaylistElementController (req: express.Request, res: expres | |||
358 | 359 | ||
359 | // --------------------------------------------------------------------------- | 360 | // --------------------------------------------------------------------------- |
360 | 361 | ||
361 | async function actorFollowing (req: express.Request, actor: ActorModel) { | 362 | async function actorFollowing (req: express.Request, actor: MActorId) { |
362 | const handler = (start: number, count: number) => { | 363 | const handler = (start: number, count: number) => { |
363 | return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) | 364 | return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) |
364 | } | 365 | } |
@@ -366,7 +367,7 @@ async function actorFollowing (req: express.Request, actor: ActorModel) { | |||
366 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) | 367 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) |
367 | } | 368 | } |
368 | 369 | ||
369 | async function actorFollowers (req: express.Request, actor: ActorModel) { | 370 | async function actorFollowers (req: express.Request, actor: MActorId) { |
370 | const handler = (start: number, count: number) => { | 371 | const handler = (start: number, count: number) => { |
371 | return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count) | 372 | return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count) |
372 | } | 373 | } |
@@ -374,7 +375,7 @@ async function actorFollowers (req: express.Request, actor: ActorModel) { | |||
374 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) | 375 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) |
375 | } | 376 | } |
376 | 377 | ||
377 | async function actorPlaylists (req: express.Request, account: AccountModel) { | 378 | async function actorPlaylists (req: express.Request, account: MAccountId) { |
378 | const handler = (start: number, count: number) => { | 379 | const handler = (start: number, count: number) => { |
379 | return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count) | 380 | return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count) |
380 | } | 381 | } |
@@ -382,7 +383,7 @@ async function actorPlaylists (req: express.Request, account: AccountModel) { | |||
382 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) | 383 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) |
383 | } | 384 | } |
384 | 385 | ||
385 | function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) { | 386 | function videoRates (req: express.Request, rateType: VideoRateType, video: MVideo, url: string) { |
386 | const handler = async (start: number, count: number) => { | 387 | const handler = async (start: number, count: number) => { |
387 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) | 388 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) |
388 | return { | 389 | return { |
diff --git a/server/controllers/activitypub/inbox.ts b/server/controllers/activitypub/inbox.ts index 2d3eef222..d9df253aa 100644 --- a/server/controllers/activitypub/inbox.ts +++ b/server/controllers/activitypub/inbox.ts | |||
@@ -7,7 +7,7 @@ import { asyncMiddleware, checkSignature, localAccountValidator, localVideoChann | |||
7 | import { activityPubValidator } from '../../middlewares/validators/activitypub/activity' | 7 | import { activityPubValidator } from '../../middlewares/validators/activitypub/activity' |
8 | import { queue } from 'async' | 8 | import { queue } from 'async' |
9 | import { ActorModel } from '../../models/activitypub/actor' | 9 | import { ActorModel } from '../../models/activitypub/actor' |
10 | import { SignatureActorModel } from '../../typings/models' | 10 | import { MActorDefault, MActorSignature } from '../../typings/models' |
11 | 11 | ||
12 | const inboxRouter = express.Router() | 12 | const inboxRouter = express.Router() |
13 | 13 | ||
@@ -41,7 +41,8 @@ export { | |||
41 | 41 | ||
42 | // --------------------------------------------------------------------------- | 42 | // --------------------------------------------------------------------------- |
43 | 43 | ||
44 | const inboxQueue = queue<{ activities: Activity[], signatureActor?: SignatureActorModel, inboxActor?: ActorModel }, Error>((task, cb) => { | 44 | type QueueParam = { activities: Activity[], signatureActor?: MActorSignature, inboxActor?: MActorDefault } |
45 | const inboxQueue = queue<QueueParam, Error>((task, cb) => { | ||
45 | const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor } | 46 | const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor } |
46 | 47 | ||
47 | processActivities(task.activities, options) | 48 | processActivities(task.activities, options) |
diff --git a/server/controllers/activitypub/outbox.ts b/server/controllers/activitypub/outbox.ts index 38b6ec976..f3dd2ad7d 100644 --- a/server/controllers/activitypub/outbox.ts +++ b/server/controllers/activitypub/outbox.ts | |||
@@ -6,11 +6,9 @@ import { logger } from '../../helpers/logger' | |||
6 | import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send' | 6 | import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send' |
7 | import { buildAudience } from '../../lib/activitypub/audience' | 7 | import { buildAudience } from '../../lib/activitypub/audience' |
8 | import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares' | 8 | import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares' |
9 | import { AccountModel } from '../../models/account/account' | ||
10 | import { ActorModel } from '../../models/activitypub/actor' | ||
11 | import { VideoModel } from '../../models/video/video' | 9 | import { VideoModel } from '../../models/video/video' |
12 | import { activityPubResponse } from './utils' | 10 | import { activityPubResponse } from './utils' |
13 | import { VideoChannelModel } from '../../models/video/video-channel' | 11 | import { MActorLight } from '@server/typings/models' |
14 | 12 | ||
15 | const outboxRouter = express.Router() | 13 | const outboxRouter = express.Router() |
16 | 14 | ||
@@ -45,14 +43,10 @@ async function outboxController (req: express.Request, res: express.Response) { | |||
45 | return activityPubResponse(activityPubContextify(json), res) | 43 | return activityPubResponse(activityPubContextify(json), res) |
46 | } | 44 | } |
47 | 45 | ||
48 | async function buildActivities (actor: ActorModel, start: number, count: number) { | 46 | async function buildActivities (actor: MActorLight, start: number, count: number) { |
49 | const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count) | 47 | const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count) |
50 | const activities: Activity[] = [] | 48 | const activities: Activity[] = [] |
51 | 49 | ||
52 | // Avoid too many SQL requests | ||
53 | const actors = data.data.map(v => v.VideoChannel.Account.Actor) | ||
54 | actors.push(actor) | ||
55 | |||
56 | for (const video of data.data) { | 50 | for (const video of data.data) { |
57 | const byActor = video.VideoChannel.Account.Actor | 51 | const byActor = video.VideoChannel.Account.Actor |
58 | const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC) | 52 | const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC) |