aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/video-rates.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/video-rates.ts')
-rw-r--r--server/lib/activitypub/video-rates.ts79
1 files changed, 24 insertions, 55 deletions
diff --git a/server/lib/activitypub/video-rates.ts b/server/lib/activitypub/video-rates.ts
index 04aa5eae9..2e7920f4e 100644
--- a/server/lib/activitypub/video-rates.ts
+++ b/server/lib/activitypub/video-rates.ts
@@ -1,49 +1,21 @@
1import { map } from 'bluebird'
2import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
3import { doJSONRequest } from '@server/helpers/requests'
4import { VideoRateType } from '../../../shared/models/videos' 2import { VideoRateType } from '../../../shared/models/videos'
5import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' 3import { MAccountActor, MActorUrl, MVideoAccountLight, MVideoFullLight, MVideoId } from '../../types/models'
6import { logger, loggerTagsFactory } from '../../helpers/logger'
7import { CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
8import { AccountVideoRateModel } from '../../models/account/account-video-rate'
9import { MAccountActor, MActorUrl, MVideo, MVideoAccountLight, MVideoId } from '../../types/models'
10import { getOrCreateAPActor } from './actors'
11import { sendLike, sendUndoDislike, sendUndoLike } from './send' 4import { sendLike, sendUndoDislike, sendUndoLike } from './send'
12import { sendDislike } from './send/send-dislike' 5import { sendDislike } from './send/send-dislike'
13import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url' 6import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from './url'
14 7import { federateVideoIfNeeded } from './videos'
15const lTags = loggerTagsFactory('ap', 'video-rate', 'create')
16
17async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) {
18 await map(ratesUrl, async rateUrl => {
19 try {
20 await createRate(rateUrl, video, rate)
21 } catch (err) {
22 logger.info('Cannot add rate %s.', rateUrl, { err, ...lTags(rateUrl, video.uuid, video.url) })
23 }
24 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
25}
26 8
27async function sendVideoRateChange ( 9async function sendVideoRateChange (
28 account: MAccountActor, 10 account: MAccountActor,
29 video: MVideoAccountLight, 11 video: MVideoFullLight,
30 likes: number, 12 likes: number,
31 dislikes: number, 13 dislikes: number,
32 t: Transaction 14 t: Transaction
33) { 15) {
34 const actor = account.Actor 16 if (video.isOwned()) return federateVideoIfNeeded(video, false, t)
35 17
36 // Keep the order: first we undo and then we create 18 return sendVideoRateChangeToOrigin(account, video, likes, dislikes, t)
37
38 // Undo Like
39 if (likes < 0) await sendUndoLike(actor, video, t)
40 // Undo Dislike
41 if (dislikes < 0) await sendUndoDislike(actor, video, t)
42
43 // Like
44 if (likes > 0) await sendLike(actor, video, t)
45 // Dislike
46 if (dislikes > 0) await sendDislike(actor, video, t)
47} 19}
48 20
49function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) { 21function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVideoId) {
@@ -56,35 +28,32 @@ function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVid
56 28
57export { 29export {
58 getLocalRateUrl, 30 getLocalRateUrl,
59 createRates,
60 sendVideoRateChange 31 sendVideoRateChange
61} 32}
62 33
63// --------------------------------------------------------------------------- 34// ---------------------------------------------------------------------------
64 35
65async function createRate (rateUrl: string, video: MVideo, rate: VideoRateType) { 36async function sendVideoRateChangeToOrigin (
66 // Fetch url 37 account: MAccountActor,
67 const { body } = await doJSONRequest<any>(rateUrl, { activityPub: true }) 38 video: MVideoAccountLight,
68 if (!body || !body.actor) throw new Error('Body or body actor is invalid') 39 likes: number,
69 40 dislikes: number,
70 const actorUrl = getAPId(body.actor) 41 t: Transaction
71 if (checkUrlsSameHost(actorUrl, rateUrl) !== true) { 42) {
72 throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`) 43 // Local video, we don't need to send like
73 } 44 if (video.isOwned()) return
74 45
75 if (checkUrlsSameHost(body.id, rateUrl) !== true) { 46 const actor = account.Actor
76 throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
77 }
78 47
79 const actor = await getOrCreateAPActor(actorUrl) 48 // Keep the order: first we undo and then we create
80 49
81 const entry = { 50 // Undo Like
82 videoId: video.id, 51 if (likes < 0) await sendUndoLike(actor, video, t)
83 accountId: actor.Account.id, 52 // Undo Dislike
84 type: rate, 53 if (dislikes < 0) await sendUndoDislike(actor, video, t)
85 url: body.id
86 }
87 54
88 // Video "likes"/"dislikes" will be updated by the caller 55 // Like
89 await AccountVideoRateModel.upsert(entry) 56 if (likes > 0) await sendLike(actor, video, t)
57 // Dislike
58 if (dislikes > 0) await sendDislike(actor, video, t)
90} 59}