1 import { Transaction } from 'sequelize'
2 import { VideoShareModel } from '../../models/video/video-share'
3 import { sendUndoAnnounce, sendVideoAnnounce } from './send'
4 import { getVideoAnnounceActivityPubUrl } from './url'
5 import * as Bluebird from 'bluebird'
6 import { doRequest } from '../../helpers/requests'
7 import { getOrCreateActorAndServerAndModel } from './actor'
8 import { logger } from '../../helpers/logger'
9 import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
10 import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
11 import { MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../types/models/video'
12 import { getServerActor } from '@server/models/application/application'
14 async function shareVideoByServerAndChannel (video: MVideoAccountLight, t: Transaction) {
15 if (!video.hasPrivacyForFederation()) return undefined
18 shareByServer(video, t),
19 shareByVideoChannel(video, t)
23 async function changeVideoChannelShare (
24 video: MVideoAccountLight,
25 oldVideoChannel: MChannelActorLight,
28 logger.info('Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name)
30 await undoShareByVideoChannel(video, oldVideoChannel, t)
32 await shareByVideoChannel(video, t)
35 async function addVideoShares (shareUrls: string[], video: MVideoId) {
36 await Bluebird.map(shareUrls, async shareUrl => {
39 const { body } = await doRequest<any>({
44 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
46 const actorUrl = getAPId(body.actor)
47 if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
48 throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
51 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
59 await VideoShareModel.upsert(entry)
61 logger.warn('Cannot add share %s.', shareUrl, { err })
63 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
67 changeVideoChannelShare,
69 shareVideoByServerAndChannel
72 // ---------------------------------------------------------------------------
74 async function shareByServer (video: MVideo, t: Transaction) {
75 const serverActor = await getServerActor()
77 const serverShareUrl = getVideoAnnounceActivityPubUrl(serverActor, video)
78 const [ serverShare ] = await VideoShareModel.findOrCreate({
80 actorId: serverActor.id,
90 return sendVideoAnnounce(serverActor, serverShare, video, t)
93 async function shareByVideoChannel (video: MVideoAccountLight, t: Transaction) {
94 const videoChannelShareUrl = getVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
95 const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
97 actorId: video.VideoChannel.actorId,
99 url: videoChannelShareUrl
102 url: videoChannelShareUrl
107 return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
110 async function undoShareByVideoChannel (video: MVideo, oldVideoChannel: MChannelActorLight, t: Transaction) {
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)
115 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
116 await oldShare.destroy({ transaction: t })