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