process-create.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
  2. import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
  3. import { retryTransactionWrapper } from '../../../helpers/database-utils'
  4. import { logger } from '../../../helpers/logger'
  5. import { sequelizeTypescript } from '../../../initializers/database'
  6. import { resolveThread } from '../video-comments'
  7. import { getOrCreateVideoAndAccountAndChannel } from '../videos'
  8. import { forwardVideoRelatedActivity } from '../send/utils'
  9. import { createOrUpdateCacheFile } from '../cache-file'
  10. import { Notifier } from '../../notifier'
  11. import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
  12. import { createOrUpdateVideoPlaylist } from '../playlist'
  13. import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
  14. import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../typings/models'
  15. import { isRedundancyAccepted } from '@server/lib/redundancy'
  16. async function processCreateActivity (options: APProcessorOptions<ActivityCreate>) {
  17. const { activity, byActor } = options
  18. // Only notify if it is not from a fetcher job
  19. const notify = options.fromFetch !== true
  20. const activityObject = activity.object
  21. const activityType = activityObject.type
  22. if (activityType === 'Video') {
  23. return processCreateVideo(activity, notify)
  24. }
  25. if (activityType === 'Note') {
  26. return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
  27. }
  28. if (activityType === 'CacheFile') {
  29. return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
  30. }
  31. if (activityType === 'Playlist') {
  32. return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
  33. }
  34. logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
  35. return Promise.resolve(undefined)
  36. }
  37. // ---------------------------------------------------------------------------
  38. export {
  39. processCreateActivity
  40. }
  41. // ---------------------------------------------------------------------------
  42. async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
  43. const videoToCreateData = activity.object as VideoTorrentObject
  44. const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
  45. if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
  46. return video
  47. }
  48. async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
  49. if (await isRedundancyAccepted(activity, byActor) !== true) return
  50. const cacheFile = activity.object as CacheFileObject
  51. const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
  52. await sequelizeTypescript.transaction(async t => {
  53. return createOrUpdateCacheFile(cacheFile, video, byActor, t)
  54. })
  55. if (video.isOwned()) {
  56. // Don't resend the activity to the sender
  57. const exceptions = [ byActor ]
  58. await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
  59. }
  60. }
  61. async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
  62. const commentObject = activity.object as VideoCommentObject
  63. const byAccount = byActor.Account
  64. if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
  65. let video: MVideoAccountLightBlacklistAllFiles
  66. let created: boolean
  67. let comment: MCommentOwnerVideo
  68. try {
  69. const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
  70. video = resolveThreadResult.video
  71. created = resolveThreadResult.commentCreated
  72. comment = resolveThreadResult.comment
  73. } catch (err) {
  74. logger.debug(
  75. 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
  76. commentObject.inReplyTo,
  77. { err }
  78. )
  79. return
  80. }
  81. if (video.isOwned() && created === true) {
  82. // Don't resend the activity to the sender
  83. const exceptions = [ byActor ]
  84. await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
  85. }
  86. if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
  87. }
  88. async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
  89. const playlistObject = activity.object as PlaylistObject
  90. const byAccount = byActor.Account
  91. if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
  92. await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
  93. }