]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/redundancy.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / redundancy.ts
1 import { Transaction } from 'sequelize'
2 import { logger, loggerTagsFactory } from '@server/helpers/logger'
3 import { CONFIG } from '@server/initializers/config'
4 import { ActorFollowModel } from '@server/models/actor/actor-follow'
5 import { getServerActor } from '@server/models/application/application'
6 import { MActorSignature, MVideoRedundancyVideo } from '@server/types/models'
7 import { Activity } from '@shared/models'
8 import { VideoRedundancyModel } from '../models/redundancy/video-redundancy'
9 import { sendUndoCacheFile } from './activitypub/send'
10
11 const lTags = loggerTagsFactory('redundancy')
12
13 async function removeVideoRedundancy (videoRedundancy: MVideoRedundancyVideo, t?: Transaction) {
14 const serverActor = await getServerActor()
15
16 // Local cache, send undo to remote instances
17 if (videoRedundancy.actorId === serverActor.id) await sendUndoCacheFile(serverActor, videoRedundancy, t)
18
19 await videoRedundancy.destroy({ transaction: t })
20 }
21
22 async function removeRedundanciesOfServer (serverId: number) {
23 const redundancies = await VideoRedundancyModel.listLocalOfServer(serverId)
24
25 for (const redundancy of redundancies) {
26 await removeVideoRedundancy(redundancy)
27 }
28 }
29
30 async function isRedundancyAccepted (activity: Activity, byActor: MActorSignature) {
31 const configAcceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM
32 if (configAcceptFrom === 'nobody') {
33 logger.info('Do not accept remote redundancy %s due instance accept policy.', activity.id, lTags())
34 return false
35 }
36
37 if (configAcceptFrom === 'followings') {
38 const serverActor = await getServerActor()
39 const allowed = await ActorFollowModel.isFollowedBy(byActor.id, serverActor.id)
40
41 if (allowed !== true) {
42 logger.info(
43 'Do not accept remote redundancy %s because actor %s is not followed by our instance.',
44 activity.id, byActor.url, lTags()
45 )
46 return false
47 }
48 }
49
50 return true
51 }
52
53 // ---------------------------------------------------------------------------
54
55 export {
56 isRedundancyAccepted,
57 removeRedundanciesOfServer,
58 removeVideoRedundancy
59 }