]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-undo.ts
Fix hooks definition
[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,
9 ActivityUndo
10} from '../../../../shared/models/activitypub'
8e0fd45e 11import { logger } from '../../../helpers/logger'
de94ac86 12import { VideoModel } from '../../../models/video/video'
453e83ea 13import {
de94ac86
C
14 MActor,
15 MActorAudience,
453e83ea
C
16 MActorFollowActors,
17 MActorLight,
18 MVideo,
19 MVideoAccountLight,
20 MVideoRedundancyVideo,
21 MVideoShare
26d6bf65 22} from '../../../types/models'
de94ac86
C
23import { audiencify, getAudience } from '../audience'
24import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url'
25import { buildAnnounceWithVideoAudience } from './send-announce'
26import { buildCreateActivity } from './send-create'
27import { buildDislikeActivity } from './send-dislike'
28import { buildFollowActivity } from './send-follow'
29import { buildLikeActivity } from './send-like'
30import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
453e83ea 31
a1587156 32function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
50d6de9c
C
33 const me = actorFollow.ActorFollower
34 const following = actorFollow.ActorFollowing
54141398 35
06a05d5f
C
36 // Same server as ours
37 if (!following.serverId) return
38
8e0fd45e
C
39 logger.info('Creating job to send an unfollow request to %s.', following.url)
40
de94ac86 41 const undoUrl = getUndoActivityPubUrl(actorFollow.url)
54141398 42
de94ac86 43 const followActivity = buildFollowActivity(actorFollow.url, me, following)
c48e82b5 44 const undoActivity = undoActivityData(undoUrl, me, followActivity)
54141398 45
2284f202 46 t.afterCommit(() => unicastTo(undoActivity, me, following.inboxUrl))
54141398
C
47}
48
453e83ea 49async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, t: Transaction) {
a2377d15 50 logger.info('Creating job to undo announce %s.', videoShare.url)
8e0fd45e 51
a2377d15 52 const undoUrl = getUndoActivityPubUrl(videoShare.url)
0032ebe9 53
a2377d15
C
54 const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
55 const undoActivity = undoActivityData(undoUrl, byActor, announceActivity)
0032ebe9 56
a2377d15
C
57 const followersException = [ byActor ]
58 return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
59}
0032ebe9 60
453e83ea 61async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
a2377d15 62 logger.info('Creating job to undo a like of video %s.', video.url)
0032ebe9 63
de94ac86 64 const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video)
a2377d15 65 const likeActivity = buildLikeActivity(likeUrl, byActor, video)
0032ebe9 66
a2377d15 67 return sendUndoVideoRelatedActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
0032ebe9
C
68}
69
453e83ea 70async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
8e0fd45e
C
71 logger.info('Creating job to undo a dislike of video %s.', video.url)
72
de94ac86 73 const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video)
5c6d985f 74 const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
0032ebe9 75
1e7eb25f 76 return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
c48e82b5
C
77}
78
453e83ea 79async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, t: Transaction) {
c48e82b5
C
80 logger.info('Creating job to undo cache file %s.', redundancyModel.url)
81
9cfeb3cf
C
82 const associatedVideo = redundancyModel.getVideo()
83 if (!associatedVideo) {
84 logger.warn('Cannot send undo activity for redundancy %s: no video files associated.', redundancyModel.url)
85 return
86 }
87
88 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(associatedVideo.id)
c48e82b5
C
89 const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
90
a2377d15 91 return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t })
0f320037
C
92}
93
54141398
C
94// ---------------------------------------------------------------------------
95
96export {
0032ebe9 97 sendUndoFollow,
07197db4 98 sendUndoLike,
0f320037 99 sendUndoDislike,
c48e82b5
C
100 sendUndoAnnounce,
101 sendUndoCacheFile
54141398
C
102}
103
104// ---------------------------------------------------------------------------
105
2186386c 106function undoActivityData (
63c93323 107 url: string,
453e83ea 108 byActor: MActorAudience,
1e7eb25f 109 object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
63c93323 110 audience?: ActivityAudience
2186386c
C
111): ActivityUndo {
112 if (!audience) audience = getAudience(byActor)
113
114 return audiencify(
115 {
116 type: 'Undo' as 'Undo',
117 id: url,
118 actor: byActor.url,
119 object
120 },
121 audience
122 )
54141398 123}
a2377d15
C
124
125async function sendUndoVideoRelatedActivity (options: {
a1587156
C
126 byActor: MActor
127 video: MVideoAccountLight
128 url: string
129 activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce
a2377d15
C
130 transaction: Transaction
131}) {
132 const activityBuilder = (audience: ActivityAudience) => {
133 const undoUrl = getUndoActivityPubUrl(options.url)
134
135 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
136 }
137
138 return sendVideoRelatedActivity(activityBuilder, options)
139}