]>
Commit | Line | Data |
---|---|---|
8fffe21a C |
1 | import * as Bluebird from 'bluebird' |
2 | import * as validator from 'validator' | |
3fd3ab2d | 3 | import { ResultList } from '../../shared/models' |
361805c4 | 4 | import { Activity } from '../../shared/models/activitypub' |
3fd3ab2d | 5 | import { ACTIVITY_PUB } from '../initializers' |
50d6de9c | 6 | import { ActorModel } from '../models/activitypub/actor' |
f7509cbe | 7 | import { signJsonLDObject } from './peertube-crypto' |
8fffe21a | 8 | import { pageToStartAndCount } from './core-utils' |
5c6d985f | 9 | import { parse } from 'url' |
571389d4 C |
10 | |
11 | function activityPubContextify <T> (data: T) { | |
2186386c | 12 | return Object.assign(data, { |
e4f97bab C |
13 | '@context': [ |
14 | 'https://www.w3.org/ns/activitystreams', | |
15 | 'https://w3id.org/security/v1', | |
16 | { | |
2186386c | 17 | RsaSignature2017: 'https://w3id.org/security#RsaSignature2017', |
09209296 | 18 | pt: 'https://joinpeertube.org/ns#', |
f7509cbe | 19 | sc: 'http://schema.org#', |
2186386c | 20 | Hashtag: 'as:Hashtag', |
f7509cbe C |
21 | uuid: 'sc:identifier', |
22 | category: 'sc:category', | |
23 | licence: 'sc:license', | |
24 | subtitleLanguage: 'sc:subtitleLanguage', | |
2186386c | 25 | sensitive: 'as:sensitive', |
f7509cbe C |
26 | language: 'sc:inLanguage', |
27 | views: 'sc:Number', | |
1cf8aca1 | 28 | state: 'sc:Number', |
f7509cbe C |
29 | size: 'sc:Number', |
30 | fps: 'sc:Number', | |
31 | commentsEnabled: 'sc:Boolean', | |
88108880 | 32 | downloadEnabled: 'sc:Boolean', |
f7509cbe C |
33 | waitTranscoding: 'sc:Boolean', |
34 | expires: 'sc:expires', | |
35 | support: 'sc:Text', | |
09209296 | 36 | CacheFile: 'pt:CacheFile', |
7519127b C |
37 | Infohash: 'pt:Infohash', |
38 | originallyPublishedAt: 'sc:DateTime' | |
2fe86927 C |
39 | }, |
40 | { | |
41 | likes: { | |
42 | '@id': 'as:likes', | |
43 | '@type': '@id' | |
44 | }, | |
45 | dislikes: { | |
46 | '@id': 'as:dislikes', | |
47 | '@type': '@id' | |
48 | }, | |
49 | shares: { | |
50 | '@id': 'as:shares', | |
51 | '@type': '@id' | |
52 | }, | |
53 | comments: { | |
54 | '@id': 'as:comments', | |
55 | '@type': '@id' | |
56 | } | |
e4f97bab C |
57 | } |
58 | ] | |
59 | }) | |
60 | } | |
61 | ||
8fffe21a | 62 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> |
babecc3c | 63 | async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { |
8fffe21a C |
64 | if (!page || !validator.isInt(page)) { |
65 | // We just display the first page URL, we only need the total items | |
66 | const result = await handler(0, 1) | |
67 | ||
68 | return { | |
babecc3c | 69 | id: baseUrl, |
8fffe21a C |
70 | type: 'OrderedCollection', |
71 | totalItems: result.total, | |
babecc3c | 72 | first: baseUrl + '?page=1' |
8fffe21a | 73 | } |
16b90975 | 74 | } |
16b90975 | 75 | |
8fffe21a C |
76 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) |
77 | const result = await handler(start, count) | |
78 | ||
c1e791ba RK |
79 | let next: string | undefined |
80 | let prev: string | undefined | |
e71bcc0f | 81 | |
c46edbc2 C |
82 | // Assert page is a number |
83 | page = parseInt(page, 10) | |
84 | ||
e71bcc0f | 85 | // There are more results |
c46edbc2 | 86 | if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) { |
babecc3c | 87 | next = baseUrl + '?page=' + (page + 1) |
e71bcc0f C |
88 | } |
89 | ||
90 | if (page > 1) { | |
babecc3c | 91 | prev = baseUrl + '?page=' + (page - 1) |
e71bcc0f C |
92 | } |
93 | ||
8fffe21a | 94 | return { |
babecc3c | 95 | id: baseUrl + '?page=' + page, |
e71bcc0f C |
96 | type: 'OrderedCollectionPage', |
97 | prev, | |
98 | next, | |
babecc3c | 99 | partOf: baseUrl, |
8fffe21a C |
100 | orderedItems: result.data, |
101 | totalItems: result.total | |
e4f97bab C |
102 | } |
103 | ||
e4f97bab C |
104 | } |
105 | ||
50d6de9c | 106 | function buildSignedActivity (byActor: ActorModel, data: Object) { |
afffe988 C |
107 | const activity = activityPubContextify(data) |
108 | ||
f7509cbe | 109 | return signJsonLDObject(byActor, activity) as Promise<Activity> |
afffe988 C |
110 | } |
111 | ||
848f499d | 112 | function getAPId (activity: string | { id: string }) { |
361805c4 | 113 | if (typeof activity === 'string') return activity |
6be84cbc | 114 | |
361805c4 | 115 | return activity.id |
6be84cbc C |
116 | } |
117 | ||
5c6d985f C |
118 | function checkUrlsSameHost (url1: string, url2: string) { |
119 | const idHost = parse(url1).host | |
120 | const actorHost = parse(url2).host | |
121 | ||
122 | return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase() | |
6be84cbc C |
123 | } |
124 | ||
e4f97bab C |
125 | // --------------------------------------------------------------------------- |
126 | ||
127 | export { | |
5c6d985f | 128 | checkUrlsSameHost, |
848f499d | 129 | getAPId, |
e4f97bab | 130 | activityPubContextify, |
0d0e8dd0 | 131 | activityPubCollectionPagination, |
892211e8 | 132 | buildSignedActivity |
e4f97bab | 133 | } |