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