1 import { Transaction } from 'sequelize'
2 import { VideoPrivacy } from '../../../shared/models/videos'
3 import { getServerActor } from '../../helpers/utils'
4 import { VideoModel } from '../../models/video/video'
5 import { VideoShareModel } from '../../models/video/video-share'
6 import { sendUndoAnnounce, sendVideoAnnounce } from './send'
7 import { getVideoAnnounceActivityPubUrl } from './url'
8 import { VideoChannelModel } from '../../models/video/video-channel'
9 import * as Bluebird from 'bluebird'
10 import { doRequest } from '../../helpers/requests'
11 import { getOrCreateActorAndServerAndModel } from './actor'
12 import { logger } from '../../helpers/logger'
13 import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
14 import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
16 async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
17 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
20 shareByServer(video, t),
21 shareByVideoChannel(video, t)
25 async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
26 logger.info('Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name)
28 await undoShareByVideoChannel(video, oldVideoChannel, t)
30 await shareByVideoChannel(video, t)
33 async function addVideoShares (shareUrls: string[], instance: VideoModel) {
34 await Bluebird.map(shareUrls, async shareUrl => {
37 const { body } = await doRequest({
42 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
44 const actorUrl = getAPId(body.actor)
45 if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
46 throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
49 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
57 await VideoShareModel.upsert(entry)
59 logger.warn('Cannot add share %s.', shareUrl, { err })
61 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
65 changeVideoChannelShare,
67 shareVideoByServerAndChannel
70 // ---------------------------------------------------------------------------
72 async function shareByServer (video: VideoModel, t: Transaction) {
73 const serverActor = await getServerActor()
75 const serverShareUrl = getVideoAnnounceActivityPubUrl(serverActor, video)
76 const [ serverShare ] = await VideoShareModel.findOrCreate({
78 actorId: serverActor.id,
88 return sendVideoAnnounce(serverActor, serverShare, video, t)
91 async function shareByVideoChannel (video: VideoModel, t: Transaction) {
92 const videoChannelShareUrl = getVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
93 const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
95 actorId: video.VideoChannel.actorId,
97 url: videoChannelShareUrl
100 url: videoChannelShareUrl
105 return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
108 async function undoShareByVideoChannel (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
110 const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
111 if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
113 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
114 await oldShare.destroy({ transaction: t })