2
1

outbox.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import express from 'express'
  2. import { activityPubCollectionPagination } from '@server/lib/activitypub/collection'
  3. import { activityPubContextify } from '@server/lib/activitypub/context'
  4. import { MActorLight } from '@server/types/models'
  5. import { Activity } from '../../../shared/models/activitypub/activity'
  6. import { VideoPrivacy } from '../../../shared/models/videos'
  7. import { logger } from '../../helpers/logger'
  8. import { buildAudience } from '../../lib/activitypub/audience'
  9. import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send'
  10. import { asyncMiddleware, ensureIsLocalChannel, localAccountValidator, videoChannelsNameWithHostValidator } from '../../middlewares'
  11. import { apPaginationValidator } from '../../middlewares/validators/activitypub'
  12. import { VideoModel } from '../../models/video/video'
  13. import { activityPubResponse } from './utils'
  14. const outboxRouter = express.Router()
  15. outboxRouter.get('/accounts/:name/outbox',
  16. apPaginationValidator,
  17. localAccountValidator,
  18. asyncMiddleware(outboxController)
  19. )
  20. outboxRouter.get('/video-channels/:nameWithHost/outbox',
  21. apPaginationValidator,
  22. asyncMiddleware(videoChannelsNameWithHostValidator),
  23. ensureIsLocalChannel,
  24. asyncMiddleware(outboxController)
  25. )
  26. // ---------------------------------------------------------------------------
  27. export {
  28. outboxRouter
  29. }
  30. // ---------------------------------------------------------------------------
  31. async function outboxController (req: express.Request, res: express.Response) {
  32. const accountOrVideoChannel = res.locals.account || res.locals.videoChannel
  33. const actor = accountOrVideoChannel.Actor
  34. const actorOutboxUrl = actor.url + '/outbox'
  35. logger.info('Receiving outbox request for %s.', actorOutboxUrl)
  36. const handler = (start: number, count: number) => buildActivities(actor, start, count)
  37. const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page, req.query.size)
  38. return activityPubResponse(activityPubContextify(json, 'Collection'), res)
  39. }
  40. async function buildActivities (actor: MActorLight, start: number, count: number) {
  41. const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
  42. const activities: Activity[] = []
  43. for (const video of data.data) {
  44. const byActor = video.VideoChannel.Account.Actor
  45. const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
  46. // This is a shared video
  47. if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
  48. const videoShare = video.VideoShares[0]
  49. const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience)
  50. activities.push(announceActivity)
  51. } else {
  52. const videoObject = video.toActivityPubObject()
  53. const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
  54. activities.push(createActivity)
  55. }
  56. }
  57. return {
  58. data: activities,
  59. total: data.total
  60. }
  61. }