video-rates.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Transaction } from 'sequelize'
  2. import { AccountModel } from '../../models/account/account'
  3. import { VideoModel } from '../../models/video/video'
  4. import { sendCreateDislike, sendLike, sendUndoDislike, sendUndoLike } from './send'
  5. import { VideoRateType } from '../../../shared/models/videos'
  6. import * as Bluebird from 'bluebird'
  7. import { getOrCreateActorAndServerAndModel } from './actor'
  8. import { AccountVideoRateModel } from '../../models/account/account-video-rate'
  9. import { logger } from '../../helpers/logger'
  10. import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers'
  11. import { doRequest } from '../../helpers/requests'
  12. import { checkUrlsSameHost, getAPUrl } from '../../helpers/activitypub'
  13. import { ActorModel } from '../../models/activitypub/actor'
  14. import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from './url'
  15. async function createRates (ratesUrl: string[], video: VideoModel, rate: VideoRateType) {
  16. let rateCounts = 0
  17. await Bluebird.map(ratesUrl, async rateUrl => {
  18. try {
  19. // Fetch url
  20. const { body } = await doRequest({
  21. uri: rateUrl,
  22. json: true,
  23. activityPub: true
  24. })
  25. if (!body || !body.actor) throw new Error('Body or body actor is invalid')
  26. const actorUrl = getAPUrl(body.actor)
  27. if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
  28. throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
  29. }
  30. if (checkUrlsSameHost(body.id, rateUrl) !== true) {
  31. throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
  32. }
  33. const actor = await getOrCreateActorAndServerAndModel(actorUrl)
  34. const [ , created ] = await AccountVideoRateModel
  35. .findOrCreate({
  36. where: {
  37. videoId: video.id,
  38. accountId: actor.Account.id
  39. },
  40. defaults: {
  41. videoId: video.id,
  42. accountId: actor.Account.id,
  43. type: rate,
  44. url: body.id
  45. }
  46. })
  47. if (created) rateCounts += 1
  48. } catch (err) {
  49. logger.warn('Cannot add rate %s.', rateUrl, { err })
  50. }
  51. }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
  52. logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
  53. // This is "likes" and "dislikes"
  54. if (rateCounts !== 0) await video.increment(rate + 's', { by: rateCounts })
  55. return
  56. }
  57. async function sendVideoRateChange (account: AccountModel,
  58. video: VideoModel,
  59. likes: number,
  60. dislikes: number,
  61. t: Transaction) {
  62. const actor = account.Actor
  63. // Keep the order: first we undo and then we create
  64. // Undo Like
  65. if (likes < 0) await sendUndoLike(actor, video, t)
  66. // Undo Dislike
  67. if (dislikes < 0) await sendUndoDislike(actor, video, t)
  68. // Like
  69. if (likes > 0) await sendLike(actor, video, t)
  70. // Dislike
  71. if (dislikes > 0) await sendCreateDislike(actor, video, t)
  72. }
  73. function getRateUrl (rateType: VideoRateType, actor: ActorModel, video: VideoModel) {
  74. return rateType === 'like' ? getVideoLikeActivityPubUrl(actor, video) : getVideoDislikeActivityPubUrl(actor, video)
  75. }
  76. export {
  77. getRateUrl,
  78. createRates,
  79. sendVideoRateChange
  80. }