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

60 lines
1.8 KiB
TypeScript
Raw Normal View History

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