aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/video-rates.ts
blob: 2e7920f4e2d0d8dc83ea1fdb3800a45c22d0cf17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Transaction } from 'sequelize'
import { VideoRateType } from '../../../shared/models/videos'
import { MAccountActor, MActorUrl, MVideoAccountLight, MVideoFullLight, MVideoId } from '../../types/models'
import { sendLike, sendUndoDislike, sendUndoLike } from './send'
import { sendDislike } from './send/send-dislike'
import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
import { federateVideoIfNeeded } from './videos'

async function sendVideoRateChange (
  account: MAccountActor,
  video: MVideoFullLight,
  likes: number,
  dislikes: number,
  t: Transaction
) {
  if (video.isOwned()) return federateVideoIfNeeded(video, false, t)

  return sendVideoRateChangeToOrigin(account, video, likes, dislikes, t)
}

function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
  return rateType === 'like'
    ? getVideoLikeActivityPubUrlByLocalActor(actor, video)
    : getVideoDislikeActivityPubUrlByLocalActor(actor, video)
}

// ---------------------------------------------------------------------------

export {
  getLocalRateUrl,
  sendVideoRateChange
}

// ---------------------------------------------------------------------------

async function sendVideoRateChangeToOrigin (
  account: MAccountActor,
  video: MVideoAccountLight,
  likes: number,
  dislikes: number,
  t: Transaction
) {
  // Local video, we don't need to send like
  if (video.isOwned()) return

  const actor = account.Actor

  // Keep the order: first we undo and then we create

  // Undo Like
  if (likes < 0) await sendUndoLike(actor, video, t)
  // Undo Dislike
  if (dislikes < 0) await sendUndoDislike(actor, video, t)

  // Like
  if (likes > 0) await sendLike(actor, video, t)
  // Dislike
  if (dislikes > 0) await sendDislike(actor, video, t)
}