]>
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', |
c48e82b5 | 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', | |
32 | waitTranscoding: 'sc:Boolean', | |
33 | expires: 'sc:expires', | |
34 | support: 'sc:Text', | |
c48e82b5 | 35 | CacheFile: 'pt:CacheFile' |
2fe86927 C |
36 | }, |
37 | { | |
38 | likes: { | |
39 | '@id': 'as:likes', | |
40 | '@type': '@id' | |
41 | }, | |
42 | dislikes: { | |
43 | '@id': 'as:dislikes', | |
44 | '@type': '@id' | |
45 | }, | |
46 | shares: { | |
47 | '@id': 'as:shares', | |
48 | '@type': '@id' | |
49 | }, | |
50 | comments: { | |
51 | '@id': 'as:comments', | |
52 | '@type': '@id' | |
53 | } | |
e4f97bab C |
54 | } |
55 | ] | |
56 | }) | |
57 | } | |
58 | ||
8fffe21a | 59 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> |
babecc3c | 60 | async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { |
8fffe21a C |
61 | if (!page || !validator.isInt(page)) { |
62 | // We just display the first page URL, we only need the total items | |
63 | const result = await handler(0, 1) | |
64 | ||
65 | return { | |
babecc3c | 66 | id: baseUrl, |
8fffe21a C |
67 | type: 'OrderedCollection', |
68 | totalItems: result.total, | |
babecc3c | 69 | first: baseUrl + '?page=1' |
8fffe21a | 70 | } |
16b90975 | 71 | } |
16b90975 | 72 | |
8fffe21a C |
73 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) |
74 | const result = await handler(start, count) | |
75 | ||
c1e791ba RK |
76 | let next: string | undefined |
77 | let prev: string | undefined | |
e71bcc0f | 78 | |
c46edbc2 C |
79 | // Assert page is a number |
80 | page = parseInt(page, 10) | |
81 | ||
e71bcc0f | 82 | // There are more results |
c46edbc2 | 83 | if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) { |
babecc3c | 84 | next = baseUrl + '?page=' + (page + 1) |
e71bcc0f C |
85 | } |
86 | ||
87 | if (page > 1) { | |
babecc3c | 88 | prev = baseUrl + '?page=' + (page - 1) |
e71bcc0f C |
89 | } |
90 | ||
8fffe21a | 91 | return { |
babecc3c | 92 | id: baseUrl + '?page=' + page, |
e71bcc0f C |
93 | type: 'OrderedCollectionPage', |
94 | prev, | |
95 | next, | |
babecc3c | 96 | partOf: baseUrl, |
8fffe21a C |
97 | orderedItems: result.data, |
98 | totalItems: result.total | |
e4f97bab C |
99 | } |
100 | ||
e4f97bab C |
101 | } |
102 | ||
50d6de9c | 103 | function buildSignedActivity (byActor: ActorModel, data: Object) { |
afffe988 C |
104 | const activity = activityPubContextify(data) |
105 | ||
f7509cbe | 106 | return signJsonLDObject(byActor, activity) as Promise<Activity> |
afffe988 C |
107 | } |
108 | ||
361805c4 C |
109 | function getAPUrl (activity: string | { id: string }) { |
110 | if (typeof activity === 'string') return activity | |
6be84cbc | 111 | |
361805c4 | 112 | return activity.id |
6be84cbc C |
113 | } |
114 | ||
5c6d985f C |
115 | function checkUrlsSameHost (url1: string, url2: string) { |
116 | const idHost = parse(url1).host | |
117 | const actorHost = parse(url2).host | |
118 | ||
119 | return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase() | |
120 | } | |
121 | ||
e4f97bab C |
122 | // --------------------------------------------------------------------------- |
123 | ||
124 | export { | |
5c6d985f | 125 | checkUrlsSameHost, |
361805c4 | 126 | getAPUrl, |
e4f97bab | 127 | activityPubContextify, |
0d0e8dd0 | 128 | activityPubCollectionPagination, |
892211e8 | 129 | buildSignedActivity |
e4f97bab | 130 | } |