]>
Commit | Line | Data |
---|---|---|
8fffe21a C |
1 | import * as Bluebird from 'bluebird' |
2 | import * as validator from 'validator' | |
3fd3ab2d | 3 | import { ResultList } from '../../shared/models' |
6be84cbc | 4 | import { Activity, ActivityPubActor } from '../../shared/models/activitypub' |
3fd3ab2d | 5 | import { ACTIVITY_PUB } from '../initializers' |
50d6de9c | 6 | import { ActorModel } from '../models/activitypub/actor' |
afffe988 | 7 | import { signObject } from './peertube-crypto' |
8fffe21a | 8 | import { pageToStartAndCount } from './core-utils' |
571389d4 C |
9 | |
10 | function activityPubContextify <T> (data: T) { | |
2186386c | 11 | return Object.assign(data, { |
e4f97bab C |
12 | '@context': [ |
13 | 'https://www.w3.org/ns/activitystreams', | |
14 | 'https://w3id.org/security/v1', | |
15 | { | |
2186386c | 16 | RsaSignature2017: 'https://w3id.org/security#RsaSignature2017', |
c48e82b5 C |
17 | pt: 'https://joinpeertube.org/ns', |
18 | schema: 'http://schema.org#', | |
2186386c | 19 | Hashtag: 'as:Hashtag', |
c48e82b5 C |
20 | uuid: 'schema:identifier', |
21 | category: 'schema:category', | |
22 | licence: 'schema:license', | |
23 | subtitleLanguage: 'schema:subtitleLanguage', | |
2186386c | 24 | sensitive: 'as:sensitive', |
c48e82b5 C |
25 | language: 'schema:inLanguage', |
26 | views: 'schema:Number', | |
27 | stats: 'schema:Number', | |
28 | size: 'schema:Number', | |
29 | fps: 'schema:Number', | |
30 | commentsEnabled: 'schema:Boolean', | |
7f2cfe3a | 31 | downloadEnabled: 'schema:Boolean', |
c48e82b5 C |
32 | waitTranscoding: 'schema:Boolean', |
33 | expires: 'schema:expires', | |
34 | support: 'schema:Text', | |
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 C |
59 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> |
60 | async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { | |
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 { | |
66 | id: url, | |
67 | type: 'OrderedCollection', | |
68 | totalItems: result.total, | |
69 | first: url + '?page=1' | |
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) { |
e71bcc0f C |
84 | next = url + '?page=' + (page + 1) |
85 | } | |
86 | ||
87 | if (page > 1) { | |
88 | prev = url + '?page=' + (page - 1) | |
89 | } | |
90 | ||
8fffe21a | 91 | return { |
e71bcc0f C |
92 | id: url + '?page=' + page, |
93 | type: 'OrderedCollectionPage', | |
94 | prev, | |
95 | next, | |
96 | partOf: url, | |
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 | ||
50d6de9c | 106 | return signObject(byActor, activity) as Promise<Activity> |
afffe988 C |
107 | } |
108 | ||
6be84cbc C |
109 | function getActorUrl (activityActor: string | ActivityPubActor) { |
110 | if (typeof activityActor === 'string') return activityActor | |
111 | ||
112 | return activityActor.id | |
113 | } | |
114 | ||
e4f97bab C |
115 | // --------------------------------------------------------------------------- |
116 | ||
117 | export { | |
6be84cbc | 118 | getActorUrl, |
e4f97bab | 119 | activityPubContextify, |
0d0e8dd0 | 120 | activityPubCollectionPagination, |
892211e8 | 121 | buildSignedActivity |
e4f97bab | 122 | } |