]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/activitypub.ts
Add originallyPublishedAt unit tests
[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 commentsEnabled: 'sc:Boolean',
32 downloadEnabled: 'sc:Boolean',
33 waitTranscoding: 'sc:Boolean',
34 expires: 'sc:expires',
35 support: 'sc:Text',
36 CacheFile: 'pt:CacheFile',
37 Infohash: 'pt:Infohash',
38 originallyPublishedAt: 'sc:DateTime'
39 },
40 {
41 likes: {
42 '@id': 'as:likes',
43 '@type': '@id'
44 },
45 dislikes: {
46 '@id': 'as:dislikes',
47 '@type': '@id'
48 },
49 shares: {
50 '@id': 'as:shares',
51 '@type': '@id'
52 },
53 comments: {
54 '@id': 'as:comments',
55 '@type': '@id'
56 }
57 }
58 ]
59 })
60 }
61
62 type ActivityPubCollectionPaginationHandler = (start: number, count: number) => Bluebird<ResultList<any>> | Promise<ResultList<any>>
63 async function activityPubCollectionPagination (baseUrl: string, handler: ActivityPubCollectionPaginationHandler, page?: any) {
64 if (!page || !validator.isInt(page)) {
65 // We just display the first page URL, we only need the total items
66 const result = await handler(0, 1)
67
68 return {
69 id: baseUrl,
70 type: 'OrderedCollection',
71 totalItems: result.total,
72 first: baseUrl + '?page=1'
73 }
74 }
75
76 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
77 const result = await handler(start, count)
78
79 let next: string | undefined
80 let prev: string | undefined
81
82 // Assert page is a number
83 page = parseInt(page, 10)
84
85 // There are more results
86 if (result.total > page * ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) {
87 next = baseUrl + '?page=' + (page + 1)
88 }
89
90 if (page > 1) {
91 prev = baseUrl + '?page=' + (page - 1)
92 }
93
94 return {
95 id: baseUrl + '?page=' + page,
96 type: 'OrderedCollectionPage',
97 prev,
98 next,
99 partOf: baseUrl,
100 orderedItems: result.data,
101 totalItems: result.total
102 }
103
104 }
105
106 function buildSignedActivity (byActor: ActorModel, data: Object) {
107 const activity = activityPubContextify(data)
108
109 return signJsonLDObject(byActor, activity) as Promise<Activity>
110 }
111
112 function getAPId (activity: string | { id: string }) {
113 if (typeof activity === 'string') return activity
114
115 return activity.id
116 }
117
118 function checkUrlsSameHost (url1: string, url2: string) {
119 const idHost = parse(url1).host
120 const actorHost = parse(url2).host
121
122 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
123 }
124
125 // ---------------------------------------------------------------------------
126
127 export {
128 checkUrlsSameHost,
129 getAPId,
130 activityPubContextify,
131 activityPubCollectionPagination,
132 buildSignedActivity
133 }