diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/activitypub.ts | 44 |
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 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import * as validator from 'validator' | ||
1 | import { ResultList } from '../../shared/models' | 3 | import { ResultList } from '../../shared/models' |
2 | import { Activity, ActivityPubActor } from '../../shared/models/activitypub' | 4 | import { Activity, ActivityPubActor } from '../../shared/models/activitypub' |
3 | import { ACTIVITY_PUB } from '../initializers' | 5 | import { ACTIVITY_PUB } from '../initializers' |
4 | import { ActorModel } from '../models/activitypub/actor' | 6 | import { ActorModel } from '../models/activitypub/actor' |
5 | import { signObject } from './peertube-crypto' | 7 | import { signObject } from './peertube-crypto' |
8 | import { pageToStartAndCount } from './core-utils' | ||
6 | 9 | ||
7 | function activityPubContextify <T> (data: T) { | 10 | function 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 | ||
47 | function activityPubCollection (url: string, results: any[]) { | 50 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> |
48 | return { | 51 | async 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 | ||
56 | function 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 | ||
95 | function buildSignedActivity (byActor: ActorModel, data: Object) { | 94 | function 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 | } |