redundancy.ts 2.1 KB

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