]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-undo.ts
Support video views/viewers stats in server
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
CommitLineData
57e4e1c1 1import { VideoModel } from '@server/models/video/video'
c48e82b5 2import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
3fd3ab2d 3import { DislikeObject } from '../../../../shared/models/activitypub/objects'
da854ddd
C
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger'
80fdaf06 6import { sequelizeTypescript } from '../../../initializers/database'
3fd3ab2d 7import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
7d9ba5c0
C
8import { ActorModel } from '../../../models/actor/actor'
9import { ActorFollowModel } from '../../../models/actor/actor-follow'
c48e82b5 10import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
c15aff66 11import { VideoShareModel } from '../../../models/video/video-share'
26d6bf65
C
12import { APProcessorOptions } from '../../../types/activitypub-processor.model'
13import { MActorSignature } from '../../../types/models'
57e4e1c1
C
14import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
15import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos'
54141398 16
1198edf4
C
17async function processUndoActivity (options: APProcessorOptions<ActivityUndo>) {
18 const { activity, byActor } = options
54141398
C
19 const activityToUndo = activity.object
20
0032ebe9 21 if (activityToUndo.type === 'Like') {
12ba460e 22 return retryTransactionWrapper(processUndoLike, byActor, activity)
c48e82b5
C
23 }
24
25 if (activityToUndo.type === 'Create') {
c15aff66 26 if (activityToUndo.object.type === 'CacheFile') {
e587e0ec 27 return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
c48e82b5
C
28 }
29 }
30
848f499d
C
31 if (activityToUndo.type === 'Dislike') {
32 return retryTransactionWrapper(processUndoDislike, byActor, activity)
33 }
34
c48e82b5 35 if (activityToUndo.type === 'Follow') {
e587e0ec 36 return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
c48e82b5
C
37 }
38
39 if (activityToUndo.type === 'Announce') {
e587e0ec 40 return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
54141398
C
41 }
42
43 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
44
45 return undefined
46}
47
48// ---------------------------------------------------------------------------
49
50export {
51 processUndoActivity
52}
53
54// ---------------------------------------------------------------------------
0032ebe9 55
453e83ea 56async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo) {
63c93323
C
57 const likeActivity = activity.object as ActivityLike
58
57e4e1c1
C
59 const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: likeActivity.object })
60 // We don't care about likes of remote videos
61 if (!onlyVideo.isOwned()) return
2ccaeeb3 62
3fd3ab2d 63 return sequelizeTypescript.transaction(async t => {
12ba460e 64 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 65
57e4e1c1 66 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(onlyVideo.id, t)
29d4e137
C
67 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, likeActivity.id, t)
68 if (!rate || rate.type !== 'like') throw new Error(`Unknown like by account ${byActor.Account.id} for video ${video.id}.`)
0032ebe9
C
69
70 await rate.destroy({ transaction: t })
63c93323 71 await video.decrement('likes', { transaction: t })
0032ebe9 72
57e4e1c1
C
73 video.likes--
74 await federateVideoIfNeeded(video, false, t)
0032ebe9
C
75 })
76}
77
453e83ea 78async function processUndoDislike (byActor: MActorSignature, activity: ActivityUndo) {
848f499d
C
79 const dislike = activity.object.type === 'Dislike'
80 ? activity.object
81 : activity.object.object as DislikeObject
63c93323 82
57e4e1c1
C
83 const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: dislike.object })
84 // We don't care about likes of remote videos
85 if (!onlyVideo.isOwned()) return
2ccaeeb3 86
3fd3ab2d 87 return sequelizeTypescript.transaction(async t => {
12ba460e 88 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 89
57e4e1c1 90 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(onlyVideo.id, t)
29d4e137
C
91 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislike.id, t)
92 if (!rate || rate.type !== 'dislike') throw new Error(`Unknown dislike by account ${byActor.Account.id} for video ${video.id}.`)
0032ebe9
C
93
94 await rate.destroy({ transaction: t })
63c93323 95 await video.decrement('dislikes', { transaction: t })
57e4e1c1 96 video.dislikes--
0032ebe9 97
57e4e1c1 98 await federateVideoIfNeeded(video, false, t)
0032ebe9
C
99 })
100}
101
57e4e1c1
C
102// ---------------------------------------------------------------------------
103
453e83ea 104async function processUndoCacheFile (byActor: MActorSignature, activity: ActivityUndo) {
c48e82b5
C
105 const cacheFileObject = activity.object.object as CacheFileObject
106
304a84d5 107 const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object })
c48e82b5
C
108
109 return sequelizeTypescript.transaction(async t => {
eae0365b 110 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
9cc8d43e
C
111 if (!cacheFile) {
112 logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
113 return
114 }
c48e82b5 115
12ba460e
C
116 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
117
eae0365b 118 await cacheFile.destroy({ transaction: t })
c48e82b5
C
119
120 if (video.isOwned()) {
121 // Don't resend the activity to the sender
122 const exceptions = [ byActor ]
123
124 await forwardVideoRelatedActivity(activity, t, exceptions, video)
125 }
126 })
127}
128
453e83ea 129function processUndoAnnounce (byActor: MActorSignature, announceActivity: ActivityAnnounce) {
0f320037
C
130 return sequelizeTypescript.transaction(async t => {
131 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
5cf84858
C
132 if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
133
134 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
0f320037
C
135
136 await share.destroy({ transaction: t })
137
9588d4f4
C
138 if (share.Video.isOwned()) {
139 // Don't resend the activity to the sender
5cf84858 140 const exceptions = [ byActor ]
9588d4f4
C
141
142 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
143 }
0f320037
C
144 })
145}
57e4e1c1
C
146
147// ---------------------------------------------------------------------------
148
149function processUndoFollow (follower: MActorSignature, followActivity: ActivityFollow) {
150 return sequelizeTypescript.transaction(async t => {
151 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
152 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
153
154 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
155
156 await actorFollow.destroy({ transaction: t })
157
158 return undefined
159 })
160}