]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-undo.ts
Fix hooks definition
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-undo.ts
1 import { Transaction } from 'sequelize'
2 import {
3 ActivityAnnounce,
4 ActivityAudience,
5 ActivityCreate,
6 ActivityDislike,
7 ActivityFollow,
8 ActivityLike,
9 ActivityUndo
10 } from '../../../../shared/models/activitypub'
11 import { logger } from '../../../helpers/logger'
12 import { VideoModel } from '../../../models/video/video'
13 import {
14 MActor,
15 MActorAudience,
16 MActorFollowActors,
17 MActorLight,
18 MVideo,
19 MVideoAccountLight,
20 MVideoRedundancyVideo,
21 MVideoShare
22 } from '../../../types/models'
23 import { audiencify, getAudience } from '../audience'
24 import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url'
25 import { buildAnnounceWithVideoAudience } from './send-announce'
26 import { buildCreateActivity } from './send-create'
27 import { buildDislikeActivity } from './send-dislike'
28 import { buildFollowActivity } from './send-follow'
29 import { buildLikeActivity } from './send-like'
30 import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
31
32 function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
33 const me = actorFollow.ActorFollower
34 const following = actorFollow.ActorFollowing
35
36 // Same server as ours
37 if (!following.serverId) return
38
39 logger.info('Creating job to send an unfollow request to %s.', following.url)
40
41 const undoUrl = getUndoActivityPubUrl(actorFollow.url)
42
43 const followActivity = buildFollowActivity(actorFollow.url, me, following)
44 const undoActivity = undoActivityData(undoUrl, me, followActivity)
45
46 t.afterCommit(() => unicastTo(undoActivity, me, following.inboxUrl))
47 }
48
49 async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, t: Transaction) {
50 logger.info('Creating job to undo announce %s.', videoShare.url)
51
52 const undoUrl = getUndoActivityPubUrl(videoShare.url)
53
54 const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
55 const undoActivity = undoActivityData(undoUrl, byActor, announceActivity)
56
57 const followersException = [ byActor ]
58 return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
59 }
60
61 async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
62 logger.info('Creating job to undo a like of video %s.', video.url)
63
64 const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video)
65 const likeActivity = buildLikeActivity(likeUrl, byActor, video)
66
67 return sendUndoVideoRelatedActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
68 }
69
70 async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
71 logger.info('Creating job to undo a dislike of video %s.', video.url)
72
73 const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video)
74 const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
75
76 return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
77 }
78
79 async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, t: Transaction) {
80 logger.info('Creating job to undo cache file %s.', redundancyModel.url)
81
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)
89 const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
90
91 return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t })
92 }
93
94 // ---------------------------------------------------------------------------
95
96 export {
97 sendUndoFollow,
98 sendUndoLike,
99 sendUndoDislike,
100 sendUndoAnnounce,
101 sendUndoCacheFile
102 }
103
104 // ---------------------------------------------------------------------------
105
106 function undoActivityData (
107 url: string,
108 byActor: MActorAudience,
109 object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
110 audience?: ActivityAudience
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 )
123 }
124
125 async function sendUndoVideoRelatedActivity (options: {
126 byActor: MActor
127 video: MVideoAccountLight
128 url: string
129 activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce
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 }