1
0
Fork 0
peertube/server/lib/activitypub/video-rates.ts

60 lines
1.8 KiB
TypeScript
Raw Normal View History

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