]>
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' |
50d6de9c | 6 | import { announceActivityData, createActivityData } from '../../lib/activitypub/send' |
e251f170 | 7 | import { buildAudience } from '../../lib/activitypub/audience' |
e71bcc0f | 8 | import { asyncMiddleware, localAccountValidator } 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' |
e71bcc0f C |
13 | |
14 | const outboxRouter = express.Router() | |
15 | ||
e8e12200 | 16 | outboxRouter.get('/accounts/:name/outbox', |
e71bcc0f C |
17 | localAccountValidator, |
18 | asyncMiddleware(outboxController) | |
19 | ) | |
20 | ||
21 | // --------------------------------------------------------------------------- | |
22 | ||
23 | export { | |
24 | outboxRouter | |
25 | } | |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | ||
29 | async function outboxController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
3fd3ab2d | 30 | const account: AccountModel = res.locals.account |
50d6de9c | 31 | const actor = account.Actor |
8fffe21a C |
32 | const actorOutboxUrl = account.Actor.url + '/outbox' |
33 | ||
34 | logger.info('Receiving outbox request for %s.', actorOutboxUrl) | |
e71bcc0f | 35 | |
8fffe21a C |
36 | const handler = (start: number, count: number) => buildActivities(actor, start, count) |
37 | const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page) | |
38 | ||
39 | return activityPubResponse(activityPubContextify(json), res) | |
40 | } | |
e71bcc0f | 41 | |
8fffe21a | 42 | async function buildActivities (actor: ActorModel, start: number, count: number) { |
50d6de9c | 43 | const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count) |
e71bcc0f C |
44 | const activities: Activity[] = [] |
45 | ||
54e74059 C |
46 | // Avoid too many SQL requests |
47 | const actors = data.data.map(v => v.VideoChannel.Account.Actor) | |
48 | actors.push(actor) | |
49 | ||
e71bcc0f | 50 | for (const video of data.data) { |
54e74059 | 51 | const byActor = video.VideoChannel.Account.Actor |
e3d5ea4f | 52 | const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC) |
54e74059 | 53 | |
50d6de9c | 54 | // This is a shared video |
40ff5707 | 55 | if (video.VideoShares !== undefined && video.VideoShares.length !== 0) { |
4ba3b8ea | 56 | const videoShare = video.VideoShares[0] |
e3d5ea4f | 57 | const announceActivity = await announceActivityData(videoShare.url, actor, video.url, undefined, createActivityAudience) |
40ff5707 | 58 | |
e71bcc0f C |
59 | activities.push(announceActivity) |
60 | } else { | |
7acee6f1 | 61 | const videoObject = video.toActivityPubObject() |
54e74059 | 62 | const createActivity = await createActivityData(video.url, byActor, videoObject, undefined, createActivityAudience) |
40ff5707 | 63 | |
50d6de9c | 64 | activities.push(createActivity) |
e71bcc0f C |
65 | } |
66 | } | |
67 | ||
8fffe21a | 68 | return { |
e71bcc0f C |
69 | data: activities, |
70 | total: data.total | |
71 | } | |
e71bcc0f | 72 | } |