]>
Commit | Line | Data |
---|---|---|
1 | import * as Bluebird from 'bluebird' | |
2 | import validator from 'validator' | |
3 | import { ResultList } from '../../shared/models' | |
4 | import { Activity } from '../../shared/models/activitypub' | |
5 | import { ACTIVITY_PUB } from '../initializers/constants' | |
6 | import { signJsonLDObject } from './peertube-crypto' | |
7 | import { pageToStartAndCount } from './core-utils' | |
8 | import { parse } from 'url' | |
9 | import { MActor } from '../typings/models' | |
10 | ||
11 | function activityPubContextify <T> (data: T) { | |
12 | return Object.assign(data, { | |
13 | '@context': [ | |
14 | 'https://www.w3.org/ns/activitystreams', | |
15 | 'https://w3id.org/security/v1', | |
16 | { | |
17 | RsaSignature2017: 'https://w3id.org/security#RsaSignature2017', | |
18 | pt: 'https://joinpeertube.org/ns#', | |
19 | sc: 'http://schema.org#', | |
20 | Hashtag: 'as:Hashtag', | |
21 | uuid: 'sc:identifier', | |
22 | category: 'sc:category', | |
23 | licence: 'sc:license', | |
24 | subtitleLanguage: 'sc:subtitleLanguage', | |
25 | sensitive: 'as:sensitive', | |
26 | language: 'sc:inLanguage', | |
27 | expires: 'sc:expires', | |
28 | CacheFile: 'pt:CacheFile', | |
29 | Infohash: 'pt:Infohash', | |
30 | originallyPublishedAt: 'sc:datePublished', | |
31 | views: { | |
32 | '@type': 'sc:Number', | |
33 | '@id': 'pt:views' | |
34 | }, | |
35 | state: { | |
36 | '@type': 'sc:Number', | |
37 | '@id': 'pt:state' | |
38 | }, | |
39 | size: { | |
40 | '@type': 'sc:Number', | |
41 | '@id': 'pt:size' | |
42 | }, | |
43 | fps: { | |
44 | '@type': 'sc:Number', | |
45 | '@id': 'pt:fps' | |
46 | }, | |
47 | startTimestamp: { | |
48 | '@type': 'sc:Number', | |
49 | '@id': 'pt:startTimestamp' | |
50 | }, | |
51 | stopTimestamp: { | |
52 | '@type': 'sc:Number', | |
53 | '@id': 'pt:stopTimestamp' | |
54 | }, | |
55 | position: { | |
56 | '@type': 'sc:Number', | |
57 | '@id': 'pt:position' | |
58 | }, | |
59 | commentsEnabled: { | |
60 | '@type': 'sc:Boolean', | |
61 | '@id': 'pt:commentsEnabled' | |
62 | }, | |
63 | downloadEnabled: { | |
64 | '@type': 'sc:Boolean', | |
65 | '@id': 'pt:downloadEnabled' | |
66 | }, | |
67 | waitTranscoding: { | |
68 | '@type': 'sc:Boolean', | |
69 | '@id': 'pt:waitTranscoding' | |
70 | }, | |
71 | support: { | |
72 | '@type': 'sc:Text', | |
73 | '@id': 'pt:support' | |
74 | } | |
75 | }, | |
76 | { | |
77 | likes: { | |
78 | '@id': 'as:likes', | |
79 | '@type': '@id' | |
80 | }, | |
81 | dislikes: { | |
82 | '@id': 'as:dislikes', | |
83 | '@type': '@id' | |
84 | }, | |
85 | playlists: { | |
86 | '@id': 'pt:playlists', | |
87 | '@type': '@id' | |
88 | }, | |
89 | shares: { | |
90 | '@id': 'as:shares', | |
91 | '@type': '@id' | |
92 | }, | |
93 | comments: { | |
94 | '@id': 'as:comments', | |
95 | '@type': '@id' | |
96 | } | |
97 | } | |
98 | ] | |
99 | }) | |
100 | } | |
101 | ||
102 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> | |
103 | async function activityPubCollectionPagination ( | |
104 | baseUrl: string, | |
105 | handler: ActivityPubCollectionPaginationHandler, | |
106 | page?: any, | |
107 | size = ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE | |
108 | ) { | |
109 | if (!page || !validator.isInt(page)) { | |
110 | // We just display the first page URL, we only need the total items | |
111 | const result = await handler(0, 1) | |
112 | ||
113 | return { | |
114 | id: baseUrl, | |
115 | type: 'OrderedCollectionPage', | |
116 | totalItems: result.total, | |
117 | first: baseUrl + '?page=1' | |
118 | } | |
119 | } | |
120 | ||
121 | const { start, count } = pageToStartAndCount(page, size) | |
122 | const result = await handler(start, count) | |
123 | ||
124 | let next: string | undefined | |
125 | let prev: string | undefined | |
126 | ||
127 | // Assert page is a number | |
128 | page = parseInt(page, 10) | |
129 | ||
130 | // There are more results | |
131 | if (result.total > page * size) { | |
132 | next = baseUrl + '?page=' + (page + 1) | |
133 | } | |
134 | ||
135 | if (page > 1) { | |
136 | prev = baseUrl + '?page=' + (page - 1) | |
137 | } | |
138 | ||
139 | return { | |
140 | id: baseUrl + '?page=' + page, | |
141 | type: 'OrderedCollectionPage', | |
142 | prev, | |
143 | next, | |
144 | partOf: baseUrl, | |
145 | orderedItems: result.data, | |
146 | totalItems: result.total | |
147 | } | |
148 | ||
149 | } | |
150 | ||
151 | function buildSignedActivity (byActor: MActor, data: Object) { | |
152 | const activity = activityPubContextify(data) | |
153 | ||
154 | return signJsonLDObject(byActor, activity) as Promise<Activity> | |
155 | } | |
156 | ||
157 | function getAPId (activity: string | { id: string }) { | |
158 | if (typeof activity === 'string') return activity | |
159 | ||
160 | return activity.id | |
161 | } | |
162 | ||
163 | function checkUrlsSameHost (url1: string, url2: string) { | |
164 | const idHost = parse(url1).host | |
165 | const actorHost = parse(url2).host | |
166 | ||
167 | return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase() | |
168 | } | |
169 | ||
170 | // --------------------------------------------------------------------------- | |
171 | ||
172 | export { | |
173 | checkUrlsSameHost, | |
174 | getAPId, | |
175 | activityPubContextify, | |
176 | activityPubCollectionPagination, | |
177 | buildSignedActivity | |
178 | } |