1 import { map } from 'bluebird'
2 import { Transaction } from 'sequelize'
3 import { getServerActor } from '@server/models/application/application'
4 import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
5 import { logger, loggerTagsFactory } from '../../helpers/logger'
6 import { doJSONRequest } from '../../helpers/requests'
7 import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
8 import { VideoShareModel } from '../../models/video/video-share'
9 import { MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../types/models/video'
10 import { getOrCreateAPActor } from './actors'
11 import { sendUndoAnnounce, sendVideoAnnounce } from './send'
12 import { getLocalVideoAnnounceActivityPubUrl } from './url'
14 const lTags = loggerTagsFactory('share')
16 async function shareVideoByServerAndChannel (video: MVideoAccountLight, t: Transaction) {
17 if (!video.hasPrivacyForFederation()) return undefined
20 shareByServer(video, t),
21 shareByVideoChannel(video, t)
25 async function changeVideoChannelShare (
26 video: MVideoAccountLight,
27 oldVideoChannel: MChannelActorLight,
31 'Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name,
35 await undoShareByVideoChannel(video, oldVideoChannel, t)
37 await shareByVideoChannel(video, t)
40 async function addVideoShares (shareUrls: string[], video: MVideoId) {
41 await map(shareUrls, async shareUrl => {
43 await addVideoShare(shareUrl, video)
45 logger.warn('Cannot add share %s.', shareUrl, { err })
47 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
51 changeVideoChannelShare,
53 shareVideoByServerAndChannel
56 // ---------------------------------------------------------------------------
58 async function addVideoShare (shareUrl: string, video: MVideoId) {
59 const { body } = await doJSONRequest<any>(shareUrl, { activityPub: true })
60 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
62 const actorUrl = getAPId(body.actor)
63 if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
64 throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
67 const actor = await getOrCreateAPActor(actorUrl)
75 await VideoShareModel.upsert(entry)
78 async function shareByServer (video: MVideo, t: Transaction) {
79 const serverActor = await getServerActor()
81 const serverShareUrl = getLocalVideoAnnounceActivityPubUrl(serverActor, video)
82 const [ serverShare ] = await VideoShareModel.findOrCreate({
84 actorId: serverActor.id,
94 return sendVideoAnnounce(serverActor, serverShare, video, t)
97 async function shareByVideoChannel (video: MVideoAccountLight, t: Transaction) {
98 const videoChannelShareUrl = getLocalVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
99 const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
101 actorId: video.VideoChannel.actorId,
103 url: videoChannelShareUrl
106 url: videoChannelShareUrl
111 return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
114 async function undoShareByVideoChannel (video: MVideo, oldVideoChannel: MChannelActorLight, t: Transaction) {
116 const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
117 if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
119 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
120 await oldShare.destroy({ transaction: t })