]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/activitypub.ts
Misc cleanup
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
index 04d85b8e619764758676b96e1de5fe472bbe6fde..1ea6422ca111f441dccc17c22543e84d7bf1248a 100644 (file)
@@ -24,12 +24,23 @@ function activityPubContextify <T> (data: T) {
   })
 }
 
-function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
+function activityPubCollection (results: any[]) {
+  return {
+    type: 'OrderedCollection',
+    totalItems: results.length,
+    orderedItems: results
+  }
+}
+
+function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
   let next: string
   let prev: string
 
+  // Assert page is a number
+  page = parseInt(page, 10)
+
   // There are more results
-  if (result.total > ((page + 1) * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)) {
+  if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
     next = url + '?page=' + (page + 1)
   }
 
@@ -46,14 +57,18 @@ function activityPubCollectionPagination (url: string, page: number, result: Res
     orderedItems: result.data
   }
 
-  const obj = {
-    id: url,
-    type: 'OrderedCollection',
-    totalItems: result.total,
-    orderedItems: orderedCollectionPagination
+  if (page === 1) {
+    return activityPubContextify({
+      id: url,
+      type: 'OrderedCollection',
+      totalItems: result.total,
+      first: orderedCollectionPagination
+    })
+  } else {
+    orderedCollectionPagination['totalItems'] = result.total
   }
 
-  return activityPubContextify(obj)
+  return orderedCollectionPagination
 }
 
 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
@@ -67,5 +82,6 @@ function buildSignedActivity (byAccount: AccountInstance, data: Object) {
 export {
   activityPubContextify,
   activityPubCollectionPagination,
+  activityPubCollection,
   buildSignedActivity
 }