process-dislike.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { ActivityCreate, ActivityDislike } from '../../../../shared'
  2. import { DislikeObject } from '../../../../shared/models/activitypub/objects'
  3. import { retryTransactionWrapper } from '../../../helpers/database-utils'
  4. import { sequelizeTypescript } from '../../../initializers'
  5. import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
  6. import { ActorModel } from '../../../models/activitypub/actor'
  7. import { getOrCreateVideoAndAccountAndChannel } from '../videos'
  8. import { forwardVideoRelatedActivity } from '../send/utils'
  9. import { getVideoDislikeActivityPubUrl } from '../url'
  10. async function processDislikeActivity (activity: ActivityCreate | ActivityDislike, byActor: ActorModel) {
  11. return retryTransactionWrapper(processDislike, activity, byActor)
  12. }
  13. // ---------------------------------------------------------------------------
  14. export {
  15. processDislikeActivity
  16. }
  17. // ---------------------------------------------------------------------------
  18. async function processDislike (activity: ActivityCreate | ActivityDislike, byActor: ActorModel) {
  19. const dislikeObject = activity.type === 'Dislike' ? activity.object : (activity.object as DislikeObject).object
  20. const byAccount = byActor.Account
  21. if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
  22. const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislikeObject })
  23. return sequelizeTypescript.transaction(async t => {
  24. const rate = {
  25. type: 'dislike' as 'dislike',
  26. videoId: video.id,
  27. accountId: byAccount.id
  28. }
  29. const [ , created ] = await AccountVideoRateModel.findOrCreate({
  30. where: rate,
  31. defaults: Object.assign({}, rate, { url: getVideoDislikeActivityPubUrl(byActor, video) }),
  32. transaction: t
  33. })
  34. if (created === true) await video.increment('dislikes', { transaction: t })
  35. if (video.isOwned() && created === true) {
  36. // Don't resend the activity to the sender
  37. const exceptions = [ byActor ]
  38. await forwardVideoRelatedActivity(activity, t, exceptions, video)
  39. }
  40. })
  41. }