]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/activitypub.ts
Reset published date on publish
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
... / ...
CommitLineData
1import * as Bluebird from 'bluebird'
2import * as validator from 'validator'
3import { ResultList } from '../../shared/models'
4import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
5import { ACTIVITY_PUB } from '../initializers'
6import { ActorModel } from '../models/activitypub/actor'
7import { signObject } from './peertube-crypto'
8import { pageToStartAndCount } from './core-utils'
9
10function activityPubContextify <T> (data: T) {
11 return Object.assign(data, {
12 '@context': [
13 'https://www.w3.org/ns/activitystreams',
14 'https://w3id.org/security/v1',
15 {
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',
21 sensitive: 'as:sensitive',
22 language: 'http://schema.org/inLanguage',
23 views: 'http://schema.org/Number',
24 stats: 'http://schema.org/Number',
25 size: 'http://schema.org/Number',
26 commentsEnabled: 'http://schema.org/Boolean',
27 waitTranscoding: 'http://schema.org/Boolean',
28 support: 'http://schema.org/Text'
29 },
30 {
31 likes: {
32 '@id': 'as:likes',
33 '@type': '@id'
34 },
35 dislikes: {
36 '@id': 'as:dislikes',
37 '@type': '@id'
38 },
39 shares: {
40 '@id': 'as:shares',
41 '@type': '@id'
42 },
43 comments: {
44 '@id': 'as:comments',
45 '@type': '@id'
46 }
47 }
48 ]
49 })
50}
51
52type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
53async function activityPubCollectionPagination (url: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
54 if (!page || !validator.isInt(page)) {
55 // We just display the first page URL, we only need the total items
56 const result = await handler(0, 1)
57
58 return {
59 id: url,
60 type: 'OrderedCollection',
61 totalItems: result.total,
62 first: url + '?page=1'
63 }
64 }
65
66 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
67 const result = await handler(start, count)
68
69 let next: string
70 let prev: string
71
72 // Assert page is a number
73 page = parseInt(page, 10)
74
75 // There are more results
76 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
77 next = url + '?page=' + (page + 1)
78 }
79
80 if (page > 1) {
81 prev = url + '?page=' + (page - 1)
82 }
83
84 return {
85 id: url + '?page=' + page,
86 type: 'OrderedCollectionPage',
87 prev,
88 next,
89 partOf: url,
90 orderedItems: result.data,
91 totalItems: result.total
92 }
93
94}
95
96function buildSignedActivity (byActor: ActorModel, data: Object) {
97 const activity = activityPubContextify(data)
98
99 return signObject(byActor, activity) as Promise<Activity>
100}
101
102function getActorUrl (activityActor: string | ActivityPubActor) {
103 if (typeof activityActor === 'string') return activityActor
104
105 return activityActor.id
106}
107
108// ---------------------------------------------------------------------------
109
110export {
111 getActorUrl,
112 activityPubContextify,
113 activityPubCollectionPagination,
114 buildSignedActivity
115}