2
1

process-create.ts 4.1 KB

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