]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/activitypub.ts
Misc cleanup
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
index 5c577bb612a559a3bf89bb8f8e4a32e639bc757e..1ea6422ca111f441dccc17c22543e84d7bf1248a 100644 (file)
@@ -2,6 +2,7 @@ import { Activity } from '../../shared/models/activitypub/activity'
 import { ResultList } from '../../shared/models/result-list.model'
 import { AccountInstance } from '../models/account/account-interface'
 import { signObject } from './peertube-crypto'
+import { ACTIVITY_PUB } from '../initializers/constants'
 
 function activityPubContextify <T> (data: T) {
   return Object.assign(data,{
@@ -23,24 +24,51 @@ function activityPubContextify <T> (data: T) {
   })
 }
 
-function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
-  const baseUrl = url.split('?').shift
+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 * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
+    next = url + '?page=' + (page + 1)
+  }
+
+  if (page > 1) {
+    prev = url + '?page=' + (page - 1)
+  }
+
+  const orderedCollectionPagination = {
+    id: url + '?page=' + page,
+    type: 'OrderedCollectionPage',
+    prev,
+    next,
+    partOf: url,
+    orderedItems: result.data
+  }
 
-  const obj = {
-    id: baseUrl,
-    type: 'Collection',
-    totalItems: result.total,
-    first: {
-      id: baseUrl + '?page=' + page,
-      type: 'CollectionPage',
+  if (page === 1) {
+    return activityPubContextify({
+      id: url,
+      type: 'OrderedCollection',
       totalItems: result.total,
-      next: baseUrl + '?page=' + (page + 1),
-      partOf: baseUrl,
-      items: result.data
-    }
+      first: orderedCollectionPagination
+    })
+  } else {
+    orderedCollectionPagination['totalItems'] = result.total
   }
 
-  return activityPubContextify(obj)
+  return orderedCollectionPagination
 }
 
 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
@@ -54,5 +82,6 @@ function buildSignedActivity (byAccount: AccountInstance, data: Object) {
 export {
   activityPubContextify,
   activityPubCollectionPagination,
+  activityPubCollection,
   buildSignedActivity
 }