aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/activitypub.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-16 15:22:39 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:52 +0100
commit20494f122186bb1bfd82f4c598c4744acea27b0c (patch)
tree097652a31ecf70491b970b6c8e06b22380ee004f /server/helpers/activitypub.ts
parentefc32059d980c51793e8e9ac0fb6a885a8026f94 (diff)
downloadPeerTube-20494f122186bb1bfd82f4c598c4744acea27b0c.tar.gz
PeerTube-20494f122186bb1bfd82f4c598c4744acea27b0c.tar.zst
PeerTube-20494f122186bb1bfd82f4c598c4744acea27b0c.zip
Server shares user videos
Diffstat (limited to 'server/helpers/activitypub.ts')
-rw-r--r--server/helpers/activitypub.ts64
1 files changed, 58 insertions, 6 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts
index b376b8ca2..c710117cd 100644
--- a/server/helpers/activitypub.ts
+++ b/server/helpers/activitypub.ts
@@ -4,13 +4,18 @@ import * as Sequelize from 'sequelize'
4import * as url from 'url' 4import * as url from 'url'
5import { ActivityIconObject } from '../../shared/index' 5import { ActivityIconObject } from '../../shared/index'
6import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor' 6import { ActivityPubActor } from '../../shared/models/activitypub/activitypub-actor'
7import { VideoChannelObject } from '../../shared/models/activitypub/objects/video-channel-object'
7import { ResultList } from '../../shared/models/result-list.model' 8import { ResultList } from '../../shared/models/result-list.model'
8import { database as db, REMOTE_SCHEME } from '../initializers' 9import { database as db, REMOTE_SCHEME } from '../initializers'
9import { ACTIVITY_PUB_ACCEPT_HEADER, CONFIG, STATIC_PATHS } from '../initializers/constants' 10import { ACTIVITY_PUB_ACCEPT_HEADER, CONFIG, STATIC_PATHS } from '../initializers/constants'
10import { sendAnnounce } from '../lib/activitypub/send-request' 11import { videoChannelActivityObjectToDBAttributes } from '../lib/activitypub/misc'
12import { sendVideoAnnounce } from '../lib/activitypub/send-request'
13import { sendVideoChannelAnnounce } from '../lib/index'
14import { AccountInstance } from '../models/account/account-interface'
11import { VideoChannelInstance } from '../models/video/video-channel-interface' 15import { VideoChannelInstance } from '../models/video/video-channel-interface'
12import { VideoInstance } from '../models/video/video-interface' 16import { VideoInstance } from '../models/video/video-interface'
13import { isRemoteAccountValid } from './custom-validators' 17import { isRemoteAccountValid } from './custom-validators'
18import { isVideoChannelObjectValid } from './custom-validators/activitypub/videos'
14import { logger } from './logger' 19import { logger } from './logger'
15import { doRequest, doRequestAndSaveToFile } from './requests' 20import { doRequest, doRequestAndSaveToFile } from './requests'
16import { getServerAccount } from './utils' 21import { getServerAccount } from './utils'
@@ -34,7 +39,7 @@ async function shareVideoChannelByServer (videoChannel: VideoChannelInstance, t:
34 videoChannelId: videoChannel.id 39 videoChannelId: videoChannel.id
35 }, { transaction: t }) 40 }, { transaction: t })
36 41
37 return sendAnnounce(serverAccount, videoChannel, t) 42 return sendVideoChannelAnnounce(serverAccount, videoChannel, t)
38} 43}
39 44
40async function shareVideoByServer (video: VideoInstance, t: Sequelize.Transaction) { 45async function shareVideoByServer (video: VideoInstance, t: Sequelize.Transaction) {
@@ -45,7 +50,7 @@ async function shareVideoByServer (video: VideoInstance, t: Sequelize.Transactio
45 videoId: video.id 50 videoId: video.id
46 }, { transaction: t }) 51 }, { transaction: t })
47 52
48 return sendAnnounce(serverAccount, video, t) 53 return sendVideoAnnounce(serverAccount, video, t)
49} 54}
50 55
51function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account' | 'videoAbuse', id: string) { 56function getActivityPubUrl (type: 'video' | 'videoChannel' | 'account' | 'videoAbuse', id: string) {
@@ -66,13 +71,27 @@ async function getOrCreateAccount (accountUrl: string) {
66 if (res === undefined) throw new Error('Cannot fetch remote account.') 71 if (res === undefined) throw new Error('Cannot fetch remote account.')
67 72
68 // Save our new account in database 73 // Save our new account in database
69 const account = res.account 74 account = await res.account.save()
70 await account.save()
71 } 75 }
72 76
73 return account 77 return account
74} 78}
75 79
80async function getOrCreateVideoChannel (ownerAccount: AccountInstance, videoChannelUrl: string) {
81 let videoChannel = await db.VideoChannel.loadByUrl(videoChannelUrl)
82
83 // We don't have this account in our database, fetch it on remote
84 if (!videoChannel) {
85 videoChannel = await fetchRemoteVideoChannel(ownerAccount, videoChannelUrl)
86 if (videoChannel === undefined) throw new Error('Cannot fetch remote video channel.')
87
88 // Save our new video channel in database
89 await videoChannel.save()
90 }
91
92 return videoChannel
93}
94
76async function fetchRemoteAccountAndCreateServer (accountUrl: string) { 95async function fetchRemoteAccountAndCreateServer (accountUrl: string) {
77 const options = { 96 const options = {
78 uri: accountUrl, 97 uri: accountUrl,
@@ -131,6 +150,38 @@ async function fetchRemoteAccountAndCreateServer (accountUrl: string) {
131 return { account, server } 150 return { account, server }
132} 151}
133 152
153async function fetchRemoteVideoChannel (ownerAccount: AccountInstance, videoChannelUrl: string) {
154 const options = {
155 uri: videoChannelUrl,
156 method: 'GET',
157 headers: {
158 'Accept': ACTIVITY_PUB_ACCEPT_HEADER
159 }
160 }
161
162 logger.info('Fetching remote video channel %s.', videoChannelUrl)
163
164 let requestResult
165 try {
166 requestResult = await doRequest(options)
167 } catch (err) {
168 logger.warn('Cannot fetch remote video channel %s.', videoChannelUrl, err)
169 return undefined
170 }
171
172 const videoChannelJSON: VideoChannelObject = JSON.parse(requestResult.body)
173 if (isVideoChannelObjectValid(videoChannelJSON) === false) {
174 logger.debug('Remote video channel JSON is not valid.', { videoChannelJSON })
175 return undefined
176 }
177
178 const videoChannelAttributes = videoChannelActivityObjectToDBAttributes(videoChannelJSON, ownerAccount)
179 const videoChannel = db.VideoChannel.build(videoChannelAttributes)
180 videoChannel.Account = ownerAccount
181
182 return videoChannel
183}
184
134function fetchRemoteVideoPreview (video: VideoInstance) { 185function fetchRemoteVideoPreview (video: VideoInstance) {
135 // FIXME: use url 186 // FIXME: use url
136 const host = video.VideoChannel.Account.Server.host 187 const host = video.VideoChannel.Account.Server.host
@@ -200,7 +251,8 @@ export {
200 fetchRemoteVideoPreview, 251 fetchRemoteVideoPreview,
201 fetchRemoteVideoDescription, 252 fetchRemoteVideoDescription,
202 shareVideoChannelByServer, 253 shareVideoChannelByServer,
203 shareVideoByServer 254 shareVideoByServer,
255 getOrCreateVideoChannel
204} 256}
205 257
206// --------------------------------------------------------------------------- 258// ---------------------------------------------------------------------------