1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 10:49:28 +02:00

Refractor and optimize AP collections

Only display urls in general object, and paginate video comments, shares, likes and
dislikes
This commit is contained in:
Chocobozzz 2018-05-25 16:21:16 +02:00
parent e251f170b0
commit 8fffe21a7b
No known key found for this signature in database
GPG key ID: 583A612D890159BE
14 changed files with 215 additions and 260 deletions

View file

@ -1,8 +1,11 @@
import * as Bluebird from 'bluebird'
import * as validator from 'validator'
import { ResultList } from '../../shared/models'
import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
import { ACTIVITY_PUB } from '../initializers'
import { ActorModel } from '../models/activitypub/actor'
import { signObject } from './peertube-crypto'
import { pageToStartAndCount } from './core-utils'
function activityPubContextify <T> (data: T) {
return Object.assign(data,{
@ -44,16 +47,23 @@ function activityPubContextify <T> (data: T) {
})
}
function activityPubCollection (url: string, results: any[]) {
return {
id: url,
type: 'OrderedCollection',
totalItems: results.length,
orderedItems: results
}
}
type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
if (!page || !validator.isInt(page)) {
// We just display the first page URL, we only need the total items
const result = await handler(0, 1)
return {
id: url,
type: 'OrderedCollection',
totalItems: result.total,
first: url + '?page=1'
}
}
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
const result = await handler(start, count)
function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
let next: string
let prev: string
@ -69,27 +79,16 @@ function activityPubCollectionPagination (url: string, page: any, result: Result
prev = url + '?page=' + (page - 1)
}
const orderedCollectionPagination = {
return {
id: url + '?page=' + page,
type: 'OrderedCollectionPage',
prev,
next,
partOf: url,
orderedItems: result.data
orderedItems: result.data,
totalItems: result.total
}
if (page === 1) {
return activityPubContextify({
id: url,
type: 'OrderedCollection',
totalItems: result.total,
first: orderedCollectionPagination
})
} else {
orderedCollectionPagination['totalItems'] = result.total
}
return orderedCollectionPagination
}
function buildSignedActivity (byActor: ActorModel, data: Object) {
@ -110,6 +109,5 @@ export {
getActorUrl,
activityPubContextify,
activityPubCollectionPagination,
activityPubCollection,
buildSignedActivity
}