]>
Commit | Line | Data |
---|---|---|
1 | import { ResultList } from '../../shared/models' | |
2 | import { Activity, ActivityPubActor } from '../../shared/models/activitypub' | |
3 | import { ACTIVITY_PUB } from '../initializers' | |
4 | import { ActorModel } from '../models/activitypub/actor' | |
5 | import { signObject } from './peertube-crypto' | |
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 | 'RsaSignature2017': 'https://w3id.org/security#RsaSignature2017', | |
14 | 'Hashtag': 'as:Hashtag', | |
15 | 'uuid': 'http://schema.org/identifier', | |
16 | 'category': 'http://schema.org/category', | |
17 | 'licence': 'http://schema.org/license', | |
18 | 'sensitive': 'as:sensitive', | |
19 | 'language': 'http://schema.org/inLanguage', | |
20 | 'views': 'http://schema.org/Number', | |
21 | 'size': 'http://schema.org/Number', | |
22 | 'commentsEnabled': 'http://schema.org/Boolean', | |
23 | 'support': 'http://schema.org/Text' | |
24 | }, | |
25 | { | |
26 | likes: { | |
27 | '@id': 'as:likes', | |
28 | '@type': '@id' | |
29 | }, | |
30 | dislikes: { | |
31 | '@id': 'as:dislikes', | |
32 | '@type': '@id' | |
33 | }, | |
34 | shares: { | |
35 | '@id': 'as:shares', | |
36 | '@type': '@id' | |
37 | }, | |
38 | comments: { | |
39 | '@id': 'as:comments', | |
40 | '@type': '@id' | |
41 | } | |
42 | } | |
43 | ] | |
44 | }) | |
45 | } | |
46 | ||
47 | function activityPubCollection (url: string, results: any[]) { | |
48 | return { | |
49 | id: url, | |
50 | type: 'OrderedCollection', | |
51 | totalItems: results.length, | |
52 | orderedItems: results | |
53 | } | |
54 | } | |
55 | ||
56 | function activityPubCollectionPagination (url: string, page: any, result: ResultList<any>) { | |
57 | let next: string | |
58 | let prev: string | |
59 | ||
60 | // Assert page is a number | |
61 | page = parseInt(page, 10) | |
62 | ||
63 | // There are more results | |
64 | if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) { | |
65 | next = url + '?page=' + (page + 1) | |
66 | } | |
67 | ||
68 | if (page > 1) { | |
69 | prev = url + '?page=' + (page - 1) | |
70 | } | |
71 | ||
72 | const orderedCollectionPagination = { | |
73 | id: url + '?page=' + page, | |
74 | type: 'OrderedCollectionPage', | |
75 | prev, | |
76 | next, | |
77 | partOf: url, | |
78 | orderedItems: result.data | |
79 | } | |
80 | ||
81 | if (page === 1) { | |
82 | return activityPubContextify({ | |
83 | id: url, | |
84 | type: 'OrderedCollection', | |
85 | totalItems: result.total, | |
86 | first: orderedCollectionPagination | |
87 | }) | |
88 | } else { | |
89 | orderedCollectionPagination['totalItems'] = result.total | |
90 | } | |
91 | ||
92 | return orderedCollectionPagination | |
93 | } | |
94 | ||
95 | function buildSignedActivity (byActor: ActorModel, data: Object) { | |
96 | const activity = activityPubContextify(data) | |
97 | ||
98 | return signObject(byActor, activity) as Promise<Activity> | |
99 | } | |
100 | ||
101 | function getActorUrl (activityActor: string | ActivityPubActor) { | |
102 | if (typeof activityActor === 'string') return activityActor | |
103 | ||
104 | return activityActor.id | |
105 | } | |
106 | ||
107 | // --------------------------------------------------------------------------- | |
108 | ||
109 | export { | |
110 | getActorUrl, | |
111 | activityPubContextify, | |
112 | activityPubCollectionPagination, | |
113 | activityPubCollection, | |
114 | buildSignedActivity | |
115 | } |