diff options
Diffstat (limited to 'server/lib/activitypub/process/process-undo.ts')
-rw-r--r-- | server/lib/activitypub/process/process-undo.ts | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index c091d9678..73ca0a17c 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts | |||
@@ -1,10 +1,8 @@ | |||
1 | import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub' | 1 | import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub' |
2 | import { DislikeObject } from '../../../../shared/models/activitypub/objects' | 2 | import { DislikeObject } from '../../../../shared/models/activitypub/objects' |
3 | import { getActorUrl } from '../../../helpers/activitypub' | ||
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
5 | import { logger } from '../../../helpers/logger' | 4 | import { logger } from '../../../helpers/logger' |
6 | import { sequelizeTypescript } from '../../../initializers' | 5 | import { sequelizeTypescript } from '../../../initializers' |
7 | import { AccountModel } from '../../../models/account/account' | ||
8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | 6 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
9 | import { ActorModel } from '../../../models/activitypub/actor' | 7 | import { ActorModel } from '../../../models/activitypub/actor' |
10 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | 8 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' |
@@ -16,15 +14,13 @@ import { VideoRedundancyModel } from '../../../models/redundancy/video-redundanc | |||
16 | async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) { | 14 | async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) { |
17 | const activityToUndo = activity.object | 15 | const activityToUndo = activity.object |
18 | 16 | ||
19 | const actorUrl = getActorUrl(activity.actor) | ||
20 | |||
21 | if (activityToUndo.type === 'Like') { | 17 | if (activityToUndo.type === 'Like') { |
22 | return retryTransactionWrapper(processUndoLike, actorUrl, activity) | 18 | return retryTransactionWrapper(processUndoLike, byActor, activity) |
23 | } | 19 | } |
24 | 20 | ||
25 | if (activityToUndo.type === 'Create') { | 21 | if (activityToUndo.type === 'Create') { |
26 | if (activityToUndo.object.type === 'Dislike') { | 22 | if (activityToUndo.object.type === 'Dislike') { |
27 | return retryTransactionWrapper(processUndoDislike, actorUrl, activity) | 23 | return retryTransactionWrapper(processUndoDislike, byActor, activity) |
28 | } else if (activityToUndo.object.type === 'CacheFile') { | 24 | } else if (activityToUndo.object.type === 'CacheFile') { |
29 | return retryTransactionWrapper(processUndoCacheFile, byActor, activity) | 25 | return retryTransactionWrapper(processUndoCacheFile, byActor, activity) |
30 | } | 26 | } |
@@ -51,48 +47,46 @@ export { | |||
51 | 47 | ||
52 | // --------------------------------------------------------------------------- | 48 | // --------------------------------------------------------------------------- |
53 | 49 | ||
54 | async function processUndoLike (actorUrl: string, activity: ActivityUndo) { | 50 | async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) { |
55 | const likeActivity = activity.object as ActivityLike | 51 | const likeActivity = activity.object as ActivityLike |
56 | 52 | ||
57 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object }) | 53 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object }) |
58 | 54 | ||
59 | return sequelizeTypescript.transaction(async t => { | 55 | return sequelizeTypescript.transaction(async t => { |
60 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) | 56 | if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) |
61 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | ||
62 | 57 | ||
63 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) | 58 | const rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t) |
64 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | 59 | if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`) |
65 | 60 | ||
66 | await rate.destroy({ transaction: t }) | 61 | await rate.destroy({ transaction: t }) |
67 | await video.decrement('likes', { transaction: t }) | 62 | await video.decrement('likes', { transaction: t }) |
68 | 63 | ||
69 | if (video.isOwned()) { | 64 | if (video.isOwned()) { |
70 | // Don't resend the activity to the sender | 65 | // Don't resend the activity to the sender |
71 | const exceptions = [ byAccount.Actor ] | 66 | const exceptions = [ byActor ] |
72 | 67 | ||
73 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | 68 | await forwardVideoRelatedActivity(activity, t, exceptions, video) |
74 | } | 69 | } |
75 | }) | 70 | }) |
76 | } | 71 | } |
77 | 72 | ||
78 | async function processUndoDislike (actorUrl: string, activity: ActivityUndo) { | 73 | async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) { |
79 | const dislike = activity.object.object as DislikeObject | 74 | const dislike = activity.object.object as DislikeObject |
80 | 75 | ||
81 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) | 76 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) |
82 | 77 | ||
83 | return sequelizeTypescript.transaction(async t => { | 78 | return sequelizeTypescript.transaction(async t => { |
84 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) | 79 | if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) |
85 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | ||
86 | 80 | ||
87 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) | 81 | const rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t) |
88 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | 82 | if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`) |
89 | 83 | ||
90 | await rate.destroy({ transaction: t }) | 84 | await rate.destroy({ transaction: t }) |
91 | await video.decrement('dislikes', { transaction: t }) | 85 | await video.decrement('dislikes', { transaction: t }) |
92 | 86 | ||
93 | if (video.isOwned()) { | 87 | if (video.isOwned()) { |
94 | // Don't resend the activity to the sender | 88 | // Don't resend the activity to the sender |
95 | const exceptions = [ byAccount.Actor ] | 89 | const exceptions = [ byActor ] |
96 | 90 | ||
97 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | 91 | await forwardVideoRelatedActivity(activity, t, exceptions, video) |
98 | } | 92 | } |
@@ -108,6 +102,8 @@ async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo | |||
108 | const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id) | 102 | const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id) |
109 | if (!cacheFile) throw new Error('Unknown video cache ' + cacheFile.url) | 103 | if (!cacheFile) throw new Error('Unknown video cache ' + cacheFile.url) |
110 | 104 | ||
105 | if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.') | ||
106 | |||
111 | await cacheFile.destroy() | 107 | await cacheFile.destroy() |
112 | 108 | ||
113 | if (video.isOwned()) { | 109 | if (video.isOwned()) { |