]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Reset published date on publish
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
CommitLineData
8fffe21a
C
1import * as Bluebird from 'bluebird'
2import * as validator from 'validator'
3fd3ab2d 3import { ResultList } from '../../shared/models'
6be84cbc 4import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
3fd3ab2d 5import { ACTIVITY_PUB } from '../initializers'
50d6de9c 6import { ActorModel } from '../models/activitypub/actor'
afffe988 7import { signObject } from './peertube-crypto'
8fffe21a 8import { pageToStartAndCount } from './core-utils'
571389d4
C
9
10function 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',
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'
2fe86927
C
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 }
e4f97bab
C
47 }
48 ]
49 })
50}
51
8fffe21a
C
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 }
16b90975 64 }
16b90975 65
8fffe21a
C
66 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
67 const result = await handler(start, count)
68
e71bcc0f
C
69 let next: string
70 let prev: string
71
c46edbc2
C
72 // Assert page is a number
73 page = parseInt(page, 10)
74
e71bcc0f 75 // There are more results
c46edbc2 76 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
e71bcc0f
C
77 next = url + '?page=' + (page + 1)
78 }
79
80 if (page > 1) {
81 prev = url + '?page=' + (page - 1)
82 }
83
8fffe21a 84 return {
e71bcc0f
C
85 id: url + '?page=' + page,
86 type: 'OrderedCollectionPage',
87 prev,
88 next,
89 partOf: url,
8fffe21a
C
90 orderedItems: result.data,
91 totalItems: result.total
e4f97bab
C
92 }
93
e4f97bab
C
94}
95
50d6de9c 96function buildSignedActivity (byActor: ActorModel, data: Object) {
afffe988
C
97 const activity = activityPubContextify(data)
98
50d6de9c 99 return signObject(byActor, activity) as Promise<Activity>
afffe988
C
100}
101
6be84cbc
C
102function getActorUrl (activityActor: string | ActivityPubActor) {
103 if (typeof activityActor === 'string') return activityActor
104
105 return activityActor.id
106}
107
e4f97bab
C
108// ---------------------------------------------------------------------------
109
110export {
6be84cbc 111 getActorUrl,
e4f97bab 112 activityPubContextify,
0d0e8dd0 113 activityPubCollectionPagination,
892211e8 114 buildSignedActivity
e4f97bab 115}