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