aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/activitypub.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-05-25 16:21:16 +0200
committerChocobozzz <me@florianbigard.com>2018-05-25 16:21:16 +0200
commit8fffe21a7bc96d08b229293d66ddba576e609790 (patch)
tree5ebd5f5198a59084c5338ce197d7e836b39200a4 /server/helpers/activitypub.ts
parente251f170b00b2014ac4e823113c6ff40e3fb1471 (diff)
downloadPeerTube-8fffe21a7bc96d08b229293d66ddba576e609790.tar.gz
PeerTube-8fffe21a7bc96d08b229293d66ddba576e609790.tar.zst
PeerTube-8fffe21a7bc96d08b229293d66ddba576e609790.zip
Refractor and optimize AP collections
Only display urls in general object, and paginate video comments, shares, likes and dislikes
Diffstat (limited to 'server/helpers/activitypub.ts')
-rw-r--r--server/helpers/activitypub.ts44
1 files changed, 21 insertions, 23 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts
index 1934fa0f0..d1f3ec02d 100644
--- a/server/helpers/activitypub.ts
+++ b/server/helpers/activitypub.ts
@@ -1,8 +1,11 @@
1import * as Bluebird from 'bluebird'
2import * as validator from 'validator'
1import { ResultList } from '../../shared/models' 3import { ResultList } from '../../shared/models'
2import { Activity, ActivityPubActor } from '../../shared/models/activitypub' 4import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
3import { ACTIVITY_PUB } from '../initializers' 5import { ACTIVITY_PUB } from '../initializers'
4import { ActorModel } from '../models/activitypub/actor' 6import { ActorModel } from '../models/activitypub/actor'
5import { signObject } from './peertube-crypto' 7import { signObject } from './peertube-crypto'
8import { pageToStartAndCount } from './core-utils'
6 9
7function activityPubContextify <T> (data: T) { 10function activityPubContextify <T> (data: T) {
8 return Object.assign(data,{ 11 return Object.assign(data,{
@@ -44,16 +47,23 @@ function activityPubContextify <T> (data: T) {
44 }) 47 })
45} 48}
46 49
47function activityPubCollection (url: string, results: any[]) { 50type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
48 return { 51async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
49 id: url, 52 if (!page || !validator.isInt(page)) {
50 type: 'OrderedCollection', 53 // We just display the first page URL, we only need the total items
51 totalItems: results.length, 54 const result = await handler(0, 1)
52 orderedItems: results 55
56 return {
57 id: url,
58 type: 'OrderedCollection',
59 totalItems: result.total,
60 first: url + '?page=1'
61 }
53 } 62 }
54}
55 63
56function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) { 64 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
65 const result = await handler(start, count)
66
57 let next: string 67 let next: string
58 let prev: string 68 let prev: string
59 69
@@ -69,27 +79,16 @@ function activityPubCollectionPagination (url: string, page: any, result: Result
69 prev = url + '?page=' + (page - 1) 79 prev = url + '?page=' + (page - 1)
70 } 80 }
71 81
72 const orderedCollectionPagination = { 82 return {
73 id: url + '?page=' + page, 83 id: url + '?page=' + page,
74 type: 'OrderedCollectionPage', 84 type: 'OrderedCollectionPage',
75 prev, 85 prev,
76 next, 86 next,
77 partOf: url, 87 partOf: url,
78 orderedItems: result.data 88 orderedItems: result.data,
79 } 89 totalItems: result.total
80
81 if (page === 1) {
82 return activityPubContextify({
83 id: url,
84 type: 'OrderedCollection',
85 totalItems: result.total,
86 first: orderedCollectionPagination
87 })
88 } else {
89 orderedCollectionPagination['totalItems'] = result.total
90 } 90 }
91 91
92 return orderedCollectionPagination
93} 92}
94 93
95function buildSignedActivity (byActor: ActorModel, data: Object) { 94function buildSignedActivity (byActor: ActorModel, data: Object) {
@@ -110,6 +109,5 @@ export {
110 getActorUrl, 109 getActorUrl,
111 activityPubContextify, 110 activityPubContextify,
112 activityPubCollectionPagination, 111 activityPubCollectionPagination,
113 activityPubCollection,
114 buildSignedActivity 112 buildSignedActivity
115} 113}