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 { getAnnounceActivityPubUrl } 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'
15 async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
16 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
19 shareByServer(video, t),
20 shareByVideoChannel(video, t)
24 async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
25 logger.info('Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name)
27 await undoShareByVideoChannel(video, oldVideoChannel, t)
29 await shareByVideoChannel(video, t)
32 async function addVideoShares (shareUrls: string[], instance: VideoModel) {
33 await Bluebird.map(shareUrls, async shareUrl => {
36 const { body } = await doRequest({
41 if (!body || !body.actor) throw new Error('Body of body actor is invalid')
43 const actorUrl = body.actor
44 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
52 await VideoShareModel.findOrCreate({
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 = getAnnounceActivityPubUrl(video.url, serverActor)
76 return VideoShareModel.findOrCreate({
78 actorId: serverActor.id,
86 }).then(([ serverShare, created ]) => {
87 if (created) return sendVideoAnnounce(serverActor, serverShare, video, t)
93 async function shareByVideoChannel (video: VideoModel, t: Transaction) {
94 const videoChannelShareUrl = getAnnounceActivityPubUrl(video.url, video.VideoChannel.Actor)
95 return VideoShareModel.findOrCreate({
97 actorId: video.VideoChannel.actorId,
99 url: videoChannelShareUrl
102 url: videoChannelShareUrl
105 }).then(([ videoChannelShare, created ]) => {
106 if (created) return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
112 async function undoShareByVideoChannel (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
114 const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
115 if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
117 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
118 await oldShare.destroy({ transaction: t })