]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-undo.ts
Split files in activitypub server
[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'
e251f170 11import { forwardActivity } 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') {
6be84cbc 21 return processUndoLike(actorUrl, activity)
0032ebe9 22 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
6be84cbc 23 return processUndoDislike(actorUrl, activity)
0032ebe9 24 } else if (activityToUndo.type === 'Follow') {
6be84cbc 25 return processUndoFollow(actorUrl, activityToUndo)
0f320037
C
26 } else if (activityToUndo.type === 'Announce') {
27 return 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
50d6de9c 43function processUndoLike (actorUrl: string, activity: ActivityUndo) {
0032ebe9 44 const options = {
50d6de9c 45 arguments: [ actorUrl, activity ],
0032ebe9
C
46 errorMessage: 'Cannot undo like with many retries.'
47 }
48
49 return retryTransactionWrapper(undoLike, options)
50}
51
2ccaeeb3 52async function undoLike (actorUrl: string, activity: ActivityUndo) {
63c93323
C
53 const likeActivity = activity.object as ActivityLike
54
2ccaeeb3
C
55 const { video } = await getOrCreateAccountAndVideoAndChannel(likeActivity.object)
56
3fd3ab2d 57 return sequelizeTypescript.transaction(async t => {
50d6de9c
C
58 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
59 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
0032ebe9 60
3fd3ab2d 61 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
0032ebe9
C
62 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
63
64 await rate.destroy({ transaction: t })
63c93323 65 await video.decrement('likes', { transaction: t })
0032ebe9 66
63c93323
C
67 if (video.isOwned()) {
68 // Don't resend the activity to the sender
50d6de9c 69 const exceptions = [ byAccount.Actor ]
63c93323
C
70 await forwardActivity(activity, t, exceptions)
71 }
0032ebe9
C
72 })
73}
74
50d6de9c 75function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
0032ebe9 76 const options = {
50d6de9c 77 arguments: [ actorUrl, activity ],
0032ebe9
C
78 errorMessage: 'Cannot undo dislike with many retries.'
79 }
80
81 return retryTransactionWrapper(undoDislike, options)
82}
83
2ccaeeb3 84async function undoDislike (actorUrl: string, activity: ActivityUndo) {
63c93323
C
85 const dislike = activity.object.object as DislikeObject
86
2ccaeeb3
C
87 const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
88
3fd3ab2d 89 return sequelizeTypescript.transaction(async t => {
50d6de9c
C
90 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
91 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
0032ebe9 92
3fd3ab2d 93 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
0032ebe9
C
94 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
95
96 await rate.destroy({ transaction: t })
63c93323 97 await video.decrement('dislikes', { transaction: t })
0032ebe9 98
63c93323
C
99 if (video.isOwned()) {
100 // Don't resend the activity to the sender
50d6de9c 101 const exceptions = [ byAccount.Actor ]
63c93323
C
102 await forwardActivity(activity, t, exceptions)
103 }
0032ebe9
C
104 })
105}
106
50d6de9c 107function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
0032ebe9 108 const options = {
50d6de9c 109 arguments: [ actorUrl, followActivity ],
0032ebe9
C
110 errorMessage: 'Cannot undo follow with many retries.'
111 }
112
113 return retryTransactionWrapper(undoFollow, options)
114}
115
50d6de9c 116function undoFollow (actorUrl: string, followActivity: ActivityFollow) {
3fd3ab2d 117 return sequelizeTypescript.transaction(async t => {
50d6de9c
C
118 const follower = await ActorModel.loadByUrl(actorUrl, t)
119 const following = await ActorModel.loadByUrl(followActivity.object, t)
120 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
0032ebe9 121
50d6de9c 122 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
0032ebe9 123
50d6de9c 124 await actorFollow.destroy({ transaction: t })
0032ebe9
C
125
126 return undefined
127 })
128}
0f320037
C
129
130function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) {
131 const options = {
132 arguments: [ actorUrl, announceActivity ],
133 errorMessage: 'Cannot undo announce with many retries.'
134 }
135
136 return retryTransactionWrapper(undoAnnounce, options)
137}
138
139function undoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) {
140 return sequelizeTypescript.transaction(async t => {
141 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
142 if (!share) throw new Error(`'Unknown video share ${announceActivity.id}.`)
143
144 await share.destroy({ transaction: t })
145
146 return undefined
147 })
148}