]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
d710f5c9766ffb71f660470a3755937e385d7c70
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
1 import * as Bluebird from 'bluebird'
2 import * as validator from 'validator'
3 import { ResultList } from '../../shared/models'
4 import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
5 import { ACTIVITY_PUB } from '../initializers'
6 import { ActorModel } from '../models/activitypub/actor'
7 import { signObject } from './peertube-crypto'
8 import { pageToStartAndCount } from './core-utils'
9
10 function activityPubContextify <T> (data: T) {
11 return Object.assign(data, {
12 '@context': [
13 'https://www.w3.org/ns/activitystreams',
14 'https://w3id.org/security/v1',
15 {
16 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
17 Hashtag: 'as:Hashtag',
18 uuid: 'http://schema.org/identifier',
19 category: 'http://schema.org/category',
20 licence: 'http://schema.org/license',
21 subtitleLanguage: 'http://schema.org/subtitleLanguage',
22 sensitive: 'as:sensitive',
23 language: 'http://schema.org/inLanguage',
24 views: 'http://schema.org/Number',
25 stats: 'http://schema.org/Number',
26 size: 'http://schema.org/Number',
27 commentsEnabled: 'http://schema.org/Boolean',
28 waitTranscoding: 'http://schema.org/Boolean',
29 support: 'http://schema.org/Text'
30 },
31 {
32 likes: {
33 '@id': 'as:likes',
34 '@type': '@id'
35 },
36 dislikes: {
37 '@id': 'as:dislikes',
38 '@type': '@id'
39 },
40 shares: {
41 '@id': 'as:shares',
42 '@type': '@id'
43 },
44 comments: {
45 '@id': 'as:comments',
46 '@type': '@id'
47 }
48 }
49 ]
50 })
51 }
52
53 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
54 async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
55 if (!page || !validator.isInt(page)) {
56 // We just display the first page URL, we only need the total items
57 const result = await handler(0, 1)
58
59 return {
60 id: url,
61 type: 'OrderedCollection',
62 totalItems: result.total,
63 first: url + '?page=1'
64 }
65 }
66
67 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
68 const result = await handler(start, count)
69
70 let next: string | undefined
71 let prev: string | undefined
72
73 // Assert page is a number
74 page = parseInt(page, 10)
75
76 // There are more results
77 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
78 next = url + '?page=' + (page + 1)
79 }
80
81 if (page > 1) {
82 prev = url + '?page=' + (page - 1)
83 }
84
85 return {
86 id: url + '?page=' + page,
87 type: 'OrderedCollectionPage',
88 prev,
89 next,
90 partOf: url,
91 orderedItems: result.data,
92 totalItems: result.total
93 }
94
95 }
96
97 function buildSignedActivity (byActor: ActorModel, data: Object) {
98 const activity = activityPubContextify(data)
99
100 return signObject(byActor, activity) as Promise<Activity>
101 }
102
103 function getActorUrl (activityActor: string | ActivityPubActor) {
104 if (typeof activityActor === 'string') return activityActor
105
106 return activityActor.id
107 }
108
109 // ---------------------------------------------------------------------------
110
111 export {
112 getActorUrl,
113 activityPubContextify,
114 activityPubCollectionPagination,
115 buildSignedActivity
116 }