]>
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 { VideoModel } from '../../../models/video/video' | |
11 | import { forwardActivity } from '../send/misc' | |
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 | function undoLike (actorUrl: string, activity: ActivityUndo) { | |
47 | const likeActivity = activity.object as ActivityLike | |
48 | ||
49 | return sequelizeTypescript.transaction(async t => { | |
50 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) | |
51 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | |
52 | ||
53 | const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t) | |
54 | if (!video) throw new Error('Unknown video ' + likeActivity.actor) | |
55 | ||
56 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) | |
57 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | |
58 | ||
59 | await rate.destroy({ transaction: t }) | |
60 | await video.decrement('likes', { transaction: t }) | |
61 | ||
62 | if (video.isOwned()) { | |
63 | // Don't resend the activity to the sender | |
64 | const exceptions = [ byAccount.Actor ] | |
65 | await forwardActivity(activity, t, exceptions) | |
66 | } | |
67 | }) | |
68 | } | |
69 | ||
70 | function processUndoDislike (actorUrl: string, activity: ActivityUndo) { | |
71 | const options = { | |
72 | arguments: [ actorUrl, activity ], | |
73 | errorMessage: 'Cannot undo dislike with many retries.' | |
74 | } | |
75 | ||
76 | return retryTransactionWrapper(undoDislike, options) | |
77 | } | |
78 | ||
79 | function undoDislike (actorUrl: string, activity: ActivityUndo) { | |
80 | const dislike = activity.object.object as DislikeObject | |
81 | ||
82 | return sequelizeTypescript.transaction(async t => { | |
83 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) | |
84 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | |
85 | ||
86 | const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) | |
87 | if (!video) throw new Error('Unknown video ' + dislike.actor) | |
88 | ||
89 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) | |
90 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | |
91 | ||
92 | await rate.destroy({ transaction: t }) | |
93 | await video.decrement('dislikes', { transaction: t }) | |
94 | ||
95 | if (video.isOwned()) { | |
96 | // Don't resend the activity to the sender | |
97 | const exceptions = [ byAccount.Actor ] | |
98 | await forwardActivity(activity, t, exceptions) | |
99 | } | |
100 | }) | |
101 | } | |
102 | ||
103 | function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) { | |
104 | const options = { | |
105 | arguments: [ actorUrl, followActivity ], | |
106 | errorMessage: 'Cannot undo follow with many retries.' | |
107 | } | |
108 | ||
109 | return retryTransactionWrapper(undoFollow, options) | |
110 | } | |
111 | ||
112 | function undoFollow (actorUrl: string, followActivity: ActivityFollow) { | |
113 | return sequelizeTypescript.transaction(async t => { | |
114 | const follower = await ActorModel.loadByUrl(actorUrl, t) | |
115 | const following = await ActorModel.loadByUrl(followActivity.object, t) | |
116 | const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t) | |
117 | ||
118 | if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`) | |
119 | ||
120 | await actorFollow.destroy({ transaction: t }) | |
121 | ||
122 | return undefined | |
123 | }) | |
124 | } |