]>
Commit | Line | Data |
---|---|---|
e71bcc0f | 1 | import * as express from 'express' |
d52eb8f6 | 2 | import { Activity } from '../../../shared/models/activitypub/activity' |
40ff5707 C |
3 | import { activityPubCollectionPagination } from '../../helpers/activitypub' |
4 | import { pageToStartAndCount } from '../../helpers/core-utils' | |
40ff5707 | 5 | import { ACTIVITY_PUB } from '../../initializers/constants' |
50d6de9c | 6 | import { announceActivityData, createActivityData } from '../../lib/activitypub/send' |
54e74059 | 7 | import { buildAudience } from '../../lib/activitypub/send/misc' |
e71bcc0f | 8 | import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url' |
e71bcc0f | 9 | import { asyncMiddleware, localAccountValidator } from '../../middlewares' |
3fd3ab2d | 10 | import { AccountModel } from '../../models/account/account' |
54e74059 | 11 | import { ActorModel } from '../../models/activitypub/actor' |
3fd3ab2d | 12 | import { VideoModel } from '../../models/video/video' |
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 |
e71bcc0f | 32 | |
c46edbc2 | 33 | const page = req.query.page || 1 |
e71bcc0f C |
34 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) |
35 | ||
50d6de9c | 36 | const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count) |
e71bcc0f C |
37 | const activities: Activity[] = [] |
38 | ||
54e74059 C |
39 | // Avoid too many SQL requests |
40 | const actors = data.data.map(v => v.VideoChannel.Account.Actor) | |
41 | actors.push(actor) | |
42 | ||
43 | const followersMatrix = await ActorModel.getActorsFollowerSharedInboxUrls(actors, undefined) | |
44 | ||
e71bcc0f C |
45 | for (const video of data.data) { |
46 | const videoObject = video.toActivityPubObject() | |
e71bcc0f | 47 | |
54e74059 C |
48 | const byActor = video.VideoChannel.Account.Actor |
49 | const createActivityAudience = buildAudience(followersMatrix[byActor.id]) | |
50 | ||
50d6de9c | 51 | // This is a shared video |
40ff5707 | 52 | if (video.VideoShares !== undefined && video.VideoShares.length !== 0) { |
54e74059 | 53 | const createActivity = await createActivityData(video.url, byActor, videoObject, undefined, createActivityAudience) |
40ff5707 | 54 | |
54e74059 | 55 | const announceAudience = buildAudience(followersMatrix[actor.id]) |
50d6de9c | 56 | const url = getAnnounceActivityPubUrl(video.url, actor) |
54e74059 | 57 | const announceActivity = await announceActivityData(url, actor, createActivity, undefined, announceAudience) |
40ff5707 | 58 | |
e71bcc0f C |
59 | activities.push(announceActivity) |
60 | } else { | |
54e74059 | 61 | const createActivity = await createActivityData(video.url, byActor, videoObject, undefined, createActivityAudience) |
40ff5707 | 62 | |
50d6de9c | 63 | activities.push(createActivity) |
e71bcc0f C |
64 | } |
65 | } | |
66 | ||
67 | const newResult = { | |
68 | data: activities, | |
69 | total: data.total | |
70 | } | |
50d6de9c | 71 | const json = activityPubCollectionPagination(account.Actor.url + '/outbox', page, newResult) |
e71bcc0f C |
72 | |
73 | return res.json(json).end() | |
74 | } |