PeerTube/server/lib/activitypub/video-rates.ts

60 lines
1.8 KiB
TypeScript
Raw Normal View History

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