Fix lint
[oweals/peertube.git] / server / helpers / activitypub.ts
1 import { Activity } from '../../shared/models/activitypub/activity'
2 import { ResultList } from '../../shared/models/result-list.model'
3 import { AccountInstance } from '../models/account/account-interface'
4 import { signObject } from './peertube-crypto'
5 import { ACTIVITY_PUB } from '../initializers/constants'
6
7 function activityPubContextify <T> (data: T) {
8   return Object.assign(data,{
9     '@context': [
10       'https://www.w3.org/ns/activitystreams',
11       'https://w3id.org/security/v1',
12       {
13         'Hashtag': 'as:Hashtag',
14         'uuid': 'http://schema.org/identifier',
15         'category': 'http://schema.org/category',
16         'licence': 'http://schema.org/license',
17         'nsfw': 'as:sensitive',
18         'language': 'http://schema.org/inLanguage',
19         'views': 'http://schema.org/Number',
20         'size': 'http://schema.org/Number',
21         'VideoChannel': 'https://peertu.be/ns/VideoChannel'
22       }
23     ]
24   })
25 }
26
27 function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
28   let next: string
29   let prev: string
30
31   // Assert page is a number
32   page = parseInt(page, 10)
33
34   // There are more results
35   if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
36     next = url + '?page=' + (page + 1)
37   }
38
39   if (page > 1) {
40     prev = url + '?page=' + (page - 1)
41   }
42
43   const orderedCollectionPagination = {
44     id: url + '?page=' + page,
45     type: 'OrderedCollectionPage',
46     prev,
47     next,
48     partOf: url,
49     orderedItems: result.data
50   }
51
52   if (page === 1) {
53     return activityPubContextify({
54       id: url,
55       type: 'OrderedCollection',
56       totalItems: result.total,
57       first: orderedCollectionPagination
58     })
59   } else {
60     orderedCollectionPagination['totalItems'] = result.total
61   }
62
63   return orderedCollectionPagination
64 }
65
66 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
67   const activity = activityPubContextify(data)
68
69   return signObject(byAccount, activity) as Promise<Activity>
70 }
71
72 // ---------------------------------------------------------------------------
73
74 export {
75   activityPubContextify,
76   activityPubCollectionPagination,
77   buildSignedActivity
78 }