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'
10 async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
11 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
14 shareByServer(video, t),
15 shareByVideoChannel(video, t)
19 async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
20 await undoShareByVideoChannel(video, oldVideoChannel, t)
22 await shareByVideoChannel(video, t)
26 changeVideoChannelShare,
27 shareVideoByServerAndChannel
30 // ---------------------------------------------------------------------------
32 async function shareByServer (video: VideoModel, t: Transaction) {
33 const serverActor = await getServerActor()
35 const serverShareUrl = getAnnounceActivityPubUrl(video.url, serverActor)
36 return VideoShareModel.findOrCreate({
38 actorId: serverActor.id,
46 }).then(([ serverShare, created ]) => {
47 if (created) return sendVideoAnnounce(serverActor, serverShare, video, t)
53 async function shareByVideoChannel (video: VideoModel, t: Transaction) {
54 const videoChannelShareUrl = getAnnounceActivityPubUrl(video.url, video.VideoChannel.Actor)
55 return VideoShareModel.findOrCreate({
57 actorId: video.VideoChannel.actorId,
59 url: videoChannelShareUrl
62 url: videoChannelShareUrl
65 }).then(([ videoChannelShare, created ]) => {
66 if (created) return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
72 async function undoShareByVideoChannel (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
74 const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
75 if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
77 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
78 await oldShare.destroy({ transaction: t })