]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Fetch video likes/dislikes too
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
CommitLineData
afffe988 1import { Activity } from '../../shared/models/activitypub/activity'
e4f97bab 2import { ResultList } from '../../shared/models/result-list.model'
20494f12 3import { AccountInstance } from '../models/account/account-interface'
afffe988 4import { signObject } from './peertube-crypto'
e71bcc0f 5import { ACTIVITY_PUB } from '../initializers/constants'
571389d4
C
6
7function activityPubContextify <T> (data: T) {
e4f97bab
C
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',
8e13fa7d
C
20 'size': 'http://schema.org/Number',
21 'VideoChannel': 'https://peertu.be/ns/VideoChannel'
e4f97bab
C
22 }
23 ]
24 })
25}
26
16b90975
C
27function activityPubCollection (results: any[]) {
28 return {
29 type: 'OrderedCollection',
30 totalItems: results.length,
31 orderedItems: results
32 }
33}
34
c46edbc2 35function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) {
e71bcc0f
C
36 let next: string
37 let prev: string
38
c46edbc2
C
39 // Assert page is a number
40 page = parseInt(page, 10)
41
e71bcc0f 42 // There are more results
c46edbc2 43 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
e71bcc0f
C
44 next = url + '?page=' + (page + 1)
45 }
46
47 if (page > 1) {
48 prev = url + '?page=' + (page - 1)
49 }
50
51 const orderedCollectionPagination = {
52 id: url + '?page=' + page,
53 type: 'OrderedCollectionPage',
54 prev,
55 next,
56 partOf: url,
57 orderedItems: result.data
58 }
e4f97bab 59
c986175d
C
60 if (page === 1) {
61 return activityPubContextify({
62 id: url,
63 type: 'OrderedCollection',
64 totalItems: result.total,
65 first: orderedCollectionPagination
66 })
c46edbc2
C
67 } else {
68 orderedCollectionPagination['totalItems'] = result.total
e4f97bab
C
69 }
70
c986175d 71 return orderedCollectionPagination
e4f97bab
C
72}
73
afffe988
C
74function buildSignedActivity (byAccount: AccountInstance, data: Object) {
75 const activity = activityPubContextify(data)
76
77 return signObject(byAccount, activity) as Promise<Activity>
78}
79
e4f97bab
C
80// ---------------------------------------------------------------------------
81
82export {
e4f97bab 83 activityPubContextify,
0d0e8dd0 84 activityPubCollectionPagination,
16b90975 85 activityPubCollection,
892211e8 86 buildSignedActivity
e4f97bab 87}