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