]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/share.ts
Optimize video view AP processing
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / share.ts
CommitLineData
892211e8 1import { Transaction } from 'sequelize'
e12a0092 2import { VideoPrivacy } from '../../../shared/models/videos'
da854ddd 3import { getServerActor } from '../../helpers/utils'
3fd3ab2d 4import { VideoModel } from '../../models/video/video'
3fd3ab2d 5import { VideoShareModel } from '../../models/video/video-share'
0f320037 6import { sendUndoAnnounce, sendVideoAnnounce } from './send'
4ba3b8ea 7import { getAnnounceActivityPubUrl } from './url'
0f320037 8import { VideoChannelModel } from '../../models/video/video-channel'
1297eb5d
C
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'
892211e8 14
a7d647c4 15async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
54b38063 16 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
e12a0092 17
0f320037
C
18 return Promise.all([
19 shareByServer(video, t),
20 shareByVideoChannel(video, t)
21 ])
22}
23
24async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
5cf84858
C
25 logger.info('Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name)
26
0f320037
C
27 await undoShareByVideoChannel(video, oldVideoChannel, t)
28
29 await shareByVideoChannel(video, t)
30}
31
1297eb5d
C
32async function addVideoShares (shareUrls: string[], instance: VideoModel) {
33 await Bluebird.map(shareUrls, async shareUrl => {
34 try {
35 // Fetch url
36 const { body } = await doRequest({
37 uri: shareUrl,
38 json: true,
39 activityPub: true
40 })
41 if (!body || !body.actor) throw new Error('Body of body actor is invalid')
42
43 const actorUrl = body.actor
44 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
45
46 const entry = {
47 actorId: actor.id,
48 videoId: instance.id,
49 url: shareUrl
50 }
51
52 await VideoShareModel.findOrCreate({
53 where: {
54 url: shareUrl
55 },
56 defaults: entry
57 })
58 } catch (err) {
59 logger.warn('Cannot add share %s.', shareUrl, { err })
60 }
61 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
62}
63
0f320037
C
64export {
65 changeVideoChannelShare,
1297eb5d 66 addVideoShares,
0f320037
C
67 shareVideoByServerAndChannel
68}
69
70// ---------------------------------------------------------------------------
71
72async function shareByServer (video: VideoModel, t: Transaction) {
50d6de9c 73 const serverActor = await getServerActor()
892211e8 74
4ba3b8ea 75 const serverShareUrl = getAnnounceActivityPubUrl(video.url, serverActor)
0f320037 76 return VideoShareModel.findOrCreate({
a7977280
C
77 defaults: {
78 actorId: serverActor.id,
79 videoId: video.id,
80 url: serverShareUrl
81 },
82 where: {
83 url: serverShareUrl
84 },
85 transaction: t
86 }).then(([ serverShare, created ]) => {
07197db4 87 if (created) return sendVideoAnnounce(serverActor, serverShare, video, t)
a7977280
C
88
89 return undefined
90 })
0f320037 91}
892211e8 92
0f320037 93async function shareByVideoChannel (video: VideoModel, t: Transaction) {
4ba3b8ea 94 const videoChannelShareUrl = getAnnounceActivityPubUrl(video.url, video.VideoChannel.Actor)
0f320037 95 return 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
105 }).then(([ videoChannelShare, created ]) => {
0f320037 106 if (created) return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
a7d647c4 107
a7977280
C
108 return undefined
109 })
892211e8
C
110}
111
0f320037
C
112async function undoShareByVideoChannel (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
113 // Load old share
114 const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
115 if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
116
117 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
118 await oldShare.destroy({ transaction: t })
892211e8 119}