process-flag.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { ActivityCreate, ActivityFlag, VideoAbuseState } from '../../../../shared'
  2. import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects'
  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 { VideoAbuseModel } from '../../../models/video/video-abuse'
  8. import { getOrCreateVideoAndAccountAndChannel } from '../videos'
  9. import { Notifier } from '../../notifier'
  10. import { getAPId } from '../../../helpers/activitypub'
  11. async function processFlagActivity (activity: ActivityCreate | ActivityFlag, byActor: ActorModel) {
  12. return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor)
  13. }
  14. // ---------------------------------------------------------------------------
  15. export {
  16. processFlagActivity
  17. }
  18. // ---------------------------------------------------------------------------
  19. async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: ActorModel) {
  20. const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject)
  21. logger.debug('Reporting remote abuse for video %s.', getAPId(flag.object))
  22. const account = byActor.Account
  23. if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
  24. const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: flag.object })
  25. return sequelizeTypescript.transaction(async t => {
  26. const videoAbuseData = {
  27. reporterAccountId: account.id,
  28. reason: flag.content,
  29. videoId: video.id,
  30. state: VideoAbuseState.PENDING
  31. }
  32. const videoAbuseInstance = await VideoAbuseModel.create(videoAbuseData, { transaction: t })
  33. videoAbuseInstance.Video = video
  34. Notifier.Instance.notifyOnNewVideoAbuse(videoAbuseInstance)
  35. logger.info('Remote abuse for video uuid %s created', flag.object)
  36. })
  37. }