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