video-rates.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Transaction } from 'sequelize'
  2. import { sendLike, sendUndoDislike, sendUndoLike } from './send'
  3. import { VideoRateType } from '../../../shared/models/videos'
  4. import * as Bluebird from 'bluebird'
  5. import { getOrCreateActorAndServerAndModel } from './actor'
  6. import { AccountVideoRateModel } from '../../models/account/account-video-rate'
  7. import { logger } from '../../helpers/logger'
  8. import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
  9. import { doRequest } from '../../helpers/requests'
  10. import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
  11. import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
  12. import { sendDislike } from './send/send-dislike'
  13. import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models'
  14. async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) {
  15. await Bluebird.map(ratesUrl, async rateUrl => {
  16. try {
  17. // Fetch url
  18. const { body } = await doRequest<any>({
  19. uri: rateUrl,
  20. json: true,
  21. activityPub: true
  22. })
  23. if (!body || !body.actor) throw new Error('Body or body actor is invalid')
  24. const actorUrl = getAPId(body.actor)
  25. if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
  26. throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
  27. }
  28. if (checkUrlsSameHost(body.id, rateUrl) !== true) {
  29. throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
  30. }
  31. const actor = await getOrCreateActorAndServerAndModel(actorUrl)
  32. const entry = {
  33. videoId: video.id,
  34. accountId: actor.Account.id,
  35. type: rate,
  36. url: body.id
  37. }
  38. // Video "likes"/"dislikes" will be updated by the caller
  39. await AccountVideoRateModel.upsert(entry)
  40. } catch (err) {
  41. logger.warn('Cannot add rate %s.', rateUrl, { err })
  42. }
  43. }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
  44. }
  45. async function sendVideoRateChange (
  46. account: MAccountActor,
  47. video: MVideoAccountLight,
  48. likes: number,
  49. dislikes: number,
  50. t: Transaction
  51. ) {
  52. const actor = account.Actor
  53. // Keep the order: first we undo and then we create
  54. // Undo Like
  55. if (likes < 0) await sendUndoLike(actor, video, t)
  56. // Undo Dislike
  57. if (dislikes < 0) await sendUndoDislike(actor, video, t)
  58. // Like
  59. if (likes > 0) await sendLike(actor, video, t)
  60. // Dislike
  61. if (dislikes > 0) await sendDislike(actor, video, t)
  62. }
  63. function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
  64. return rateType === 'like'
  65. ? getVideoLikeActivityPubUrlByLocalActor(actor, video)
  66. : getVideoDislikeActivityPubUrlByLocalActor(actor, video)
  67. }
  68. export {
  69. getLocalRateUrl,
  70. createRates,
  71. sendVideoRateChange
  72. }