]>
Commit | Line | Data |
---|---|---|
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 | 'sensitive': 'as:sensitive', | |
22 | 'language': 'http://schema.org/inLanguage', | |
23 | 'views': 'http://schema.org/Number', | |
24 | 'size': 'http://schema.org/Number', | |
25 | 'commentsEnabled': 'http://schema.org/Boolean', | |
26 | 'support': 'http://schema.org/Text' | |
27 | }, | |
28 | { | |
29 | likes: { | |
30 | '@id': 'as:likes', | |
31 | '@type': '@id' | |
32 | }, | |
33 | dislikes: { | |
34 | '@id': 'as:dislikes', | |
35 | '@type': '@id' | |
36 | }, | |
37 | shares: { | |
38 | '@id': 'as:shares', | |
39 | '@type': '@id' | |
40 | }, | |
41 | comments: { | |
42 | '@id': 'as:comments', | |
43 | '@type': '@id' | |
44 | } | |
45 | } | |
46 | ] | |
47 | }) | |
48 | } | |
49 | ||
50 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> | |
51 | async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { | |
52 | if (!page || !validator.isInt(page)) { | |
53 | // We just display the first page URL, we only need the total items | |
54 | const result = await handler(0, 1) | |
55 | ||
56 | return { | |
57 | id: url, | |
58 | type: 'OrderedCollection', | |
59 | totalItems: result.total, | |
60 | first: url + '?page=1' | |
61 | } | |
62 | } | |
63 | ||
64 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) | |
65 | const result = await handler(start, count) | |
66 | ||
67 | let next: string | |
68 | let prev: string | |
69 | ||
70 | // Assert page is a number | |
71 | page = parseInt(page, 10) | |
72 | ||
73 | // There are more results | |
74 | if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) { | |
75 | next = url + '?page=' + (page + 1) | |
76 | } | |
77 | ||
78 | if (page > 1) { | |
79 | prev = url + '?page=' + (page - 1) | |
80 | } | |
81 | ||
82 | return { | |
83 | id: url + '?page=' + page, | |
84 | type: 'OrderedCollectionPage', | |
85 | prev, | |
86 | next, | |
87 | partOf: url, | |
88 | orderedItems: result.data, | |
89 | totalItems: result.total | |
90 | } | |
91 | ||
92 | } | |
93 | ||
94 | function buildSignedActivity (byActor: ActorModel, data: Object) { | |
95 | const activity = activityPubContextify(data) | |
96 | ||
97 | return signObject(byActor, activity) as Promise<Activity> | |
98 | } | |
99 | ||
100 | function getActorUrl (activityActor: string | ActivityPubActor) { | |
101 | if (typeof activityActor === 'string') return activityActor | |
102 | ||
103 | return activityActor.id | |
104 | } | |
105 | ||
106 | // --------------------------------------------------------------------------- | |
107 | ||
108 | export { | |
109 | getActorUrl, | |
110 | activityPubContextify, | |
111 | activityPubCollectionPagination, | |
112 | buildSignedActivity | |
113 | } |