]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Fetch outbox to grab old activities
[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
27function activityPubCollectionPagination (url: string, page: number, result: ResultList<any>) {
e71bcc0f
C
28 let next: string
29 let prev: string
30
31 // There are more results
32 if (result.total > ((page + 1) * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)) {
33 next = url + '?page=' + (page + 1)
34 }
35
36 if (page > 1) {
37 prev = url + '?page=' + (page - 1)
38 }
39
40 const orderedCollectionPagination = {
41 id: url + '?page=' + page,
42 type: 'OrderedCollectionPage',
43 prev,
44 next,
45 partOf: url,
46 orderedItems: result.data
47 }
e4f97bab 48
c986175d
C
49 if (page === 1) {
50 return activityPubContextify({
51 id: url,
52 type: 'OrderedCollection',
53 totalItems: result.total,
54 first: orderedCollectionPagination
55 })
e4f97bab
C
56 }
57
c986175d 58 return orderedCollectionPagination
e4f97bab
C
59}
60
afffe988
C
61function buildSignedActivity (byAccount: AccountInstance, data: Object) {
62 const activity = activityPubContextify(data)
63
64 return signObject(byAccount, activity) as Promise<Activity>
65}
66
e4f97bab
C
67// ---------------------------------------------------------------------------
68
69export {
e4f97bab 70 activityPubContextify,
0d0e8dd0 71 activityPubCollectionPagination,
892211e8 72 buildSignedActivity
e4f97bab 73}