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