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