]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/share.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / share.ts
CommitLineData
41fb13c3 1import { map } from 'bluebird'
452b3bea
C
2import { Transaction } from 'sequelize'
3import { getServerActor } from '@server/models/application/application'
452b3bea 4import { logger, loggerTagsFactory } from '../../helpers/logger'
db4b15f2 5import { doJSONRequest } from '../../helpers/requests'
74dc3bca 6import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
452b3bea 7import { VideoShareModel } from '../../models/video/video-share'
26d6bf65 8import { MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../types/models/video'
7e98a7df 9import { getAPId } from './activity'
136d7efd 10import { getOrCreateAPActor } from './actors'
452b3bea 11import { sendUndoAnnounce, sendVideoAnnounce } from './send'
7e98a7df 12import { checkUrlsSameHost, getLocalVideoAnnounceActivityPubUrl } from './url'
452b3bea
C
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) {
41fb13c3 41 await map(shareUrls, async shareUrl => {
1297eb5d 42 try {
49af5ac8 43 await addVideoShare(shareUrl, video)
1297eb5d
C
44 } catch (err) {
45 logger.warn('Cannot add share %s.', shareUrl, { err })
46 }
47 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
48}
49
0f320037
C
50export {
51 changeVideoChannelShare,
1297eb5d 52 addVideoShares,
0f320037
C
53 shareVideoByServerAndChannel
54}
55
56// ---------------------------------------------------------------------------
57
49af5ac8
C
58async function addVideoShare (shareUrl: string, video: MVideoId) {
59 const { body } = await doJSONRequest<any>(shareUrl, { activityPub: true })
99b75748 60 if (!body?.actor) throw new Error('Body or body actor is invalid')
49af5ac8
C
61
62 const actorUrl = getAPId(body.actor)
63 if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
64 throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
65 }
66
136d7efd 67 const actor = await getOrCreateAPActor(actorUrl)
49af5ac8
C
68
69 const entry = {
70 actorId: actor.id,
71 videoId: video.id,
72 url: shareUrl
73 }
74
75 await VideoShareModel.upsert(entry)
76}
77
453e83ea 78async function shareByServer (video: MVideo, t: Transaction) {
50d6de9c 79 const serverActor = await getServerActor()
892211e8 80
de94ac86 81 const serverShareUrl = getLocalVideoAnnounceActivityPubUrl(serverActor, video)
5abb9fbb 82 const [ serverShare ] = await VideoShareModel.findOrCreate({
a7977280
C
83 defaults: {
84 actorId: serverActor.id,
85 videoId: video.id,
86 url: serverShareUrl
87 },
88 where: {
89 url: serverShareUrl
90 },
91 transaction: t
a7977280 92 })
5abb9fbb
C
93
94 return sendVideoAnnounce(serverActor, serverShare, video, t)
0f320037 95}
892211e8 96
453e83ea 97async function shareByVideoChannel (video: MVideoAccountLight, t: Transaction) {
de94ac86 98 const videoChannelShareUrl = getLocalVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
5abb9fbb 99 const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
a7977280
C
100 defaults: {
101 actorId: video.VideoChannel.actorId,
102 videoId: video.id,
103 url: videoChannelShareUrl
104 },
105 where: {
106 url: videoChannelShareUrl
107 },
108 transaction: t
a7977280 109 })
5abb9fbb
C
110
111 return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
892211e8
C
112}
113
453e83ea 114async function undoShareByVideoChannel (video: MVideo, oldVideoChannel: MChannelActorLight, t: Transaction) {
0f320037
C
115 // Load old share
116 const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
117 if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
118
119 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
120 await oldShare.destroy({ transaction: t })
892211e8 121}