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