aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/share.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/share.ts')
-rw-r--r--server/lib/activitypub/share.ts45
1 files changed, 34 insertions, 11 deletions
diff --git a/server/lib/activitypub/share.ts b/server/lib/activitypub/share.ts
index f256f8d21..698414867 100644
--- a/server/lib/activitypub/share.ts
+++ b/server/lib/activitypub/share.ts
@@ -3,16 +3,37 @@ import { VideoPrivacy } from '../../../shared/models/videos'
3import { getServerActor } from '../../helpers/utils' 3import { getServerActor } from '../../helpers/utils'
4import { VideoModel } from '../../models/video/video' 4import { VideoModel } from '../../models/video/video'
5import { VideoShareModel } from '../../models/video/video-share' 5import { VideoShareModel } from '../../models/video/video-share'
6import { sendVideoAnnounce } from './send' 6import { sendUndoAnnounce, sendVideoAnnounce } from './send'
7import { getAnnounceActivityPubUrl } from './url' 7import { getAnnounceActivityPubUrl } from './url'
8import { VideoChannelModel } from '../../models/video/video-channel'
8 9
9async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) { 10async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
10 if (video.privacy === VideoPrivacy.PRIVATE) return undefined 11 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
11 12
13 return Promise.all([
14 shareByServer(video, t),
15 shareByVideoChannel(video, t)
16 ])
17}
18
19async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
20 await undoShareByVideoChannel(video, oldVideoChannel, t)
21
22 await shareByVideoChannel(video, t)
23}
24
25export {
26 changeVideoChannelShare,
27 shareVideoByServerAndChannel
28}
29
30// ---------------------------------------------------------------------------
31
32async function shareByServer (video: VideoModel, t: Transaction) {
12 const serverActor = await getServerActor() 33 const serverActor = await getServerActor()
13 34
14 const serverShareUrl = getAnnounceActivityPubUrl(video.url, serverActor) 35 const serverShareUrl = getAnnounceActivityPubUrl(video.url, serverActor)
15 const serverSharePromise = VideoShareModel.findOrCreate({ 36 return VideoShareModel.findOrCreate({
16 defaults: { 37 defaults: {
17 actorId: serverActor.id, 38 actorId: serverActor.id,
18 videoId: video.id, 39 videoId: video.id,
@@ -27,9 +48,11 @@ async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction)
27 48
28 return undefined 49 return undefined
29 }) 50 })
51}
30 52
53async function shareByVideoChannel (video: VideoModel, t: Transaction) {
31 const videoChannelShareUrl = getAnnounceActivityPubUrl(video.url, video.VideoChannel.Actor) 54 const videoChannelShareUrl = getAnnounceActivityPubUrl(video.url, video.VideoChannel.Actor)
32 const videoChannelSharePromise = VideoShareModel.findOrCreate({ 55 return VideoShareModel.findOrCreate({
33 defaults: { 56 defaults: {
34 actorId: video.VideoChannel.actorId, 57 actorId: video.VideoChannel.actorId,
35 videoId: video.id, 58 videoId: video.id,
@@ -40,17 +63,17 @@ async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction)
40 }, 63 },
41 transaction: t 64 transaction: t
42 }).then(([ videoChannelShare, created ]) => { 65 }).then(([ videoChannelShare, created ]) => {
43 if (created) return sendVideoAnnounce(serverActor, videoChannelShare, video, t) 66 if (created) return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
44 67
45 return undefined 68 return undefined
46 }) 69 })
47
48 return Promise.all([
49 serverSharePromise,
50 videoChannelSharePromise
51 ])
52} 70}
53 71
54export { 72async function undoShareByVideoChannel (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
55 shareVideoByServerAndChannel 73 // Load old share
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)
76
77 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
78 await oldShare.destroy({ transaction: t })
56} 79}