aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/activitypub/outbox.ts
blob: e2c4e9b1e67fd6342cd48489deff42fb2554332f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as express from 'express'
import { Activity } from '../../../shared/models/activitypub/activity'
import { activityPubCollectionPagination } from '../../helpers/activitypub'
import { pageToStartAndCount } from '../../helpers/core-utils'
import { database as db } from '../../initializers'
import { ACTIVITY_PUB } from '../../initializers/constants'
import { addActivityData } from '../../lib/activitypub/send/send-add'
import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url'
import { announceActivityData } from '../../lib/index'
import { asyncMiddleware, localAccountValidator } from '../../middlewares'
import { AccountInstance } from '../../models/account/account-interface'

const outboxRouter = express.Router()

outboxRouter.get('/account/:name/outbox',
  localAccountValidator,
  asyncMiddleware(outboxController)
)

// ---------------------------------------------------------------------------

export {
  outboxRouter
}

// ---------------------------------------------------------------------------

async function outboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
  const account: AccountInstance = res.locals.account

  const page = req.query.page || 1
  const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)

  const data = await db.Video.listAllAndSharedByAccountForOutbox(account.id, start, count)
  const activities: Activity[] = []

  for (const video of data.data) {
    const videoObject = video.toActivityPubObject()

    // This is a shared video
    if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
      const addActivity = await addActivityData(video.url, video.VideoChannel.Account, video, video.VideoChannel.url, videoObject)

      const url = getAnnounceActivityPubUrl(video.url, account)
      const announceActivity = await announceActivityData(url, account, addActivity)

      activities.push(announceActivity)
    } else {
      const addActivity = await addActivityData(video.url, account, video, video.VideoChannel.url, videoObject)

      activities.push(addActivity)
    }
  }

  const newResult = {
    data: activities,
    total: data.total
  }
  const json = activityPubCollectionPagination(account.url + '/outbox', page, newResult)

  return res.json(json).end()
}