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