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