]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/redundancy.ts
Add test on AP hooks
[github/Chocobozzz/PeerTube.git] / server / lib / redundancy.ts
CommitLineData
c48e82b5 1import { Transaction } from 'sequelize'
52b1fd15 2import { logger, loggerTagsFactory } from '@server/helpers/logger'
7d9ba5c0
C
3import { CONFIG } from '@server/initializers/config'
4import { ActorFollowModel } from '@server/models/actor/actor-follow'
8dc8a34e 5import { getServerActor } from '@server/models/application/application'
7d9ba5c0
C
6import { MActorSignature, MVideoRedundancyVideo } from '@server/types/models'
7import { Activity } from '@shared/models'
8import { VideoRedundancyModel } from '../models/redundancy/video-redundancy'
9import { sendUndoCacheFile } from './activitypub/send'
c48e82b5 10
52b1fd15
C
11const lTags = loggerTagsFactory('redundancy')
12
453e83ea 13async function removeVideoRedundancy (videoRedundancy: MVideoRedundancyVideo, t?: Transaction) {
c48e82b5
C
14 const serverActor = await getServerActor()
15
e5565833
C
16 // Local cache, send undo to remote instances
17 if (videoRedundancy.actorId === serverActor.id) await sendUndoCacheFile(serverActor, videoRedundancy, t)
c48e82b5
C
18
19 await videoRedundancy.destroy({ transaction: t })
20}
21
b764380a
C
22async function removeRedundanciesOfServer (serverId: number) {
23 const redundancies = await VideoRedundancyModel.listLocalOfServer(serverId)
161b061d 24
b764380a 25 for (const redundancy of redundancies) {
161b061d
C
26 await removeVideoRedundancy(redundancy)
27 }
28}
29
8c9e7875
C
30async function isRedundancyAccepted (activity: Activity, byActor: MActorSignature) {
31 const configAcceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM
32 if (configAcceptFrom === 'nobody') {
52b1fd15 33 logger.info('Do not accept remote redundancy %s due instance accept policy.', activity.id, lTags())
8c9e7875
C
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) {
52b1fd15
C
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 )
8c9e7875
C
46 return false
47 }
48 }
49
50 return true
51}
52
c48e82b5
C
53// ---------------------------------------------------------------------------
54
55export {
8c9e7875 56 isRedundancyAccepted,
b764380a 57 removeRedundanciesOfServer,
c48e82b5
C
58 removeVideoRedundancy
59}