]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/redundancy.ts
Improve target bitrate calculation
[github/Chocobozzz/PeerTube.git] / server / lib / redundancy.ts
1 import { Transaction } from 'sequelize'
2 import { logger } 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 async function removeVideoRedundancy (videoRedundancy: MVideoRedundancyVideo, t?: Transaction) {
12 const serverActor = await getServerActor()
13
14 // Local cache, send undo to remote instances
15 if (videoRedundancy.actorId === serverActor.id) await sendUndoCacheFile(serverActor, videoRedundancy, t)
16
17 await videoRedundancy.destroy({ transaction: t })
18 }
19
20 async function removeRedundanciesOfServer (serverId: number) {
21 const redundancies = await VideoRedundancyModel.listLocalOfServer(serverId)
22
23 for (const redundancy of redundancies) {
24 await removeVideoRedundancy(redundancy)
25 }
26 }
27
28 async function isRedundancyAccepted (activity: Activity, byActor: MActorSignature) {
29 const configAcceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM
30 if (configAcceptFrom === 'nobody') {
31 logger.info('Do not accept remote redundancy %s due instance accept policy.', activity.id)
32 return false
33 }
34
35 if (configAcceptFrom === 'followings') {
36 const serverActor = await getServerActor()
37 const allowed = await ActorFollowModel.isFollowedBy(byActor.id, serverActor.id)
38
39 if (allowed !== true) {
40 logger.info('Do not accept remote redundancy %s because actor %s is not followed by our instance.', activity.id, byActor.url)
41 return false
42 }
43 }
44
45 return true
46 }
47
48 // ---------------------------------------------------------------------------
49
50 export {
51 isRedundancyAccepted,
52 removeRedundanciesOfServer,
53 removeVideoRedundancy
54 }