1 import { Transaction } from 'sequelize'
2 import { VideoPrivacy } from '../../../shared/models/videos'
3 import { getServerActor } from '../../helpers/utils'
4 import { VideoShareModel } from '../../models/video/video-share'
5 import { sendUndoAnnounce, sendVideoAnnounce } from './send'
6 import { getVideoAnnounceActivityPubUrl } from './url'
7 import * as Bluebird from 'bluebird'
8 import { doRequest } from '../../helpers/requests'
9 import { getOrCreateActorAndServerAndModel } from './actor'
10 import { logger } from '../../helpers/logger'
11 import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
12 import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
13 import { MChannelActor, MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../typings/models/video'
15 async function shareVideoByServerAndChannel (video: MVideoAccountLight, t: Transaction) {
16 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
19 shareByServer(video, t),
20 shareByVideoChannel(video, t)
24 async function changeVideoChannelShare (
25 video: MVideoAccountLight,
26 oldVideoChannel: MChannelActorLight,
29 logger.info('Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name)
31 await undoShareByVideoChannel(video, oldVideoChannel, t)
33 await shareByVideoChannel(video, t)
36 async function addVideoShares (shareUrls: string[], video: MVideoId) {
37 await Bluebird.map(shareUrls, async shareUrl => {
40 const { body } = await doRequest({
45 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
47 const actorUrl = getAPId(body.actor)
48 if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
49 throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
52 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
60 await VideoShareModel.upsert(entry)
62 logger.warn('Cannot add share %s.', shareUrl, { err })
64 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
68 changeVideoChannelShare,
70 shareVideoByServerAndChannel
73 // ---------------------------------------------------------------------------
75 async function shareByServer (video: MVideo, t: Transaction) {
76 const serverActor = await getServerActor()
78 const serverShareUrl = getVideoAnnounceActivityPubUrl(serverActor, video)
79 const [ serverShare ] = await VideoShareModel.findOrCreate({
81 actorId: serverActor.id,
91 return sendVideoAnnounce(serverActor, serverShare, video, t)
94 async function shareByVideoChannel (video: MVideoAccountLight, t: Transaction) {
95 const videoChannelShareUrl = getVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
96 const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
98 actorId: video.VideoChannel.actorId,
100 url: videoChannelShareUrl
103 url: videoChannelShareUrl
108 return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
111 async function undoShareByVideoChannel (video: MVideo, oldVideoChannel: MChannelActorLight, t: Transaction) {
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)
116 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
117 await oldShare.destroy({ transaction: t })