2
1

redundancy.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { VideoRedundancyModel } from '../models/redundancy/video-redundancy'
  2. import { sendUndoCacheFile } from './activitypub/send'
  3. import { Transaction } from 'sequelize'
  4. import { MActorSignature, MVideoRedundancyVideo } from '@server/types/models'
  5. import { CONFIG } from '@server/initializers/config'
  6. import { logger } from '@server/helpers/logger'
  7. import { ActorFollowModel } from '@server/models/activitypub/actor-follow'
  8. import { Activity } from '@shared/models'
  9. import { getServerActor } from '@server/models/application/application'
  10. async function removeVideoRedundancy (videoRedundancy: MVideoRedundancyVideo, t?: Transaction) {
  11. const serverActor = await getServerActor()
  12. // Local cache, send undo to remote instances
  13. if (videoRedundancy.actorId === serverActor.id) await sendUndoCacheFile(serverActor, videoRedundancy, t)
  14. await videoRedundancy.destroy({ transaction: t })
  15. }
  16. async function removeRedundanciesOfServer (serverId: number) {
  17. const redundancies = await VideoRedundancyModel.listLocalOfServer(serverId)
  18. for (const redundancy of redundancies) {
  19. await removeVideoRedundancy(redundancy)
  20. }
  21. }
  22. async function isRedundancyAccepted (activity: Activity, byActor: MActorSignature) {
  23. const configAcceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM
  24. if (configAcceptFrom === 'nobody') {
  25. logger.info('Do not accept remote redundancy %s due instance accept policy.', activity.id)
  26. return false
  27. }
  28. if (configAcceptFrom === 'followings') {
  29. const serverActor = await getServerActor()
  30. const allowed = await ActorFollowModel.isFollowedBy(byActor.id, serverActor.id)
  31. if (allowed !== true) {
  32. logger.info('Do not accept remote redundancy %s because actor %s is not followed by our instance.', activity.id, byActor.url)
  33. return false
  34. }
  35. }
  36. return true
  37. }
  38. // ---------------------------------------------------------------------------
  39. export {
  40. isRedundancyAccepted,
  41. removeRedundanciesOfServer,
  42. removeVideoRedundancy
  43. }