]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/share.ts
Merge branch 'release/3.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / share.ts
CommitLineData
1297eb5d 1import * as Bluebird from 'bluebird'
452b3bea
C
2import { Transaction } from 'sequelize'
3import { getServerActor } from '@server/models/application/application'
4import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
5import { logger, loggerTagsFactory } from '../../helpers/logger'
db4b15f2 6import { doJSONRequest } from '../../helpers/requests'
74dc3bca 7import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
452b3bea 8import { VideoShareModel } from '../../models/video/video-share'
26d6bf65 9import { MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../types/models/video'
452b3bea
C
10import { getOrCreateActorAndServerAndModel } from './actor'
11import { sendUndoAnnounce, sendVideoAnnounce } from './send'
12import { getLocalVideoAnnounceActivityPubUrl } from './url'
13
14const lTags = loggerTagsFactory('share')
892211e8 15
453e83ea 16async function shareVideoByServerAndChannel (video: MVideoAccountLight, t: Transaction) {
22a73cb8 17 if (!video.hasPrivacyForFederation()) return undefined
e12a0092 18
0f320037
C
19 return Promise.all([
20 shareByServer(video, t),
21 shareByVideoChannel(video, t)
22 ])
23}
24
453e83ea
C
25async function changeVideoChannelShare (
26 video: MVideoAccountLight,
27 oldVideoChannel: MChannelActorLight,
28 t: Transaction
29) {
452b3bea
C
30 logger.info(
31 'Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name,
32 lTags(video.uuid)
33 )
5cf84858 34
0f320037
C
35 await undoShareByVideoChannel(video, oldVideoChannel, t)
36
37 await shareByVideoChannel(video, t)
38}
39
453e83ea 40async function addVideoShares (shareUrls: string[], video: MVideoId) {
1297eb5d
C
41 await Bluebird.map(shareUrls, async shareUrl => {
42 try {
db4b15f2 43 const { body } = await doJSONRequest<any>(shareUrl, { activityPub: true })
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
de94ac86 77 const serverShareUrl = getLocalVideoAnnounceActivityPubUrl(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) {
de94ac86 94 const videoChannelShareUrl = getLocalVideoAnnounceActivityPubUrl(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}