diff options
author | Chocobozzz <me@florianbigard.com> | 2018-05-11 15:10:13 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-05-11 15:25:51 +0200 |
commit | 0f320037e689b2778959c12ddd4ce790f6e4ae4f (patch) | |
tree | 425acaa4345442388901c833275bb76b42a8a268 /server/lib/activitypub/share.ts | |
parent | 9675333decd0b89b73a4fc67b39272f7296bfe3f (diff) | |
download | PeerTube-0f320037e689b2778959c12ddd4ce790f6e4ae4f.tar.gz PeerTube-0f320037e689b2778959c12ddd4ce790f6e4ae4f.tar.zst PeerTube-0f320037e689b2778959c12ddd4ce790f6e4ae4f.zip |
Add ability to update a video channel
Diffstat (limited to 'server/lib/activitypub/share.ts')
-rw-r--r-- | server/lib/activitypub/share.ts | 45 |
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' | |||
3 | import { getServerActor } from '../../helpers/utils' | 3 | import { getServerActor } from '../../helpers/utils' |
4 | import { VideoModel } from '../../models/video/video' | 4 | import { VideoModel } from '../../models/video/video' |
5 | import { VideoShareModel } from '../../models/video/video-share' | 5 | import { VideoShareModel } from '../../models/video/video-share' |
6 | import { sendVideoAnnounce } from './send' | 6 | import { sendUndoAnnounce, sendVideoAnnounce } from './send' |
7 | import { getAnnounceActivityPubUrl } from './url' | 7 | import { getAnnounceActivityPubUrl } from './url' |
8 | import { VideoChannelModel } from '../../models/video/video-channel' | ||
8 | 9 | ||
9 | async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) { | 10 | async 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 | |||
19 | async function changeVideoChannelShare (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) { | ||
20 | await undoShareByVideoChannel(video, oldVideoChannel, t) | ||
21 | |||
22 | await shareByVideoChannel(video, t) | ||
23 | } | ||
24 | |||
25 | export { | ||
26 | changeVideoChannelShare, | ||
27 | shareVideoByServerAndChannel | ||
28 | } | ||
29 | |||
30 | // --------------------------------------------------------------------------- | ||
31 | |||
32 | async 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 | ||
53 | async 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 | ||
54 | export { | 72 | async 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 | } |