]>
Commit | Line | Data |
---|---|---|
e71bcc0f | 1 | import * as express from 'express' |
d52eb8f6 | 2 | import { Activity } from '../../../shared/models/activitypub/activity' |
0bc22f8d | 3 | import { VideoPrivacy } from '../../../shared/models/videos' |
8fffe21a | 4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' |
0a8ae684 | 5 | import { logger } from '../../helpers/logger' |
c48e82b5 | 6 | import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send' |
e251f170 | 7 | import { buildAudience } from '../../lib/activitypub/audience' |
06a05d5f | 8 | import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares' |
3fd3ab2d | 9 | import { AccountModel } from '../../models/account/account' |
54e74059 | 10 | import { ActorModel } from '../../models/activitypub/actor' |
3fd3ab2d | 11 | import { VideoModel } from '../../models/video/video' |
8fffe21a | 12 | import { activityPubResponse } from './utils' |
06a05d5f | 13 | import { VideoChannelModel } from '../../models/video/video-channel' |
e71bcc0f C |
14 | |
15 | const outboxRouter = express.Router() | |
16 | ||
e8e12200 | 17 | outboxRouter.get('/accounts/:name/outbox', |
e71bcc0f C |
18 | localAccountValidator, |
19 | asyncMiddleware(outboxController) | |
20 | ) | |
21 | ||
06a05d5f C |
22 | outboxRouter.get('/video-channels/:name/outbox', |
23 | localVideoChannelValidator, | |
24 | asyncMiddleware(outboxController) | |
25 | ) | |
26 | ||
e71bcc0f C |
27 | // --------------------------------------------------------------------------- |
28 | ||
29 | export { | |
30 | outboxRouter | |
31 | } | |
32 | ||
33 | // --------------------------------------------------------------------------- | |
34 | ||
418d092a | 35 | async function outboxController (req: express.Request, res: express.Response) { |
dae86118 | 36 | const accountOrVideoChannel = res.locals.account || res.locals.videoChannel |
06a05d5f C |
37 | const actor = accountOrVideoChannel.Actor |
38 | const actorOutboxUrl = actor.url + '/outbox' | |
8fffe21a C |
39 | |
40 | logger.info('Receiving outbox request for %s.', actorOutboxUrl) | |
e71bcc0f | 41 | |
8fffe21a C |
42 | const handler = (start: number, count: number) => buildActivities(actor, start, count) |
43 | const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page) | |
44 | ||
45 | return activityPubResponse(activityPubContextify(json), res) | |
46 | } | |
e71bcc0f | 47 | |
8fffe21a | 48 | async function buildActivities (actor: ActorModel, start: number, count: number) { |
50d6de9c | 49 | const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count) |
e71bcc0f C |
50 | const activities: Activity[] = [] |
51 | ||
54e74059 C |
52 | // Avoid too many SQL requests |
53 | const actors = data.data.map(v => v.VideoChannel.Account.Actor) | |
54 | actors.push(actor) | |
55 | ||
e71bcc0f | 56 | for (const video of data.data) { |
54e74059 | 57 | const byActor = video.VideoChannel.Account.Actor |
e3d5ea4f | 58 | const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC) |
54e74059 | 59 | |
50d6de9c | 60 | // This is a shared video |
40ff5707 | 61 | if (video.VideoShares !== undefined && video.VideoShares.length !== 0) { |
4ba3b8ea | 62 | const videoShare = video.VideoShares[0] |
c48e82b5 | 63 | const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience) |
40ff5707 | 64 | |
e71bcc0f C |
65 | activities.push(announceActivity) |
66 | } else { | |
7acee6f1 | 67 | const videoObject = video.toActivityPubObject() |
c48e82b5 | 68 | const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience) |
40ff5707 | 69 | |
50d6de9c | 70 | activities.push(createActivity) |
e71bcc0f C |
71 | } |
72 | } | |
73 | ||
8fffe21a | 74 | return { |
e71bcc0f C |
75 | data: activities, |
76 | total: data.total | |
77 | } | |
e71bcc0f | 78 | } |