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