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