]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-undo.ts
Merge branch 'release/2.1.0' into develop
[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'
3fd3ab2d 11import { VideoModel } from '../../../models/video/video'
50d6de9c 12import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
a2377d15
C
13import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
14import { audiencify, getAudience } from '../audience'
1e7eb25f 15import { buildCreateActivity } from './send-create'
c48e82b5
C
16import { buildFollowActivity } from './send-follow'
17import { buildLikeActivity } from './send-like'
c48e82b5 18import { buildAnnounceWithVideoAudience } from './send-announce'
8e0fd45e 19import { logger } from '../../../helpers/logger'
1e7eb25f 20import { buildDislikeActivity } from './send-dislike'
453e83ea
C
21import {
22 MActor, MActorAudience,
23 MActorFollowActors,
24 MActorLight,
25 MVideo,
26 MVideoAccountLight,
27 MVideoRedundancyVideo,
28 MVideoShare
29} from '../../../typings/models'
30
a1587156 31function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
50d6de9c
C
32 const me = actorFollow.ActorFollower
33 const following = actorFollow.ActorFollowing
54141398 34
06a05d5f
C
35 // Same server as ours
36 if (!following.serverId) return
37
8e0fd45e
C
38 logger.info('Creating job to send an unfollow request to %s.', following.url)
39
5b9c965d 40 const followUrl = getActorFollowActivityPubUrl(me, following)
54141398
C
41 const undoUrl = getUndoActivityPubUrl(followUrl)
42
c48e82b5
C
43 const followActivity = buildFollowActivity(followUrl, me, following)
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
a2377d15
C
64 const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
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
50d6de9c 73 const dislikeUrl = getVideoDislikeActivityPubUrl(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
09209296
C
82 const videoId = redundancyModel.getVideo().id
83 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
c48e82b5
C
84 const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
85
a2377d15 86 return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t })
0f320037
C
87}
88
54141398
C
89// ---------------------------------------------------------------------------
90
91export {
0032ebe9 92 sendUndoFollow,
07197db4 93 sendUndoLike,
0f320037 94 sendUndoDislike,
c48e82b5
C
95 sendUndoAnnounce,
96 sendUndoCacheFile
54141398
C
97}
98
99// ---------------------------------------------------------------------------
100
2186386c 101function undoActivityData (
63c93323 102 url: string,
453e83ea 103 byActor: MActorAudience,
1e7eb25f 104 object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
63c93323 105 audience?: ActivityAudience
2186386c
C
106): ActivityUndo {
107 if (!audience) audience = getAudience(byActor)
108
109 return audiencify(
110 {
111 type: 'Undo' as 'Undo',
112 id: url,
113 actor: byActor.url,
114 object
115 },
116 audience
117 )
54141398 118}
a2377d15
C
119
120async function sendUndoVideoRelatedActivity (options: {
a1587156
C
121 byActor: MActor
122 video: MVideoAccountLight
123 url: string
124 activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce
a2377d15
C
125 transaction: Transaction
126}) {
127 const activityBuilder = (audience: ActivityAudience) => {
128 const undoUrl = getUndoActivityPubUrl(options.url)
129
130 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
131 }
132
133 return sendVideoRelatedActivity(activityBuilder, options)
134}