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