]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-undo.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
CommitLineData
3fd3ab2d
C
1import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
2import { DislikeObject } from '../../../../shared/models/activitypub/objects'
da854ddd
C
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
3fd3ab2d
C
5import { sequelizeTypescript } from '../../../initializers'
6import { AccountModel } from '../../../models/account/account'
3fd3ab2d 7import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
50d6de9c
C
8import { ActorModel } from '../../../models/activitypub/actor'
9import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
3fd3ab2d 10import { VideoModel } from '../../../models/video/video'
63c93323 11import { forwardActivity } from '../send/misc'
54141398
C
12
13async function processUndoActivity (activity: ActivityUndo) {
14 const activityToUndo = activity.object
15
0032ebe9 16 if (activityToUndo.type === 'Like') {
63c93323 17 return processUndoLike(activity.actor, activity)
0032ebe9 18 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
63c93323 19 return processUndoDislike(activity.actor, activity)
0032ebe9
C
20 } else if (activityToUndo.type === 'Follow') {
21 return processUndoFollow(activity.actor, activityToUndo)
54141398
C
22 }
23
24 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
25
26 return undefined
27}
28
29// ---------------------------------------------------------------------------
30
31export {
32 processUndoActivity
33}
34
35// ---------------------------------------------------------------------------
0032ebe9 36
50d6de9c 37function processUndoLike (actorUrl: string, activity: ActivityUndo) {
0032ebe9 38 const options = {
50d6de9c 39 arguments: [ actorUrl, activity ],
0032ebe9
C
40 errorMessage: 'Cannot undo like with many retries.'
41 }
42
43 return retryTransactionWrapper(undoLike, options)
44}
45
50d6de9c 46function undoLike (actorUrl: string, activity: ActivityUndo) {
63c93323
C
47 const likeActivity = activity.object as ActivityLike
48
3fd3ab2d 49 return sequelizeTypescript.transaction(async t => {
50d6de9c
C
50 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
51 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
0032ebe9 52
3fd3ab2d 53 const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t)
0032ebe9
C
54 if (!video) throw new Error('Unknown video ' + likeActivity.actor)
55
3fd3ab2d 56 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
0032ebe9
C
57 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
58
59 await rate.destroy({ transaction: t })
63c93323 60 await video.decrement('likes', { transaction: t })
0032ebe9 61
63c93323
C
62 if (video.isOwned()) {
63 // Don't resend the activity to the sender
50d6de9c 64 const exceptions = [ byAccount.Actor ]
63c93323
C
65 await forwardActivity(activity, t, exceptions)
66 }
0032ebe9
C
67 })
68}
69
50d6de9c 70function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
0032ebe9 71 const options = {
50d6de9c 72 arguments: [ actorUrl, activity ],
0032ebe9
C
73 errorMessage: 'Cannot undo dislike with many retries.'
74 }
75
76 return retryTransactionWrapper(undoDislike, options)
77}
78
50d6de9c 79function undoDislike (actorUrl: string, activity: ActivityUndo) {
63c93323
C
80 const dislike = activity.object.object as DislikeObject
81
3fd3ab2d 82 return sequelizeTypescript.transaction(async t => {
50d6de9c
C
83 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
84 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
0032ebe9 85
3fd3ab2d 86 const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
0032ebe9
C
87 if (!video) throw new Error('Unknown video ' + dislike.actor)
88
3fd3ab2d 89 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
0032ebe9
C
90 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
91
92 await rate.destroy({ transaction: t })
63c93323 93 await video.decrement('dislikes', { transaction: t })
0032ebe9 94
63c93323
C
95 if (video.isOwned()) {
96 // Don't resend the activity to the sender
50d6de9c 97 const exceptions = [ byAccount.Actor ]
63c93323
C
98 await forwardActivity(activity, t, exceptions)
99 }
0032ebe9
C
100 })
101}
102
50d6de9c 103function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
0032ebe9 104 const options = {
50d6de9c 105 arguments: [ actorUrl, followActivity ],
0032ebe9
C
106 errorMessage: 'Cannot undo follow with many retries.'
107 }
108
109 return retryTransactionWrapper(undoFollow, options)
110}
111
50d6de9c 112function undoFollow (actorUrl: string, followActivity: ActivityFollow) {
3fd3ab2d 113 return sequelizeTypescript.transaction(async t => {
50d6de9c
C
114 const follower = await ActorModel.loadByUrl(actorUrl, t)
115 const following = await ActorModel.loadByUrl(followActivity.object, t)
116 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
0032ebe9 117
50d6de9c 118 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
0032ebe9 119
50d6de9c 120 await actorFollow.destroy({ transaction: t })
0032ebe9
C
121
122 return undefined
123 })
124}