]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/share.ts
Add auto follow back support for instances
[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 { VideoShareModel } from '../../models/video/video-share'
0f320037 5import { sendUndoAnnounce, sendVideoAnnounce } from './send'
5c6d985f 6import { getVideoAnnounceActivityPubUrl } from './url'
1297eb5d
C
7import * as Bluebird from 'bluebird'
8import { doRequest } from '../../helpers/requests'
9import { getOrCreateActorAndServerAndModel } from './actor'
10import { logger } from '../../helpers/logger'
74dc3bca 11import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
848f499d 12import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
453e83ea 13import { MChannelActor, MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../typings/models/video'
892211e8 14
453e83ea 15async function shareVideoByServerAndChannel (video: MVideoAccountLight, 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
453e83ea
C
24async function changeVideoChannelShare (
25 video: MVideoAccountLight,
26 oldVideoChannel: MChannelActorLight,
27 t: Transaction
28) {
5cf84858
C
29 logger.info('Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name)
30
0f320037
C
31 await undoShareByVideoChannel(video, oldVideoChannel, t)
32
33 await shareByVideoChannel(video, t)
34}
35
453e83ea 36async function addVideoShares (shareUrls: string[], video: MVideoId) {
1297eb5d
C
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 })
5c6d985f
C
45 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
46
848f499d 47 const actorUrl = getAPId(body.actor)
5c6d985f
C
48 if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
49 throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
50 }
1297eb5d 51
1297eb5d
C
52 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
53
54 const entry = {
55 actorId: actor.id,
453e83ea 56 videoId: video.id,
1297eb5d
C
57 url: shareUrl
58 }
59
2ba92871 60 await VideoShareModel.upsert(entry)
1297eb5d
C
61 } catch (err) {
62 logger.warn('Cannot add share %s.', shareUrl, { err })
63 }
64 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
65}
66
0f320037
C
67export {
68 changeVideoChannelShare,
1297eb5d 69 addVideoShares,
0f320037
C
70 shareVideoByServerAndChannel
71}
72
73// ---------------------------------------------------------------------------
74
453e83ea 75async function shareByServer (video: MVideo, t: Transaction) {
50d6de9c 76 const serverActor = await getServerActor()
892211e8 77
5c6d985f 78 const serverShareUrl = getVideoAnnounceActivityPubUrl(serverActor, video)
5abb9fbb 79 const [ serverShare ] = await VideoShareModel.findOrCreate({
a7977280
C
80 defaults: {
81 actorId: serverActor.id,
82 videoId: video.id,
83 url: serverShareUrl
84 },
85 where: {
86 url: serverShareUrl
87 },
88 transaction: t
a7977280 89 })
5abb9fbb
C
90
91 return sendVideoAnnounce(serverActor, serverShare, video, t)
0f320037 92}
892211e8 93
453e83ea 94async function shareByVideoChannel (video: MVideoAccountLight, t: Transaction) {
5c6d985f 95 const videoChannelShareUrl = getVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
5abb9fbb 96 const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
a7977280
C
97 defaults: {
98 actorId: video.VideoChannel.actorId,
99 videoId: video.id,
100 url: videoChannelShareUrl
101 },
102 where: {
103 url: videoChannelShareUrl
104 },
105 transaction: t
a7977280 106 })
5abb9fbb
C
107
108 return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
892211e8
C
109}
110
453e83ea 111async function undoShareByVideoChannel (video: MVideo, oldVideoChannel: MChannelActorLight, t: Transaction) {
0f320037
C
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 })
892211e8 118}