aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/video-rates.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-03 14:30:09 +0200
committerChocobozzz <me@florianbigard.com>2021-06-03 16:40:32 +0200
commit49af5ac8c2653cb0ef23479c9d3256c5b724d49d (patch)
tree6783df1833b13e141cfd5dc0177531887c4a4e2e /server/lib/activitypub/video-rates.ts
parent9777fe9eebe53debdf45091cab98f72a5987e05a (diff)
downloadPeerTube-49af5ac8c2653cb0ef23479c9d3256c5b724d49d.tar.gz
PeerTube-49af5ac8c2653cb0ef23479c9d3256c5b724d49d.tar.zst
PeerTube-49af5ac8c2653cb0ef23479c9d3256c5b724d49d.zip
Refactor AP playlists
Diffstat (limited to 'server/lib/activitypub/video-rates.ts')
-rw-r--r--server/lib/activitypub/video-rates.ts56
1 files changed, 32 insertions, 24 deletions
diff --git a/server/lib/activitypub/video-rates.ts b/server/lib/activitypub/video-rates.ts
index f40c07fea..091f4ec23 100644
--- a/server/lib/activitypub/video-rates.ts
+++ b/server/lib/activitypub/video-rates.ts
@@ -15,30 +15,7 @@ import { getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlBy
15async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) { 15async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateType) {
16 await Bluebird.map(ratesUrl, async rateUrl => { 16 await Bluebird.map(ratesUrl, async rateUrl => {
17 try { 17 try {
18 // Fetch url 18 await createRate(rateUrl, video, rate)
19 const { body } = await doJSONRequest<any>(rateUrl, { activityPub: true })
20 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
21
22 const actorUrl = getAPId(body.actor)
23 if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
24 throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
25 }
26
27 if (checkUrlsSameHost(body.id, rateUrl) !== true) {
28 throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
29 }
30
31 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
32
33 const entry = {
34 videoId: video.id,
35 accountId: actor.Account.id,
36 type: rate,
37 url: body.id
38 }
39
40 // Video "likes"/"dislikes" will be updated by the caller
41 await AccountVideoRateModel.upsert(entry)
42 } catch (err) { 19 } catch (err) {
43 logger.warn('Cannot add rate %s.', rateUrl, { err }) 20 logger.warn('Cannot add rate %s.', rateUrl, { err })
44 } 21 }
@@ -73,8 +50,39 @@ function getLocalRateUrl (rateType: VideoRateType, actor: MActorUrl, video: MVid
73 : getVideoDislikeActivityPubUrlByLocalActor(actor, video) 50 : getVideoDislikeActivityPubUrlByLocalActor(actor, video)
74} 51}
75 52
53// ---------------------------------------------------------------------------
54
76export { 55export {
77 getLocalRateUrl, 56 getLocalRateUrl,
78 createRates, 57 createRates,
79 sendVideoRateChange 58 sendVideoRateChange
80} 59}
60
61// ---------------------------------------------------------------------------
62
63async function createRate (rateUrl: string, video: MVideo, rate: VideoRateType) {
64 // Fetch url
65 const { body } = await doJSONRequest<any>(rateUrl, { activityPub: true })
66 if (!body || !body.actor) throw new Error('Body or body actor is invalid')
67
68 const actorUrl = getAPId(body.actor)
69 if (checkUrlsSameHost(actorUrl, rateUrl) !== true) {
70 throw new Error(`Rate url ${rateUrl} has not the same host than actor url ${actorUrl}`)
71 }
72
73 if (checkUrlsSameHost(body.id, rateUrl) !== true) {
74 throw new Error(`Rate url ${rateUrl} host is different from the AP object id ${body.id}`)
75 }
76
77 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
78
79 const entry = {
80 videoId: video.id,
81 accountId: actor.Account.id,
82 type: rate,
83 url: body.id
84 }
85
86 // Video "likes"/"dislikes" will be updated by the caller
87 await AccountVideoRateModel.upsert(entry)
88}