]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/share.ts
Add ability to search a video with an URL
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / share.ts
CommitLineData
892211e8 1import { Transaction } from 'sequelize'
e12a0092 2import { VideoPrivacy } from '../../../shared/models/videos'
da854ddd 3import { getServerActor } from '../../helpers/utils'
3fd3ab2d 4import { VideoModel } from '../../models/video/video'
3fd3ab2d 5import { VideoShareModel } from '../../models/video/video-share'
0f320037 6import { sendUndoAnnounce, sendVideoAnnounce } from './send'
4ba3b8ea 7import { getAnnounceActivityPubUrl } from './url'
0f320037 8import { VideoChannelModel } from '../../models/video/video-channel'
892211e8 9
a7d647c4 10async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
54b38063 11 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
e12a0092 12
0f320037
C
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) {
50d6de9c 33 const serverActor = await getServerActor()
892211e8 34
4ba3b8ea 35 const serverShareUrl = getAnnounceActivityPubUrl(video.url, serverActor)
0f320037 36 return VideoShareModel.findOrCreate({
a7977280
C
37 defaults: {
38 actorId: serverActor.id,
39 videoId: video.id,
40 url: serverShareUrl
41 },
42 where: {
43 url: serverShareUrl
44 },
45 transaction: t
46 }).then(([ serverShare, created ]) => {
07197db4 47 if (created) return sendVideoAnnounce(serverActor, serverShare, video, t)
a7977280
C
48
49 return undefined
50 })
0f320037 51}
892211e8 52
0f320037 53async function shareByVideoChannel (video: VideoModel, t: Transaction) {
4ba3b8ea 54 const videoChannelShareUrl = getAnnounceActivityPubUrl(video.url, video.VideoChannel.Actor)
0f320037 55 return VideoShareModel.findOrCreate({
a7977280
C
56 defaults: {
57 actorId: video.VideoChannel.actorId,
58 videoId: video.id,
59 url: videoChannelShareUrl
60 },
61 where: {
62 url: videoChannelShareUrl
63 },
64 transaction: t
65 }).then(([ videoChannelShare, created ]) => {
0f320037 66 if (created) return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
a7d647c4 67
a7977280
C
68 return undefined
69 })
892211e8
C
70}
71
0f320037
C
72async function undoShareByVideoChannel (video: VideoModel, oldVideoChannel: VideoChannelModel, t: Transaction) {
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 })
892211e8 79}