]>
Commit | Line | Data |
---|---|---|
0f320037 | 1 | import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub' |
3fd3ab2d | 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' | |
9588d4f4 | 11 | import { forwardVideoRelatedActivity } from '../send/utils' |
2ccaeeb3 | 12 | import { getOrCreateAccountAndVideoAndChannel } from '../videos' |
0f320037 | 13 | import { VideoShareModel } from '../../../models/video/video-share' |
54141398 C |
14 | |
15 | async function processUndoActivity (activity: ActivityUndo) { | |
16 | const activityToUndo = activity.object | |
17 | ||
6be84cbc C |
18 | const actorUrl = getActorUrl(activity.actor) |
19 | ||
0032ebe9 | 20 | if (activityToUndo.type === 'Like') { |
6be84cbc | 21 | return processUndoLike(actorUrl, activity) |
0032ebe9 | 22 | } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { |
6be84cbc | 23 | return processUndoDislike(actorUrl, activity) |
0032ebe9 | 24 | } else if (activityToUndo.type === 'Follow') { |
6be84cbc | 25 | return processUndoFollow(actorUrl, activityToUndo) |
0f320037 C |
26 | } else if (activityToUndo.type === 'Announce') { |
27 | return processUndoAnnounce(actorUrl, activityToUndo) | |
54141398 C |
28 | } |
29 | ||
30 | logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id }) | |
31 | ||
32 | return undefined | |
33 | } | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | ||
37 | export { | |
38 | processUndoActivity | |
39 | } | |
40 | ||
41 | // --------------------------------------------------------------------------- | |
0032ebe9 | 42 | |
50d6de9c | 43 | function processUndoLike (actorUrl: string, activity: ActivityUndo) { |
0032ebe9 | 44 | const options = { |
50d6de9c | 45 | arguments: [ actorUrl, activity ], |
0032ebe9 C |
46 | errorMessage: 'Cannot undo like with many retries.' |
47 | } | |
48 | ||
49 | return retryTransactionWrapper(undoLike, options) | |
50 | } | |
51 | ||
2ccaeeb3 | 52 | async function undoLike (actorUrl: string, activity: ActivityUndo) { |
63c93323 C |
53 | const likeActivity = activity.object as ActivityLike |
54 | ||
2ccaeeb3 C |
55 | const { video } = await getOrCreateAccountAndVideoAndChannel(likeActivity.object) |
56 | ||
3fd3ab2d | 57 | return sequelizeTypescript.transaction(async t => { |
50d6de9c C |
58 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) |
59 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | |
0032ebe9 | 60 | |
3fd3ab2d | 61 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) |
0032ebe9 C |
62 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
63 | ||
64 | await rate.destroy({ transaction: t }) | |
63c93323 | 65 | await video.decrement('likes', { transaction: t }) |
0032ebe9 | 66 | |
63c93323 C |
67 | if (video.isOwned()) { |
68 | // Don't resend the activity to the sender | |
50d6de9c | 69 | const exceptions = [ byAccount.Actor ] |
9588d4f4 C |
70 | |
71 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
63c93323 | 72 | } |
0032ebe9 C |
73 | }) |
74 | } | |
75 | ||
50d6de9c | 76 | function processUndoDislike (actorUrl: string, activity: ActivityUndo) { |
0032ebe9 | 77 | const options = { |
50d6de9c | 78 | arguments: [ actorUrl, activity ], |
0032ebe9 C |
79 | errorMessage: 'Cannot undo dislike with many retries.' |
80 | } | |
81 | ||
82 | return retryTransactionWrapper(undoDislike, options) | |
83 | } | |
84 | ||
2ccaeeb3 | 85 | async function undoDislike (actorUrl: string, activity: ActivityUndo) { |
63c93323 C |
86 | const dislike = activity.object.object as DislikeObject |
87 | ||
2ccaeeb3 C |
88 | const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) |
89 | ||
3fd3ab2d | 90 | return sequelizeTypescript.transaction(async t => { |
50d6de9c C |
91 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) |
92 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | |
0032ebe9 | 93 | |
3fd3ab2d | 94 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) |
0032ebe9 C |
95 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
96 | ||
97 | await rate.destroy({ transaction: t }) | |
63c93323 | 98 | await video.decrement('dislikes', { transaction: t }) |
0032ebe9 | 99 | |
63c93323 C |
100 | if (video.isOwned()) { |
101 | // Don't resend the activity to the sender | |
50d6de9c | 102 | const exceptions = [ byAccount.Actor ] |
9588d4f4 C |
103 | |
104 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
63c93323 | 105 | } |
0032ebe9 C |
106 | }) |
107 | } | |
108 | ||
50d6de9c | 109 | function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) { |
0032ebe9 | 110 | const options = { |
50d6de9c | 111 | arguments: [ actorUrl, followActivity ], |
0032ebe9 C |
112 | errorMessage: 'Cannot undo follow with many retries.' |
113 | } | |
114 | ||
115 | return retryTransactionWrapper(undoFollow, options) | |
116 | } | |
117 | ||
50d6de9c | 118 | function undoFollow (actorUrl: string, followActivity: ActivityFollow) { |
3fd3ab2d | 119 | return sequelizeTypescript.transaction(async t => { |
50d6de9c C |
120 | const follower = await ActorModel.loadByUrl(actorUrl, t) |
121 | const following = await ActorModel.loadByUrl(followActivity.object, t) | |
122 | const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t) | |
0032ebe9 | 123 | |
50d6de9c | 124 | if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`) |
0032ebe9 | 125 | |
50d6de9c | 126 | await actorFollow.destroy({ transaction: t }) |
0032ebe9 C |
127 | |
128 | return undefined | |
129 | }) | |
130 | } | |
0f320037 C |
131 | |
132 | function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) { | |
133 | const options = { | |
134 | arguments: [ actorUrl, announceActivity ], | |
135 | errorMessage: 'Cannot undo announce with many retries.' | |
136 | } | |
137 | ||
138 | return retryTransactionWrapper(undoAnnounce, options) | |
139 | } | |
140 | ||
141 | function undoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) { | |
142 | return sequelizeTypescript.transaction(async t => { | |
9588d4f4 C |
143 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) |
144 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | |
145 | ||
0f320037 C |
146 | const share = await VideoShareModel.loadByUrl(announceActivity.id, t) |
147 | if (!share) throw new Error(`'Unknown video share ${announceActivity.id}.`) | |
148 | ||
149 | await share.destroy({ transaction: t }) | |
150 | ||
9588d4f4 C |
151 | if (share.Video.isOwned()) { |
152 | // Don't resend the activity to the sender | |
153 | const exceptions = [ byAccount.Actor ] | |
154 | ||
155 | await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video) | |
156 | } | |
0f320037 C |
157 | }) |
158 | } |