]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/share.ts
Fix playlist more button with long video names
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / share.ts
... / ...
CommitLineData
1import { Transaction } from 'sequelize'
2import { VideoPrivacy } from '../../../shared/models/videos'
3import { getServerActor } from '../../helpers/utils'
4import { VideoModel } from '../../models/video/video'
5import { VideoShareModel } from '../../models/video/video-share'
6import { sendUndoAnnounce, sendVideoAnnounce } from './send'
7import { getVideoAnnounceActivityPubUrl } from './url'
8import { VideoChannelModel } from '../../models/video/video-channel'
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/constants'
14import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
15
16async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
17 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
18
19 return Promise.all([
20 shareByServer(video, t),
21 shareByVideoChannel(video, t)
22 ])
23}
24
25async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
26 logger.info('Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name)
27
28 await undoShareByVideoChannel(video, oldVideoChannel, t)
29
30 await shareByVideoChannel(video, t)
31}
32
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 })
42 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
43
44 const actorUrl = getAPId(body.actor)
45 if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
46 throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
47 }
48
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.upsert(entry)
58 } catch (err) {
59 logger.warn('Cannot add share %s.', shareUrl, { err })
60 }
61 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
62}
63
64export {
65 changeVideoChannelShare,
66 addVideoShares,
67 shareVideoByServerAndChannel
68}
69
70// ---------------------------------------------------------------------------
71
72async function shareByServer (video: VideoModel, t: Transaction) {
73 const serverActor = await getServerActor()
74
75 const serverShareUrl = getVideoAnnounceActivityPubUrl(serverActor, video)
76 const [ serverShare ] = await VideoShareModel.findOrCreate({
77 defaults: {
78 actorId: serverActor.id,
79 videoId: video.id,
80 url: serverShareUrl
81 },
82 where: {
83 url: serverShareUrl
84 },
85 transaction: t
86 })
87
88 return sendVideoAnnounce(serverActor, serverShare, video, t)
89}
90
91async function shareByVideoChannel (video: VideoModel, t: Transaction) {
92 const videoChannelShareUrl = getVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
93 const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
94 defaults: {
95 actorId: video.VideoChannel.actorId,
96 videoId: video.id,
97 url: videoChannelShareUrl
98 },
99 where: {
100 url: videoChannelShareUrl
101 },
102 transaction: t
103 })
104
105 return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
106}
107
108async function undoShareByVideoChannel (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
109 // Load old share
110 const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
111 if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
112
113 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
114 await oldShare.destroy({ transaction: t })
115}