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