]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
Playlist server API
[github/Chocobozzz/PeerTube.git] / server / helpers / activitypub.ts
1 import * as Bluebird from 'bluebird'
2 import * as validator from 'validator'
3 import { ResultList } from '../../shared/models'
4 import { Activity } from '../../shared/models/activitypub'
5 import { ACTIVITY_PUB } from '../initializers'
6 import { ActorModel } from '../models/activitypub/actor'
7 import { signJsonLDObject } from './peertube-crypto'
8 import { pageToStartAndCount } from './core-utils'
9 import { parse } from 'url'
10
11 function activityPubContextify <T> (data: T) {
12 return Object.assign(data, {
13 '@context': [
14 'https://www.w3.org/ns/activitystreams',
15 'https://w3id.org/security/v1',
16 {
17 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
18 pt: 'https://joinpeertube.org/ns#',
19 sc: 'http://schema.org#',
20 Hashtag: 'as:Hashtag',
21 uuid: 'sc:identifier',
22 category: 'sc:category',
23 licence: 'sc:license',
24 subtitleLanguage: 'sc:subtitleLanguage',
25 sensitive: 'as:sensitive',
26 language: 'sc:inLanguage',
27 views: 'sc:Number',
28 state: 'sc:Number',
29 size: 'sc:Number',
30 fps: 'sc:Number',
31 startTimestamp: 'sc:Number',
32 stopTimestamp: 'sc:Number',
33 position: 'sc:Number',
34 commentsEnabled: 'sc:Boolean',
35 downloadEnabled: 'sc:Boolean',
36 waitTranscoding: 'sc:Boolean',
37 expires: 'sc:expires',
38 support: 'sc:Text',
39 CacheFile: 'pt:CacheFile',
40 Infohash: 'pt:Infohash',
41 originallyPublishedAt: 'sc:DateTime'
42 },
43 {
44 likes: {
45 '@id': 'as:likes',
46 '@type': '@id'
47 },
48 dislikes: {
49 '@id': 'as:dislikes',
50 '@type': '@id'
51 },
52 playlists: {
53 '@id': 'pt:playlists',
54 '@type': '@id'
55 },
56 shares: {
57 '@id': 'as:shares',
58 '@type': '@id'
59 },
60 comments: {
61 '@id': 'as:comments',
62 '@type': '@id'
63 }
64 }
65 ]
66 })
67 }
68
69 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
70 async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
71 if (!page || !validator.isInt(page)) {
72 // We just display the first page URL, we only need the total items
73 const result = await handler(0, 1)
74
75 return {
76 id: baseUrl,
77 type: 'OrderedCollectionPage',
78 totalItems: result.total,
79 first: baseUrl + '?page=1'
80 }
81 }
82
83 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
84 const result = await handler(start, count)
85
86 let next: string | undefined
87 let prev: string | undefined
88
89 // Assert page is a number
90 page = parseInt(page, 10)
91
92 // There are more results
93 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
94 next = baseUrl + '?page=' + (page + 1)
95 }
96
97 if (page > 1) {
98 prev = baseUrl + '?page=' + (page - 1)
99 }
100
101 return {
102 id: baseUrl + '?page=' + page,
103 type: 'OrderedCollectionPage',
104 prev,
105 next,
106 partOf: baseUrl,
107 orderedItems: result.data,
108 totalItems: result.total
109 }
110
111 }
112
113 function buildSignedActivity (byActor: ActorModel, data: Object) {
114 const activity = activityPubContextify(data)
115
116 return signJsonLDObject(byActor, activity) as Promise<Activity>
117 }
118
119 function getAPId (activity: string | { id: string }) {
120 if (typeof activity === 'string') return activity
121
122 return activity.id
123 }
124
125 function checkUrlsSameHost (url1: string, url2: string) {
126 const idHost = parse(url1).host
127 const actorHost = parse(url2).host
128
129 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
130 }
131
132 // ---------------------------------------------------------------------------
133
134 export {
135 checkUrlsSameHost,
136 getAPId,
137 activityPubContextify,
138 activityPubCollectionPagination,
139 buildSignedActivity
140 }