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