]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/share.ts
Add ability to unfederate a local video (on blacklist)
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / share.ts
CommitLineData
892211e8 1import { Transaction } from 'sequelize'
e12a0092 2import { VideoPrivacy } from '../../../shared/models/videos'
da854ddd 3import { getServerActor } from '../../helpers/utils'
3fd3ab2d 4import { VideoModel } from '../../models/video/video'
3fd3ab2d 5import { VideoShareModel } from '../../models/video/video-share'
0f320037 6import { sendUndoAnnounce, sendVideoAnnounce } from './send'
5c6d985f 7import { getVideoAnnounceActivityPubUrl } from './url'
0f320037 8import { VideoChannelModel } from '../../models/video/video-channel'
1297eb5d
C
9import * as Bluebird from 'bluebird'
10import { doRequest } from '../../helpers/requests'
11import { getOrCreateActorAndServerAndModel } from './actor'
12import { logger } from '../../helpers/logger'
13import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers'
361805c4 14import { checkUrlsSameHost, getAPUrl } from '../../helpers/activitypub'
892211e8 15
a7d647c4 16async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
54b38063 17 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
e12a0092 18
0f320037
C
19 return Promise.all([
20 shareByServer(video, t),
21 shareByVideoChannel(video, t)
22 ])
23}
24
25async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
5cf84858
C
26 logger.info('Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name)
27
0f320037
C
28 await undoShareByVideoChannel(video, oldVideoChannel, t)
29
30 await shareByVideoChannel(video, t)
31}
32
1297eb5d
C
33async function addVideoShares (shareUrls: string[], instance: VideoModel) {
34 await Bluebird.map(shareUrls, async shareUrl => {
35 try {
36 // Fetch url
37 const { body } = await doRequest({
38 uri: shareUrl,
39 json: true,
40 activityPub: true
41 })
5c6d985f
C
42 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
43
361805c4 44 const actorUrl = getAPUrl(body.actor)
5c6d985f
C
45 if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
46 throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
47 }
1297eb5d 48
1297eb5d
C
49 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
50
51 const entry = {
52 actorId: actor.id,
53 videoId: instance.id,
54 url: shareUrl
55 }
56
57 await VideoShareModel.findOrCreate({
58 where: {
59 url: shareUrl
60 },
61 defaults: entry
62 })
63 } catch (err) {
64 logger.warn('Cannot add share %s.', shareUrl, { err })
65 }
66 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
67}
68
0f320037
C
69export {
70 changeVideoChannelShare,
1297eb5d 71 addVideoShares,
0f320037
C
72 shareVideoByServerAndChannel
73}
74
75// ---------------------------------------------------------------------------
76
77async function shareByServer (video: VideoModel, t: Transaction) {
50d6de9c 78 const serverActor = await getServerActor()
892211e8 79
5c6d985f 80 const serverShareUrl = getVideoAnnounceActivityPubUrl(serverActor, video)
5abb9fbb 81 const [ serverShare ] = await VideoShareModel.findOrCreate({
a7977280
C
82 defaults: {
83 actorId: serverActor.id,
84 videoId: video.id,
85 url: serverShareUrl
86 },
87 where: {
88 url: serverShareUrl
89 },
90 transaction: t
a7977280 91 })
5abb9fbb
C
92
93 return sendVideoAnnounce(serverActor, serverShare, video, t)
0f320037 94}
892211e8 95
0f320037 96async function shareByVideoChannel (video: VideoModel, t: Transaction) {
5c6d985f 97 const videoChannelShareUrl = getVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
5abb9fbb 98 const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
a7977280
C
99 defaults: {
100 actorId: video.VideoChannel.actorId,
101 videoId: video.id,
102 url: videoChannelShareUrl
103 },
104 where: {
105 url: videoChannelShareUrl
106 },
107 transaction: t
a7977280 108 })
5abb9fbb
C
109
110 return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
892211e8
C
111}
112
0f320037
C
113async function undoShareByVideoChannel (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
114 // Load old share
115 const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
116 if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
117
118 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
119 await oldShare.destroy({ transaction: t })
892211e8 120}