]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/activitypub.ts
Merge branch 'develop' into pr/1285
[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'
3fd3ab2d 5import { ACTIVITY_PUB } from '../initializers'
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
C
26 language: 'sc:inLanguage',
27 views: 'sc:Number',
1cf8aca1 28 state: 'sc:Number',
f7509cbe
C
29 size: 'sc:Number',
30 fps: 'sc:Number',
31 commentsEnabled: 'sc:Boolean',
88108880 32 downloadEnabled: 'sc:Boolean',
f7509cbe
C
33 waitTranscoding: 'sc:Boolean',
34 expires: 'sc:expires',
35 support: 'sc:Text',
09209296
C
36 CacheFile: 'pt:CacheFile',
37 Infohash: 'pt:Infohash'
2fe86927
C
38 },
39 {
40 likes: {
41 '@id': 'as:likes',
42 '@type': '@id'
43 },
44 dislikes: {
45 '@id': 'as:dislikes',
46 '@type': '@id'
47 },
48 shares: {
49 '@id': 'as:shares',
50 '@type': '@id'
51 },
52 comments: {
53 '@id': 'as:comments',
54 '@type': '@id'
55 }
e4f97bab
C
56 }
57 ]
58 })
59}
60
8fffe21a 61type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
babecc3c 62async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
8fffe21a
C
63 if (!page || !validator.isInt(page)) {
64 // We just display the first page URL, we only need the total items
65 const result = await handler(0, 1)
66
67 return {
babecc3c 68 id: baseUrl,
8fffe21a
C
69 type: 'OrderedCollection',
70 totalItems: result.total,
babecc3c 71 first: baseUrl + '?page=1'
8fffe21a 72 }
16b90975 73 }
16b90975 74
8fffe21a
C
75 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
76 const result = await handler(start, count)
77
c1e791ba
RK
78 let next: string | undefined
79 let prev: string | undefined
e71bcc0f 80
c46edbc2
C
81 // Assert page is a number
82 page = parseInt(page, 10)
83
e71bcc0f 84 // There are more results
c46edbc2 85 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
babecc3c 86 next = baseUrl + '?page=' + (page + 1)
e71bcc0f
C
87 }
88
89 if (page > 1) {
babecc3c 90 prev = baseUrl + '?page=' + (page - 1)
e71bcc0f
C
91 }
92
8fffe21a 93 return {
babecc3c 94 id: baseUrl + '?page=' + page,
e71bcc0f
C
95 type: 'OrderedCollectionPage',
96 prev,
97 next,
babecc3c 98 partOf: baseUrl,
8fffe21a
C
99 orderedItems: result.data,
100 totalItems: result.total
e4f97bab
C
101 }
102
e4f97bab
C
103}
104
50d6de9c 105function buildSignedActivity (byActor: ActorModel, data: Object) {
afffe988
C
106 const activity = activityPubContextify(data)
107
f7509cbe 108 return signJsonLDObject(byActor, activity) as Promise<Activity>
afffe988
C
109}
110
848f499d 111function getAPId (activity: string | { id: string }) {
361805c4 112 if (typeof activity === 'string') return activity
6be84cbc 113
361805c4 114 return activity.id
6be84cbc
C
115}
116
5c6d985f
C
117function checkUrlsSameHost (url1: string, url2: string) {
118 const idHost = parse(url1).host
119 const actorHost = parse(url2).host
120
121 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
6be84cbc
C
122}
123
e4f97bab
C
124// ---------------------------------------------------------------------------
125
126export {
5c6d985f 127 checkUrlsSameHost,
848f499d 128 getAPId,
e4f97bab 129 activityPubContextify,
0d0e8dd0 130 activityPubCollectionPagination,
892211e8 131 buildSignedActivity
e4f97bab 132}