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