]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub' | |
2 | import { DislikeObject } from '../../../../shared/models/activitypub/objects' | |
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { sequelizeTypescript } from '../../../initializers' | |
6 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
7 | import { ActorModel } from '../../../models/activitypub/actor' | |
8 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
9 | import { forwardVideoRelatedActivity } from '../send/utils' | |
10 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | |
11 | import { VideoShareModel } from '../../../models/video/video-share' | |
12 | import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' | |
13 | import { APProcessorOptions } from '../../../typings/activitypub-processor.model' | |
14 | import { MActorSignature } from '../../../typings/models' | |
15 | ||
16 | async function processUndoActivity (options: APProcessorOptions<ActivityUndo>) { | |
17 | const { activity, byActor } = options | |
18 | const activityToUndo = activity.object | |
19 | ||
20 | if (activityToUndo.type === 'Like') { | |
21 | return retryTransactionWrapper(processUndoLike, byActor, activity) | |
22 | } | |
23 | ||
24 | if (activityToUndo.type === 'Create') { | |
25 | if (activityToUndo.object.type === 'Dislike') { | |
26 | return retryTransactionWrapper(processUndoDislike, byActor, activity) | |
27 | } else if (activityToUndo.object.type === 'CacheFile') { | |
28 | return retryTransactionWrapper(processUndoCacheFile, byActor, activity) | |
29 | } | |
30 | } | |
31 | ||
32 | if (activityToUndo.type === 'Dislike') { | |
33 | return retryTransactionWrapper(processUndoDislike, byActor, activity) | |
34 | } | |
35 | ||
36 | if (activityToUndo.type === 'Follow') { | |
37 | return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo) | |
38 | } | |
39 | ||
40 | if (activityToUndo.type === 'Announce') { | |
41 | return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo) | |
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 | // --------------------------------------------------------------------------- | |
56 | ||
57 | async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo) { | |
58 | const likeActivity = activity.object as ActivityLike | |
59 | ||
60 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object }) | |
61 | ||
62 | return sequelizeTypescript.transaction(async t => { | |
63 | if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) | |
64 | ||
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}.`) | |
67 | ||
68 | await rate.destroy({ transaction: t }) | |
69 | await video.decrement('likes', { transaction: t }) | |
70 | ||
71 | if (video.isOwned()) { | |
72 | // Don't resend the activity to the sender | |
73 | const exceptions = [ byActor ] | |
74 | ||
75 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
76 | } | |
77 | }) | |
78 | } | |
79 | ||
80 | async function processUndoDislike (byActor: MActorSignature, activity: ActivityUndo) { | |
81 | const dislike = activity.object.type === 'Dislike' | |
82 | ? activity.object | |
83 | : activity.object.object as DislikeObject | |
84 | ||
85 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) | |
86 | ||
87 | return sequelizeTypescript.transaction(async t => { | |
88 | if (!byActor.Account) throw new Error('Unknown account ' + byActor.url) | |
89 | ||
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}.`) | |
92 | ||
93 | await rate.destroy({ transaction: t }) | |
94 | await video.decrement('dislikes', { transaction: t }) | |
95 | ||
96 | if (video.isOwned()) { | |
97 | // Don't resend the activity to the sender | |
98 | const exceptions = [ byActor ] | |
99 | ||
100 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
101 | } | |
102 | }) | |
103 | } | |
104 | ||
105 | async function processUndoCacheFile (byActor: MActorSignature, activity: ActivityUndo) { | |
106 | const cacheFileObject = activity.object.object as CacheFileObject | |
107 | ||
108 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) | |
109 | ||
110 | return sequelizeTypescript.transaction(async t => { | |
111 | const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id) | |
112 | if (!cacheFile) { | |
113 | logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id) | |
114 | return | |
115 | } | |
116 | ||
117 | if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.') | |
118 | ||
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 | ||
130 | function processUndoFollow (follower: MActorSignature, followActivity: ActivityFollow) { | |
131 | return sequelizeTypescript.transaction(async t => { | |
132 | const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t) | |
133 | const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t) | |
134 | ||
135 | if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`) | |
136 | ||
137 | await actorFollow.destroy({ transaction: t }) | |
138 | ||
139 | return undefined | |
140 | }) | |
141 | } | |
142 | ||
143 | function processUndoAnnounce (byActor: MActorSignature, announceActivity: ActivityAnnounce) { | |
144 | return sequelizeTypescript.transaction(async t => { | |
145 | const share = await VideoShareModel.loadByUrl(announceActivity.id, t) | |
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}.`) | |
149 | ||
150 | await share.destroy({ transaction: t }) | |
151 | ||
152 | if (share.Video.isOwned()) { | |
153 | // Don't resend the activity to the sender | |
154 | const exceptions = [ byActor ] | |
155 | ||
156 | await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video) | |
157 | } | |
158 | }) | |
159 | } |