federate.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { forceNumber } from '@peertube/peertube-core-utils'
  2. import { VideoPrivacy, VideoPrivacyType, VideoState, VideoStateType } from '@peertube/peertube-models'
  3. import { CONFIG } from '@server/initializers/config.js'
  4. import { MVideoAPLight, MVideoWithBlacklistRights } from '@server/types/models/index.js'
  5. import { Transaction } from 'sequelize'
  6. import { sendCreateVideo, sendUpdateVideo } from '../send/index.js'
  7. import { shareByServer, shareByVideoChannel } from '../share.js'
  8. export async function federateVideoIfNeeded (videoArg: MVideoAPLight, isNewVideo: boolean, transaction?: Transaction) {
  9. if (!canVideoBeFederated(videoArg, isNewVideo)) return
  10. const video = await videoArg.lightAPToFullAP(transaction)
  11. if (isNewVideo) {
  12. // Now we'll add the video's meta data to our followers
  13. await sendCreateVideo(video, transaction)
  14. await Promise.all([
  15. shareByServer(video, transaction),
  16. shareByVideoChannel(video, transaction)
  17. ])
  18. } else {
  19. await sendUpdateVideo(video, transaction)
  20. }
  21. }
  22. export function canVideoBeFederated (video: MVideoWithBlacklistRights, isNewVideo = false) {
  23. // Check this is not a blacklisted video
  24. if (video.isBlacklisted() === true) {
  25. if (isNewVideo === false) return false
  26. if (video.VideoBlacklist.unfederated === true) return false
  27. }
  28. // Check the video is public/unlisted and published
  29. return isPrivacyForFederation(video.privacy) && isStateForFederation(video.state)
  30. }
  31. export function isNewVideoPrivacyForFederation (currentPrivacy: VideoPrivacyType, newPrivacy: VideoPrivacyType) {
  32. return !isPrivacyForFederation(currentPrivacy) && isPrivacyForFederation(newPrivacy)
  33. }
  34. export function isPrivacyForFederation (privacy: VideoPrivacyType) {
  35. const castedPrivacy = forceNumber(privacy)
  36. return castedPrivacy === VideoPrivacy.PUBLIC ||
  37. (CONFIG.FEDERATION.VIDEOS.FEDERATE_UNLISTED === true && castedPrivacy === VideoPrivacy.UNLISTED)
  38. }
  39. export function isStateForFederation (state: VideoStateType) {
  40. const castedState = forceNumber(state)
  41. return castedState === VideoState.PUBLISHED || castedState === VideoState.WAITING_FOR_LIVE || castedState === VideoState.LIVE_ENDED
  42. }