watch-action.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { arrayify } from '@peertube/peertube-core-utils'
  2. import { WatchActionObject } from '@peertube/peertube-models'
  3. import { isDateValid, isUUIDValid } from '../misc.js'
  4. import { isVideoTimeValid } from '../video-view.js'
  5. import { isActivityPubVideoDurationValid, isObjectValid } from './misc.js'
  6. function isWatchActionObjectValid (action: WatchActionObject) {
  7. if (!action || action.type !== 'WatchAction') return false
  8. // TODO: compat with < 6.1, remove in 7.0
  9. if (!action.uuid && action['identifier']) action.uuid = action['identifier']
  10. if (action['_:actionStatus'] && !action.actionStatus) action.actionStatus = action['_:actionStatus']
  11. if (action['_:watchSections'] && !action.watchSections) action.watchSections = arrayify(action['_:watchSections'])
  12. return isObjectValid(action.id) &&
  13. isActivityPubVideoDurationValid(action.duration) &&
  14. isDateValid(action.startTime) &&
  15. isDateValid(action.endTime) &&
  16. isLocationValid(action.location) &&
  17. isUUIDValid(action.uuid) &&
  18. isObjectValid(action.object) &&
  19. areWatchSectionsValid(action.watchSections)
  20. }
  21. // ---------------------------------------------------------------------------
  22. export {
  23. isWatchActionObjectValid
  24. }
  25. // ---------------------------------------------------------------------------
  26. function isLocationValid (location: any) {
  27. if (!location) return true
  28. if (typeof location !== 'object') return false
  29. if (location.addressCountry && typeof location.addressCountry !== 'string') return false
  30. if (location.addressRegion && typeof location.addressRegion !== 'string') return false
  31. return true
  32. }
  33. function areWatchSectionsValid (sections: WatchActionObject['watchSections']) {
  34. return Array.isArray(sections) && sections.every(s => {
  35. // TODO: compat with < 6.1, remove in 7.0
  36. if (s['_:endTimestamp'] && !s.endTimestamp) s.endTimestamp = s['_:endTimestamp']
  37. return isVideoTimeValid(s.startTimestamp) && isVideoTimeValid(s.endTimestamp)
  38. })
  39. }