]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Add list of instance follows in about page
[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'
361805c4 4import { Activity } from '../../shared/models/activitypub'
74dc3bca 5import { ACTIVITY_PUB } from '../initializers/constants'
50d6de9c 6import { ActorModel } from '../models/activitypub/actor'
f7509cbe 7import { signJsonLDObject } from './peertube-crypto'
8fffe21a 8import { pageToStartAndCount } from './core-utils'
5c6d985f 9import { parse } from 'url'
571389d4
C
10
11function activityPubContextify <T> (data: T) {
2186386c 12 return Object.assign(data, {
e4f97bab
C
13 '@context': [
14 'https://www.w3.org/ns/activitystreams',
15 'https://w3id.org/security/v1',
16 {
2186386c 17 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
09209296 18 pt: 'https://joinpeertube.org/ns#',
f7509cbe 19 sc: 'http://schema.org#',
2186386c 20 Hashtag: 'as:Hashtag',
f7509cbe
C
21 uuid: 'sc:identifier',
22 category: 'sc:category',
23 licence: 'sc:license',
24 subtitleLanguage: 'sc:subtitleLanguage',
2186386c 25 sensitive: 'as:sensitive',
f7509cbe 26 language: 'sc:inLanguage',
f7509cbe 27 expires: 'sc:expires',
09209296 28 CacheFile: 'pt:CacheFile',
7519127b 29 Infohash: 'pt:Infohash',
ae3171b6
C
30 originallyPublishedAt: 'sc:datePublished',
31 views: {
4e10f4b2 32 '@type': 'sc:Number',
ae3171b6
C
33 '@id': 'pt:views'
34 },
35 state: {
4e10f4b2 36 '@type': 'sc:Number',
ae3171b6
C
37 '@id': 'pt:state'
38 },
39 size: {
4e10f4b2 40 '@type': 'sc:Number',
ae3171b6
C
41 '@id': 'pt:size'
42 },
43 fps: {
4e10f4b2 44 '@type': 'sc:Number',
ae3171b6
C
45 '@id': 'pt:fps'
46 },
47 startTimestamp: {
4e10f4b2 48 '@type': 'sc:Number',
ae3171b6
C
49 '@id': 'pt:startTimestamp'
50 },
51 stopTimestamp: {
4e10f4b2 52 '@type': 'sc:Number',
ae3171b6
C
53 '@id': 'pt:stopTimestamp'
54 },
55 position: {
4e10f4b2 56 '@type': 'sc:Number',
ae3171b6
C
57 '@id': 'pt:position'
58 },
59 commentsEnabled: {
4e10f4b2 60 '@type': 'sc:Boolean',
ae3171b6
C
61 '@id': 'pt:commentsEnabled'
62 },
63 downloadEnabled: {
4e10f4b2 64 '@type': 'sc:Boolean',
ae3171b6
C
65 '@id': 'pt:downloadEnabled'
66 },
67 waitTranscoding: {
4e10f4b2 68 '@type': 'sc:Boolean',
ae3171b6
C
69 '@id': 'pt:waitTranscoding'
70 },
71 support: {
4e10f4b2 72 '@type': 'sc:Text',
ae3171b6
C
73 '@id': 'pt:support'
74 }
2fe86927
C
75 },
76 {
77 likes: {
78 '@id': 'as:likes',
79 '@type': '@id'
80 },
81 dislikes: {
82 '@id': 'as:dislikes',
83 '@type': '@id'
84 },
418d092a
C
85 playlists: {
86 '@id': 'pt:playlists',
87 '@type': '@id'
88 },
2fe86927
C
89 shares: {
90 '@id': 'as:shares',
91 '@type': '@id'
92 },
93 comments: {
94 '@id': 'as:comments',
95 '@type': '@id'
96 }
e4f97bab
C
97 }
98 ]
99 })
100}
101
8fffe21a 102type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
babecc3c 103async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
8fffe21a
C
104 if (!page || !validator.isInt(page)) {
105 // We just display the first page URL, we only need the total items
106 const result = await handler(0, 1)
107
108 return {
babecc3c 109 id: baseUrl,
418d092a 110 type: 'OrderedCollectionPage',
8fffe21a 111 totalItems: result.total,
babecc3c 112 first: baseUrl + '?page=1'
8fffe21a 113 }
16b90975 114 }
16b90975 115
8fffe21a
C
116 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
117 const result = await handler(start, count)
118
c1e791ba
RK
119 let next: string | undefined
120 let prev: string | undefined
e71bcc0f 121
c46edbc2
C
122 // Assert page is a number
123 page = parseInt(page, 10)
124
e71bcc0f 125 // There are more results
c46edbc2 126 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
babecc3c 127 next = baseUrl + '?page=' + (page + 1)
e71bcc0f
C
128 }
129
130 if (page > 1) {
babecc3c 131 prev = baseUrl + '?page=' + (page - 1)
e71bcc0f
C
132 }
133
8fffe21a 134 return {
babecc3c 135 id: baseUrl + '?page=' + page,
e71bcc0f
C
136 type: 'OrderedCollectionPage',
137 prev,
138 next,
babecc3c 139 partOf: baseUrl,
8fffe21a
C
140 orderedItems: result.data,
141 totalItems: result.total
e4f97bab
C
142 }
143
e4f97bab
C
144}
145
50d6de9c 146function buildSignedActivity (byActor: ActorModel, data: Object) {
afffe988
C
147 const activity = activityPubContextify(data)
148
f7509cbe 149 return signJsonLDObject(byActor, activity) as Promise<Activity>
afffe988
C
150}
151
848f499d 152function getAPId (activity: string | { id: string }) {
361805c4 153 if (typeof activity === 'string') return activity
6be84cbc 154
361805c4 155 return activity.id
6be84cbc
C
156}
157
5c6d985f
C
158function checkUrlsSameHost (url1: string, url2: string) {
159 const idHost = parse(url1).host
160 const actorHost = parse(url2).host
161
162 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
6be84cbc
C
163}
164
e4f97bab
C
165// ---------------------------------------------------------------------------
166
167export {
5c6d985f 168 checkUrlsSameHost,
848f499d 169 getAPId,
e4f97bab 170 activityPubContextify,
0d0e8dd0 171 activityPubCollectionPagination,
892211e8 172 buildSignedActivity
e4f97bab 173}