feature: initial syndication feeds support
[oweals/peertube.git] / server / lib / activitypub / share.ts
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 { sendVideoAnnounce } from './send'
7 import { getAnnounceActivityPubUrl } from './url'
8
9 async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
10   if (video.privacy === VideoPrivacy.PRIVATE) return undefined
11
12   const serverActor = await getServerActor()
13
14   const serverShareUrl = getAnnounceActivityPubUrl(video.url, serverActor)
15   const serverSharePromise = VideoShareModel.findOrCreate({
16     defaults: {
17       actorId: serverActor.id,
18       videoId: video.id,
19       url: serverShareUrl
20     },
21     where: {
22       url: serverShareUrl
23     },
24     transaction: t
25   }).then(([ serverShare, created ]) => {
26     if (created) return sendVideoAnnounce(serverActor, serverShare, video, t)
27
28     return undefined
29   })
30
31   const videoChannelShareUrl = getAnnounceActivityPubUrl(video.url, video.VideoChannel.Actor)
32   const videoChannelSharePromise = VideoShareModel.findOrCreate({
33     defaults: {
34       actorId: video.VideoChannel.actorId,
35       videoId: video.id,
36       url: videoChannelShareUrl
37     },
38     where: {
39       url: videoChannelShareUrl
40     },
41     transaction: t
42   }).then(([ videoChannelShare, created ]) => {
43     if (created) return sendVideoAnnounce(serverActor, videoChannelShare, video, t)
44
45     return undefined
46   })
47
48   return Promise.all([
49     serverSharePromise,
50     videoChannelSharePromise
51   ])
52 }
53
54 export {
55   shareVideoByServerAndChannel
56 }