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