]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
Fix lint
[github/Chocobozzz/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
6 function activityPubContextify <T> (data: T) {
7 return Object.assign(data,{
8 '@context': [
9 'https://www.w3.org/ns/activitystreams',
10 'https://w3id.org/security/v1',
11 {
12 'Hashtag': 'as:Hashtag',
13 'uuid': 'http://schema.org/identifier',
14 'category': 'http://schema.org/category',
15 'licence': 'http://schema.org/license',
16 'nsfw': 'as:sensitive',
17 'language': 'http://schema.org/inLanguage',
18 'views': 'http://schema.org/Number',
19 'size': 'http://schema.org/Number',
20 'VideoChannel': 'https://peertu.be/ns/VideoChannel'
21 }
22 ]
23 })
24 }
25
26 function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
27 const baseUrl = url.split('?').shift
28
29 const obj = {
30 id: baseUrl,
31 type: 'Collection',
32 totalItems: result.total,
33 first: {
34 id: baseUrl + '?page=' + page,
35 type: 'CollectionPage',
36 totalItems: result.total,
37 next: baseUrl + '?page=' + (page + 1),
38 partOf: baseUrl,
39 items: result.data
40 }
41 }
42
43 return activityPubContextify(obj)
44 }
45
46 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
47 const activity = activityPubContextify(data)
48
49 return signObject(byAccount, activity) as Promise<Activity>
50 }
51
52 // ---------------------------------------------------------------------------
53
54 export {
55 activityPubContextify,
56 activityPubCollectionPagination,
57 buildSignedActivity
58 }