]>
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 C |
16 | RsaSignature2017: 'https://w3id.org/security#RsaSignature2017', |
17 | Hashtag: 'as:Hashtag', | |
18 | uuid: 'http://schema.org/identifier', | |
19 | category: 'http://schema.org/category', | |
20 | licence: 'http://schema.org/license', | |
40e87e9e | 21 | subtitleLanguage: 'http://schema.org/subtitleLanguage', |
2186386c C |
22 | sensitive: 'as:sensitive', |
23 | language: 'http://schema.org/inLanguage', | |
24 | views: 'http://schema.org/Number', | |
25 | stats: 'http://schema.org/Number', | |
26 | size: 'http://schema.org/Number', | |
27 | commentsEnabled: 'http://schema.org/Boolean', | |
28 | waitTranscoding: 'http://schema.org/Boolean', | |
29 | support: 'http://schema.org/Text' | |
2fe86927 C |
30 | }, |
31 | { | |
32 | likes: { | |
33 | '@id': 'as:likes', | |
34 | '@type': '@id' | |
35 | }, | |
36 | dislikes: { | |
37 | '@id': 'as:dislikes', | |
38 | '@type': '@id' | |
39 | }, | |
40 | shares: { | |
41 | '@id': 'as:shares', | |
42 | '@type': '@id' | |
43 | }, | |
44 | comments: { | |
45 | '@id': 'as:comments', | |
46 | '@type': '@id' | |
47 | } | |
e4f97bab C |
48 | } |
49 | ] | |
50 | }) | |
51 | } | |
52 | ||
8fffe21a C |
53 | type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>> |
54 | async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) { | |
55 | if (!page || !validator.isInt(page)) { | |
56 | // We just display the first page URL, we only need the total items | |
57 | const result = await handler(0, 1) | |
58 | ||
59 | return { | |
60 | id: url, | |
61 | type: 'OrderedCollection', | |
62 | totalItems: result.total, | |
63 | first: url + '?page=1' | |
64 | } | |
16b90975 | 65 | } |
16b90975 | 66 | |
8fffe21a C |
67 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) |
68 | const result = await handler(start, count) | |
69 | ||
c1e791ba RK |
70 | let next: string | undefined |
71 | let prev: string | undefined | |
e71bcc0f | 72 | |
c46edbc2 C |
73 | // Assert page is a number |
74 | page = parseInt(page, 10) | |
75 | ||
e71bcc0f | 76 | // There are more results |
c46edbc2 | 77 | if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) { |
e71bcc0f C |
78 | next = url + '?page=' + (page + 1) |
79 | } | |
80 | ||
81 | if (page > 1) { | |
82 | prev = url + '?page=' + (page - 1) | |
83 | } | |
84 | ||
8fffe21a | 85 | return { |
e71bcc0f C |
86 | id: url + '?page=' + page, |
87 | type: 'OrderedCollectionPage', | |
88 | prev, | |
89 | next, | |
90 | partOf: url, | |
8fffe21a C |
91 | orderedItems: result.data, |
92 | totalItems: result.total | |
e4f97bab C |
93 | } |
94 | ||
e4f97bab C |
95 | } |
96 | ||
50d6de9c | 97 | function buildSignedActivity (byActor: ActorModel, data: Object) { |
afffe988 C |
98 | const activity = activityPubContextify(data) |
99 | ||
50d6de9c | 100 | return signObject(byActor, activity) as Promise<Activity> |
afffe988 C |
101 | } |
102 | ||
6be84cbc C |
103 | function getActorUrl (activityActor: string | ActivityPubActor) { |
104 | if (typeof activityActor === 'string') return activityActor | |
105 | ||
106 | return activityActor.id | |
107 | } | |
108 | ||
e4f97bab C |
109 | // --------------------------------------------------------------------------- |
110 | ||
111 | export { | |
6be84cbc | 112 | getActorUrl, |
e4f97bab | 113 | activityPubContextify, |
0d0e8dd0 | 114 | activityPubCollectionPagination, |
892211e8 | 115 | buildSignedActivity |
e4f97bab | 116 | } |