diff options
Diffstat (limited to 'server/lib/activitypub/process/process-undo.ts')
-rw-r--r-- | server/lib/activitypub/process/process-undo.ts | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index 9fe066c01..cc221045f 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts | |||
@@ -3,16 +3,15 @@ import { DislikeObject } from '../../../../shared/models/activitypub/objects/dis | |||
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
4 | import { logger } from '../../../helpers/logger' | 4 | import { logger } from '../../../helpers/logger' |
5 | import { database as db } from '../../../initializers' | 5 | import { database as db } from '../../../initializers' |
6 | import { sendUndoDislikeToVideoFollowers } from '../index' | 6 | import { forwardActivity } from '../send/misc' |
7 | import { sendUndoLikeToVideoFollowers } from '../send/send-undo' | ||
8 | 7 | ||
9 | async function processUndoActivity (activity: ActivityUndo) { | 8 | async function processUndoActivity (activity: ActivityUndo) { |
10 | const activityToUndo = activity.object | 9 | const activityToUndo = activity.object |
11 | 10 | ||
12 | if (activityToUndo.type === 'Like') { | 11 | if (activityToUndo.type === 'Like') { |
13 | return processUndoLike(activity.actor, activityToUndo) | 12 | return processUndoLike(activity.actor, activity) |
14 | } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { | 13 | } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { |
15 | return processUndoDislike(activity.actor, activityToUndo.object) | 14 | return processUndoDislike(activity.actor, activity) |
16 | } else if (activityToUndo.type === 'Follow') { | 15 | } else if (activityToUndo.type === 'Follow') { |
17 | return processUndoFollow(activity.actor, activityToUndo) | 16 | return processUndoFollow(activity.actor, activityToUndo) |
18 | } | 17 | } |
@@ -30,57 +29,69 @@ export { | |||
30 | 29 | ||
31 | // --------------------------------------------------------------------------- | 30 | // --------------------------------------------------------------------------- |
32 | 31 | ||
33 | function processUndoLike (actor: string, likeActivity: ActivityLike) { | 32 | function processUndoLike (actor: string, activity: ActivityUndo) { |
34 | const options = { | 33 | const options = { |
35 | arguments: [ actor, likeActivity ], | 34 | arguments: [ actor, activity ], |
36 | errorMessage: 'Cannot undo like with many retries.' | 35 | errorMessage: 'Cannot undo like with many retries.' |
37 | } | 36 | } |
38 | 37 | ||
39 | return retryTransactionWrapper(undoLike, options) | 38 | return retryTransactionWrapper(undoLike, options) |
40 | } | 39 | } |
41 | 40 | ||
42 | function undoLike (actor: string, likeActivity: ActivityLike) { | 41 | function undoLike (actor: string, activity: ActivityUndo) { |
42 | const likeActivity = activity.object as ActivityLike | ||
43 | |||
43 | return db.sequelize.transaction(async t => { | 44 | return db.sequelize.transaction(async t => { |
44 | const byAccount = await db.Account.loadByUrl(actor, t) | 45 | const byAccount = await db.Account.loadByUrl(actor, t) |
45 | if (!byAccount) throw new Error('Unknown account ' + actor) | 46 | if (!byAccount) throw new Error('Unknown account ' + actor) |
46 | 47 | ||
47 | const video = await db.Video.loadByUrlAndPopulateAccount(likeActivity.object) | 48 | const video = await db.Video.loadByUrlAndPopulateAccount(likeActivity.object, t) |
48 | if (!video) throw new Error('Unknown video ' + likeActivity.actor) | 49 | if (!video) throw new Error('Unknown video ' + likeActivity.actor) |
49 | 50 | ||
50 | const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) | 51 | const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) |
51 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | 52 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
52 | 53 | ||
53 | await rate.destroy({ transaction: t }) | 54 | await rate.destroy({ transaction: t }) |
54 | await video.decrement('likes') | 55 | await video.decrement('likes', { transaction: t }) |
55 | 56 | ||
56 | if (video.isOwned()) await sendUndoLikeToVideoFollowers(byAccount, video, t) | 57 | if (video.isOwned()) { |
58 | // Don't resend the activity to the sender | ||
59 | const exceptions = [ byAccount ] | ||
60 | await forwardActivity(activity, t, exceptions) | ||
61 | } | ||
57 | }) | 62 | }) |
58 | } | 63 | } |
59 | 64 | ||
60 | function processUndoDislike (actor: string, dislikeCreateActivity: DislikeObject) { | 65 | function processUndoDislike (actor: string, activity: ActivityUndo) { |
61 | const options = { | 66 | const options = { |
62 | arguments: [ actor, dislikeCreateActivity ], | 67 | arguments: [ actor, activity ], |
63 | errorMessage: 'Cannot undo dislike with many retries.' | 68 | errorMessage: 'Cannot undo dislike with many retries.' |
64 | } | 69 | } |
65 | 70 | ||
66 | return retryTransactionWrapper(undoDislike, options) | 71 | return retryTransactionWrapper(undoDislike, options) |
67 | } | 72 | } |
68 | 73 | ||
69 | function undoDislike (actor: string, dislike: DislikeObject) { | 74 | function undoDislike (actor: string, activity: ActivityUndo) { |
75 | const dislike = activity.object.object as DislikeObject | ||
76 | |||
70 | return db.sequelize.transaction(async t => { | 77 | return db.sequelize.transaction(async t => { |
71 | const byAccount = await db.Account.loadByUrl(actor, t) | 78 | const byAccount = await db.Account.loadByUrl(actor, t) |
72 | if (!byAccount) throw new Error('Unknown account ' + actor) | 79 | if (!byAccount) throw new Error('Unknown account ' + actor) |
73 | 80 | ||
74 | const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object) | 81 | const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t) |
75 | if (!video) throw new Error('Unknown video ' + dislike.actor) | 82 | if (!video) throw new Error('Unknown video ' + dislike.actor) |
76 | 83 | ||
77 | const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) | 84 | const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) |
78 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | 85 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
79 | 86 | ||
80 | await rate.destroy({ transaction: t }) | 87 | await rate.destroy({ transaction: t }) |
81 | await video.decrement('dislikes') | 88 | await video.decrement('dislikes', { transaction: t }) |
82 | 89 | ||
83 | if (video.isOwned()) await sendUndoDislikeToVideoFollowers(byAccount, video, t) | 90 | if (video.isOwned()) { |
91 | // Don't resend the activity to the sender | ||
92 | const exceptions = [ byAccount ] | ||
93 | await forwardActivity(activity, t, exceptions) | ||
94 | } | ||
84 | }) | 95 | }) |
85 | } | 96 | } |
86 | 97 | ||