]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-undo.ts
Fix cc field in classic audience
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
1 import { ActivityAnnounce, 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 import { VideoShareModel } from '../../../models/video/video-share'
14
15 async function processUndoActivity (activity: ActivityUndo) {
16 const activityToUndo = activity.object
17
18 const actorUrl = getActorUrl(activity.actor)
19
20 if (activityToUndo.type === 'Like') {
21 return processUndoLike(actorUrl, activity)
22 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
23 return processUndoDislike(actorUrl, activity)
24 } else if (activityToUndo.type === 'Follow') {
25 return processUndoFollow(actorUrl, activityToUndo)
26 } else if (activityToUndo.type === 'Announce') {
27 return processUndoAnnounce(actorUrl, activityToUndo)
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
37 export {
38 processUndoActivity
39 }
40
41 // ---------------------------------------------------------------------------
42
43 function processUndoLike (actorUrl: string, activity: ActivityUndo) {
44 const options = {
45 arguments: [ actorUrl, activity ],
46 errorMessage: 'Cannot undo like with many retries.'
47 }
48
49 return retryTransactionWrapper(undoLike, options)
50 }
51
52 async function undoLike (actorUrl: string, activity: ActivityUndo) {
53 const likeActivity = activity.object as ActivityLike
54
55 const { video } = await getOrCreateAccountAndVideoAndChannel(likeActivity.object)
56
57 return sequelizeTypescript.transaction(async t => {
58 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
59 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
60
61 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
62 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
63
64 await rate.destroy({ transaction: t })
65 await video.decrement('likes', { transaction: t })
66
67 if (video.isOwned()) {
68 // Don't resend the activity to the sender
69 const exceptions = [ byAccount.Actor ]
70 await forwardActivity(activity, t, exceptions)
71 }
72 })
73 }
74
75 function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
76 const options = {
77 arguments: [ actorUrl, activity ],
78 errorMessage: 'Cannot undo dislike with many retries.'
79 }
80
81 return retryTransactionWrapper(undoDislike, options)
82 }
83
84 async function undoDislike (actorUrl: string, activity: ActivityUndo) {
85 const dislike = activity.object.object as DislikeObject
86
87 const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
88
89 return sequelizeTypescript.transaction(async t => {
90 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
91 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
92
93 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
94 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
95
96 await rate.destroy({ transaction: t })
97 await video.decrement('dislikes', { transaction: t })
98
99 if (video.isOwned()) {
100 // Don't resend the activity to the sender
101 const exceptions = [ byAccount.Actor ]
102 await forwardActivity(activity, t, exceptions)
103 }
104 })
105 }
106
107 function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
108 const options = {
109 arguments: [ actorUrl, followActivity ],
110 errorMessage: 'Cannot undo follow with many retries.'
111 }
112
113 return retryTransactionWrapper(undoFollow, options)
114 }
115
116 function undoFollow (actorUrl: string, followActivity: ActivityFollow) {
117 return sequelizeTypescript.transaction(async t => {
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)
121
122 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
123
124 await actorFollow.destroy({ transaction: t })
125
126 return undefined
127 })
128 }
129
130 function 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
139 function 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 }