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