]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-undo.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-undo.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
0f320037
C
2import {
3 ActivityAnnounce,
4 ActivityAudience,
453e83ea
C
5 ActivityCreate,
6 ActivityDislike,
0f320037
C
7 ActivityFollow,
8 ActivityLike,
a219c910
C
9 ActivityUndo,
10 ContextType
57e4e1c1 11} from '@shared/models'
8e0fd45e 12import { logger } from '../../../helpers/logger'
de94ac86 13import { VideoModel } from '../../../models/video/video'
453e83ea 14import {
de94ac86
C
15 MActor,
16 MActorAudience,
453e83ea
C
17 MActorFollowActors,
18 MActorLight,
19 MVideo,
20 MVideoAccountLight,
21 MVideoRedundancyVideo,
22 MVideoShare
26d6bf65 23} from '../../../types/models'
de94ac86
C
24import { audiencify, getAudience } from '../audience'
25import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url'
26import { buildAnnounceWithVideoAudience } from './send-announce'
27import { buildCreateActivity } from './send-create'
28import { buildDislikeActivity } from './send-dislike'
29import { buildFollowActivity } from './send-follow'
30import { buildLikeActivity } from './send-like'
57e4e1c1 31import { broadcastToFollowers, sendVideoActivityToOrigin, sendVideoRelatedActivity, unicastTo } from './shared/send-utils'
453e83ea 32
a1587156 33function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
50d6de9c
C
34 const me = actorFollow.ActorFollower
35 const following = actorFollow.ActorFollowing
54141398 36
06a05d5f
C
37 // Same server as ours
38 if (!following.serverId) return
39
8e0fd45e
C
40 logger.info('Creating job to send an unfollow request to %s.', following.url)
41
de94ac86 42 const undoUrl = getUndoActivityPubUrl(actorFollow.url)
54141398 43
de94ac86 44 const followActivity = buildFollowActivity(actorFollow.url, me, following)
c48e82b5 45 const undoActivity = undoActivityData(undoUrl, me, followActivity)
54141398 46
bae61627 47 t.afterCommit(() => {
a219c910
C
48 return unicastTo({
49 data: undoActivity,
50 byActor: me,
51 toActorUrl: following.inboxUrl,
52 contextType: 'Follow'
53 })
54 })
54141398
C
55}
56
57e4e1c1
C
57// ---------------------------------------------------------------------------
58
a219c910 59async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, transaction: Transaction) {
a2377d15 60 logger.info('Creating job to undo announce %s.', videoShare.url)
8e0fd45e 61
a2377d15 62 const undoUrl = getUndoActivityPubUrl(videoShare.url)
0032ebe9 63
a219c910
C
64 const { activity: announce, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, transaction)
65 const undoActivity = undoActivityData(undoUrl, byActor, announce)
66
67 return broadcastToFollowers({
68 data: undoActivity,
69 byActor,
70 toFollowersOf: actorsInvolvedInVideo,
71 transaction,
72 actorsException: [ byActor ],
73 contextType: 'Announce'
74 })
a2377d15 75}
0032ebe9 76
a219c910 77async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, transaction: Transaction) {
57e4e1c1
C
78 logger.info('Creating job to undo cache file %s.', redundancyModel.url)
79
80 const associatedVideo = redundancyModel.getVideo()
81 if (!associatedVideo) {
82 logger.warn('Cannot send undo activity for redundancy %s: no video files associated.', redundancyModel.url)
83 return
84 }
85
4fae2b1f 86 const video = await VideoModel.loadFull(associatedVideo.id)
57e4e1c1
C
87 const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
88
a219c910
C
89 return sendUndoVideoRelatedActivity({
90 byActor,
91 video,
92 url: redundancyModel.url,
93 activity: createActivity,
94 contextType: 'CacheFile',
95 transaction
96 })
57e4e1c1
C
97}
98
99// ---------------------------------------------------------------------------
100
453e83ea 101async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
a2377d15 102 logger.info('Creating job to undo a like of video %s.', video.url)
0032ebe9 103
de94ac86 104 const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video)
a2377d15 105 const likeActivity = buildLikeActivity(likeUrl, byActor, video)
0032ebe9 106
a219c910 107 return sendUndoVideoRateToOriginActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
0032ebe9
C
108}
109
453e83ea 110async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
8e0fd45e
C
111 logger.info('Creating job to undo a dislike of video %s.', video.url)
112
de94ac86 113 const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video)
5c6d985f 114 const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
0032ebe9 115
a219c910 116 return sendUndoVideoRateToOriginActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
0f320037
C
117}
118
54141398
C
119// ---------------------------------------------------------------------------
120
121export {
0032ebe9 122 sendUndoFollow,
07197db4 123 sendUndoLike,
0f320037 124 sendUndoDislike,
c48e82b5
C
125 sendUndoAnnounce,
126 sendUndoCacheFile
54141398
C
127}
128
129// ---------------------------------------------------------------------------
130
2186386c 131function undoActivityData (
63c93323 132 url: string,
453e83ea 133 byActor: MActorAudience,
1e7eb25f 134 object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
63c93323 135 audience?: ActivityAudience
2186386c
C
136): ActivityUndo {
137 if (!audience) audience = getAudience(byActor)
138
139 return audiencify(
140 {
141 type: 'Undo' as 'Undo',
142 id: url,
143 actor: byActor.url,
144 object
145 },
146 audience
147 )
54141398 148}
a2377d15
C
149
150async function sendUndoVideoRelatedActivity (options: {
a1587156
C
151 byActor: MActor
152 video: MVideoAccountLight
153 url: string
57e4e1c1 154 activity: ActivityFollow | ActivityCreate | ActivityAnnounce
a219c910 155 contextType: ContextType
a2377d15
C
156 transaction: Transaction
157}) {
158 const activityBuilder = (audience: ActivityAudience) => {
159 const undoUrl = getUndoActivityPubUrl(options.url)
160
161 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
162 }
163
164 return sendVideoRelatedActivity(activityBuilder, options)
165}
57e4e1c1 166
a219c910 167async function sendUndoVideoRateToOriginActivity (options: {
57e4e1c1
C
168 byActor: MActor
169 video: MVideoAccountLight
170 url: string
171 activity: ActivityLike | ActivityDislike
172 transaction: Transaction
173}) {
174 const activityBuilder = (audience: ActivityAudience) => {
175 const undoUrl = getUndoActivityPubUrl(options.url)
176
177 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
178 }
179
a219c910 180 return sendVideoActivityToOrigin(activityBuilder, { ...options, contextType: 'Rate' })
57e4e1c1 181}