2018-01-10 17:18:12 +01:00
|
|
|
import { Transaction } from 'sequelize'
|
2018-08-22 16:15:35 +02:00
|
|
|
import { VideoRateType } from '../../../shared/models/videos'
|
2022-03-18 11:17:35 +01:00
|
|
|
import { MAccountActor, MActorUrl, MVideoAccountLight, MVideoFullLight, MVideoId } from '../../types/models'
|
2021-03-08 14:24:11 +01:00
|
|
|
import { sendLike, sendUndoDislike, sendUndoLike } from './send'
|
|
|
|
import { sendDislike } from './send/send-dislike'
|
|
|
|
import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
|
2022-03-18 11:17:35 +01:00
|
|
|
import { federateVideoIfNeeded } from './videos'
|
2018-01-10 17:18:12 +01:00
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
async function sendVideoRateChange (
|
|
|
|
account: MAccountActor,
|
2022-03-18 11:17:35 +01:00
|
|
|
video: MVideoFullLight,
|
2019-08-15 11:53:26 +02:00
|
|
|
likes: number,
|
|
|
|
dislikes: number,
|
|
|
|
t: Transaction
|
|
|
|
) {
|
2022-03-18 11:17:35 +01:00
|
|
|
if (video.isOwned()) return federateVideoIfNeeded(video, false, t)
|
2018-01-10 17:18:12 +01:00
|
|
|
|
2022-03-18 11:17:35 +01:00
|
|
|
return sendVideoRateChangeToOrigin(account, video, likes, dislikes, t)
|
2018-01-10 17:18:12 +01:00
|
|
|
}
|
|
|
|
|
2020-11-20 11:21:08 +01:00
|
|
|
function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
|
2019-08-15 11:53:26 +02:00
|
|
|
return rateType === 'like'
|
2020-11-20 11:21:08 +01:00
|
|
|
? getVideoLikeActivityPubUrlByLocalActor(actor, video)
|
|
|
|
: getVideoDislikeActivityPubUrlByLocalActor(actor, video)
|
2018-11-14 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
2021-06-03 14:30:09 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-01-10 17:18:12 +01:00
|
|
|
export {
|
2020-11-20 11:21:08 +01:00
|
|
|
getLocalRateUrl,
|
2018-03-27 13:33:56 +02:00
|
|
|
sendVideoRateChange
|
2018-01-10 17:18:12 +01:00
|
|
|
}
|
2021-06-03 14:30:09 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-03-18 11:17:35 +01:00
|
|
|
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
|
2021-06-03 14:30:09 +02:00
|
|
|
|
2022-03-18 11:17:35 +01:00
|
|
|
const actor = account.Actor
|
2021-06-03 14:30:09 +02:00
|
|
|
|
2022-03-18 11:17:35 +01:00
|
|
|
// Keep the order: first we undo and then we create
|
2021-06-03 14:30:09 +02:00
|
|
|
|
2022-03-18 11:17:35 +01:00
|
|
|
// Undo Like
|
|
|
|
if (likes < 0) await sendUndoLike(actor, video, t)
|
|
|
|
// Undo Dislike
|
|
|
|
if (dislikes < 0) await sendUndoDislike(actor, video, t)
|
2021-06-03 14:30:09 +02:00
|
|
|
|
2022-03-18 11:17:35 +01:00
|
|
|
// Like
|
|
|
|
if (likes > 0) await sendLike(actor, video, t)
|
|
|
|
// Dislike
|
|
|
|
if (dislikes > 0) await sendDislike(actor, video, t)
|
2021-06-03 14:30:09 +02:00
|
|
|
}
|