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