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