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