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.ts120
1 files changed, 0 insertions, 120 deletions
diff --git a/server/lib/activitypub/share.ts b/server/lib/activitypub/share.ts
deleted file mode 100644
index 792a73f2a..000000000
--- a/server/lib/activitypub/share.ts
+++ /dev/null
@@ -1,120 +0,0 @@
1import { map } from 'bluebird'
2import { Transaction } from 'sequelize'
3import { getServerActor } from '@server/models/application/application'
4import { logger, loggerTagsFactory } from '../../helpers/logger'
5import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
6import { VideoShareModel } from '../../models/video/video-share'
7import { MChannelActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../types/models/video'
8import { fetchAP, getAPId } from './activity'
9import { getOrCreateAPActor } from './actors'
10import { sendUndoAnnounce, sendVideoAnnounce } from './send'
11import { checkUrlsSameHost, getLocalVideoAnnounceActivityPubUrl } from './url'
12
13const lTags = loggerTagsFactory('share')
14
15async function shareVideoByServerAndChannel (video: MVideoAccountLight, t: Transaction) {
16 if (!video.hasPrivacyForFederation()) return undefined
17
18 return Promise.all([
19 shareByServer(video, t),
20 shareByVideoChannel(video, t)
21 ])
22}
23
24async function changeVideoChannelShare (
25 video: MVideoAccountLight,
26 oldVideoChannel: MChannelActorLight,
27 t: Transaction
28) {
29 logger.info(
30 'Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name,
31 lTags(video.uuid)
32 )
33
34 await undoShareByVideoChannel(video, oldVideoChannel, t)
35
36 await shareByVideoChannel(video, t)
37}
38
39async function addVideoShares (shareUrls: string[], video: MVideoId) {
40 await map(shareUrls, async shareUrl => {
41 try {
42 await addVideoShare(shareUrl, video)
43 } catch (err) {
44 logger.warn('Cannot add share %s.', shareUrl, { err })
45 }
46 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
47}
48
49export {
50 changeVideoChannelShare,
51 addVideoShares,
52 shareVideoByServerAndChannel
53}
54
55// ---------------------------------------------------------------------------
56
57async function addVideoShare (shareUrl: string, video: MVideoId) {
58 const { body } = await fetchAP<any>(shareUrl)
59 if (!body?.actor) throw new Error('Body or body actor is invalid')
60
61 const actorUrl = getAPId(body.actor)
62 if (checkUrlsSameHost(shareUrl, actorUrl) !== true) {
63 throw new Error(`Actor url ${actorUrl} has not the same host than the share url ${shareUrl}`)
64 }
65
66 const actor = await getOrCreateAPActor(actorUrl)
67
68 const entry = {
69 actorId: actor.id,
70 videoId: video.id,
71 url: shareUrl
72 }
73
74 await VideoShareModel.upsert(entry)
75}
76
77async function shareByServer (video: MVideo, t: Transaction) {
78 const serverActor = await getServerActor()
79
80 const serverShareUrl = getLocalVideoAnnounceActivityPubUrl(serverActor, video)
81 const [ serverShare ] = await VideoShareModel.findOrCreate({
82 defaults: {
83 actorId: serverActor.id,
84 videoId: video.id,
85 url: serverShareUrl
86 },
87 where: {
88 url: serverShareUrl
89 },
90 transaction: t
91 })
92
93 return sendVideoAnnounce(serverActor, serverShare, video, t)
94}
95
96async function shareByVideoChannel (video: MVideoAccountLight, t: Transaction) {
97 const videoChannelShareUrl = getLocalVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
98 const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
99 defaults: {
100 actorId: video.VideoChannel.actorId,
101 videoId: video.id,
102 url: videoChannelShareUrl
103 },
104 where: {
105 url: videoChannelShareUrl
106 },
107 transaction: t
108 })
109
110 return sendVideoAnnounce(video.VideoChannel.Actor, videoChannelShare, video, t)
111}
112
113async function undoShareByVideoChannel (video: MVideo, oldVideoChannel: MChannelActorLight, t: Transaction) {
114 // Load old share
115 const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
116 if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
117
118 await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
119 await oldShare.destroy({ transaction: t })
120}