]>
Commit | Line | Data |
---|---|---|
3fd3ab2d C |
1 | import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub' |
2 | import { DislikeObject } from '../../../../shared/models/activitypub/objects' | |
da854ddd C |
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
4 | import { logger } from '../../../helpers/logger' | |
3fd3ab2d C |
5 | import { sequelizeTypescript } from '../../../initializers' |
6 | import { AccountModel } from '../../../models/account/account' | |
3fd3ab2d | 7 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
50d6de9c C |
8 | import { ActorModel } from '../../../models/activitypub/actor' |
9 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
63c93323 | 10 | import { forwardActivity } from '../send/misc' |
2ccaeeb3 | 11 | import { getOrCreateAccountAndVideoAndChannel } from '../videos' |
54141398 C |
12 | |
13 | async function processUndoActivity (activity: ActivityUndo) { | |
14 | const activityToUndo = activity.object | |
15 | ||
0032ebe9 | 16 | if (activityToUndo.type === 'Like') { |
63c93323 | 17 | return processUndoLike(activity.actor, activity) |
0032ebe9 | 18 | } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { |
63c93323 | 19 | return processUndoDislike(activity.actor, activity) |
0032ebe9 C |
20 | } else if (activityToUndo.type === 'Follow') { |
21 | return processUndoFollow(activity.actor, activityToUndo) | |
54141398 C |
22 | } |
23 | ||
24 | logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id }) | |
25 | ||
26 | return undefined | |
27 | } | |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | ||
31 | export { | |
32 | processUndoActivity | |
33 | } | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
0032ebe9 | 36 | |
50d6de9c | 37 | function processUndoLike (actorUrl: string, activity: ActivityUndo) { |
0032ebe9 | 38 | const options = { |
50d6de9c | 39 | arguments: [ actorUrl, activity ], |
0032ebe9 C |
40 | errorMessage: 'Cannot undo like with many retries.' |
41 | } | |
42 | ||
43 | return retryTransactionWrapper(undoLike, options) | |
44 | } | |
45 | ||
2ccaeeb3 | 46 | async function undoLike (actorUrl: string, activity: ActivityUndo) { |
63c93323 C |
47 | const likeActivity = activity.object as ActivityLike |
48 | ||
2ccaeeb3 C |
49 | const { video } = await getOrCreateAccountAndVideoAndChannel(likeActivity.object) |
50 | ||
3fd3ab2d | 51 | return sequelizeTypescript.transaction(async t => { |
50d6de9c C |
52 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) |
53 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | |
0032ebe9 | 54 | |
3fd3ab2d | 55 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) |
0032ebe9 C |
56 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
57 | ||
58 | await rate.destroy({ transaction: t }) | |
63c93323 | 59 | await video.decrement('likes', { transaction: t }) |
0032ebe9 | 60 | |
63c93323 C |
61 | if (video.isOwned()) { |
62 | // Don't resend the activity to the sender | |
50d6de9c | 63 | const exceptions = [ byAccount.Actor ] |
63c93323 C |
64 | await forwardActivity(activity, t, exceptions) |
65 | } | |
0032ebe9 C |
66 | }) |
67 | } | |
68 | ||
50d6de9c | 69 | function processUndoDislike (actorUrl: string, activity: ActivityUndo) { |
0032ebe9 | 70 | const options = { |
50d6de9c | 71 | arguments: [ actorUrl, activity ], |
0032ebe9 C |
72 | errorMessage: 'Cannot undo dislike with many retries.' |
73 | } | |
74 | ||
75 | return retryTransactionWrapper(undoDislike, options) | |
76 | } | |
77 | ||
2ccaeeb3 | 78 | async function undoDislike (actorUrl: string, activity: ActivityUndo) { |
63c93323 C |
79 | const dislike = activity.object.object as DislikeObject |
80 | ||
2ccaeeb3 C |
81 | const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) |
82 | ||
3fd3ab2d | 83 | return sequelizeTypescript.transaction(async t => { |
50d6de9c C |
84 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) |
85 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | |
0032ebe9 | 86 | |
3fd3ab2d | 87 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) |
0032ebe9 C |
88 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
89 | ||
90 | await rate.destroy({ transaction: t }) | |
63c93323 | 91 | await video.decrement('dislikes', { transaction: t }) |
0032ebe9 | 92 | |
63c93323 C |
93 | if (video.isOwned()) { |
94 | // Don't resend the activity to the sender | |
50d6de9c | 95 | const exceptions = [ byAccount.Actor ] |
63c93323 C |
96 | await forwardActivity(activity, t, exceptions) |
97 | } | |
0032ebe9 C |
98 | }) |
99 | } | |
100 | ||
50d6de9c | 101 | function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) { |
0032ebe9 | 102 | const options = { |
50d6de9c | 103 | arguments: [ actorUrl, followActivity ], |
0032ebe9 C |
104 | errorMessage: 'Cannot undo follow with many retries.' |
105 | } | |
106 | ||
107 | return retryTransactionWrapper(undoFollow, options) | |
108 | } | |
109 | ||
50d6de9c | 110 | function undoFollow (actorUrl: string, followActivity: ActivityFollow) { |
3fd3ab2d | 111 | return sequelizeTypescript.transaction(async t => { |
50d6de9c C |
112 | const follower = await ActorModel.loadByUrl(actorUrl, t) |
113 | const following = await ActorModel.loadByUrl(followActivity.object, t) | |
114 | const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t) | |
0032ebe9 | 115 | |
50d6de9c | 116 | if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`) |
0032ebe9 | 117 | |
50d6de9c | 118 | await actorFollow.destroy({ transaction: t }) |
0032ebe9 C |
119 | |
120 | return undefined | |
121 | }) | |
122 | } |