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