]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/redundancy.ts
Begin auth plugin support
[github/Chocobozzz/PeerTube.git] / server / lib / redundancy.ts
CommitLineData
c48e82b5
C
1import { VideoRedundancyModel } from '../models/redundancy/video-redundancy'
2import { sendUndoCacheFile } from './activitypub/send'
3import { Transaction } from 'sequelize'
4import { getServerActor } from '../helpers/utils'
8c9e7875
C
5import { MActorSignature, MVideoRedundancyVideo } from '@server/typings/models'
6import { CONFIG } from '@server/initializers/config'
7import { logger } from '@server/helpers/logger'
8import { ActorFollowModel } from '@server/models/activitypub/actor-follow'
9import { Activity } from '@shared/models'
c48e82b5 10
453e83ea 11async function removeVideoRedundancy (videoRedundancy: MVideoRedundancyVideo, t?: Transaction) {
c48e82b5
C
12 const serverActor = await getServerActor()
13
e5565833
C
14 // Local cache, send undo to remote instances
15 if (videoRedundancy.actorId === serverActor.id) await sendUndoCacheFile(serverActor, videoRedundancy, t)
c48e82b5
C
16
17 await videoRedundancy.destroy({ transaction: t })
18}
19
b764380a
C
20async function removeRedundanciesOfServer (serverId: number) {
21 const redundancies = await VideoRedundancyModel.listLocalOfServer(serverId)
161b061d 22
b764380a 23 for (const redundancy of redundancies) {
161b061d
C
24 await removeVideoRedundancy(redundancy)
25 }
26}
27
8c9e7875
C
28async 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
c48e82b5
C
48// ---------------------------------------------------------------------------
49
50export {
8c9e7875 51 isRedundancyAccepted,
b764380a 52 removeRedundanciesOfServer,
c48e82b5
C
53 removeVideoRedundancy
54}