diff options
Diffstat (limited to 'server/server/lib/activitypub/video-rates.ts')
-rw-r--r-- | server/server/lib/activitypub/video-rates.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/server/server/lib/activitypub/video-rates.ts b/server/server/lib/activitypub/video-rates.ts new file mode 100644 index 000000000..1c0d5ec14 --- /dev/null +++ b/server/server/lib/activitypub/video-rates.ts | |||
@@ -0,0 +1,59 @@ | |||
1 | import { Transaction } from 'sequelize' | ||
2 | import { VideoRateType } from '@peertube/peertube-models' | ||
3 | import { MAccountActor, MActorUrl, MVideoAccountLight, MVideoFullLight, MVideoId } from '../../types/models/index.js' | ||
4 | import { sendLike, sendUndoDislike, sendUndoLike } from './send/index.js' | ||
5 | import { sendDislike } from './send/send-dislike.js' | ||
6 | import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url.js' | ||
7 | import { federateVideoIfNeeded } from './videos/index.js' | ||
8 | |||
9 | async function sendVideoRateChange ( | ||
10 | account: MAccountActor, | ||
11 | video: MVideoFullLight, | ||
12 | likes: number, | ||
13 | dislikes: number, | ||
14 | t: Transaction | ||
15 | ) { | ||
16 | if (video.isOwned()) return federateVideoIfNeeded(video, false, t) | ||
17 | |||
18 | return sendVideoRateChangeToOrigin(account, video, likes, dislikes, t) | ||
19 | } | ||
20 | |||
21 | function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) { | ||
22 | return rateType === 'like' | ||
23 | ? getVideoLikeActivityPubUrlByLocalActor(actor, video) | ||
24 | : getVideoDislikeActivityPubUrlByLocalActor(actor, video) | ||
25 | } | ||
26 | |||
27 | // --------------------------------------------------------------------------- | ||
28 | |||
29 | export { | ||
30 | getLocalRateUrl, | ||
31 | sendVideoRateChange | ||
32 | } | ||
33 | |||
34 | // --------------------------------------------------------------------------- | ||
35 | |||
36 | async function sendVideoRateChangeToOrigin ( | ||
37 | account: MAccountActor, | ||
38 | video: MVideoAccountLight, | ||
39 | likes: number, | ||
40 | dislikes: number, | ||
41 | t: Transaction | ||
42 | ) { | ||
43 | // Local video, we don't need to send like | ||
44 | if (video.isOwned()) return | ||
45 | |||
46 | const actor = account.Actor | ||
47 | |||
48 | // Keep the order: first we undo and then we create | ||
49 | |||
50 | // Undo Like | ||
51 | if (likes < 0) await sendUndoLike(actor, video, t) | ||
52 | // Undo Dislike | ||
53 | if (dislikes < 0) await sendUndoDislike(actor, video, t) | ||
54 | |||
55 | // Like | ||
56 | if (likes > 0) await sendLike(actor, video, t) | ||
57 | // Dislike | ||
58 | if (dislikes > 0) await sendDislike(actor, video, t) | ||
59 | } | ||