]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
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 { VideoModel } from '../../../models/video/video'
12 import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
13 import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
14 import { audiencify, getAudience } from '../audience'
15 import { buildCreateActivity } from './send-create'
16 import { buildFollowActivity } from './send-follow'
17 import { buildLikeActivity } from './send-like'
18 import { buildAnnounceWithVideoAudience } from './send-announce'
19 import { logger } from '../../../helpers/logger'
20 import { buildDislikeActivity } from './send-dislike'
21 import {
22 MActor, MActorAudience,
23 MActorFollowActors,
24 MActorLight,
25 MVideo,
26 MVideoAccountLight,
27 MVideoRedundancyVideo,
28 MVideoShare
29 } from '../../../typings/models'
30
31 function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
32 const me = actorFollow.ActorFollower
33 const following = actorFollow.ActorFollowing
34
35 // Same server as ours
36 if (!following.serverId) return
37
38 logger.info('Creating job to send an unfollow request to %s.', following.url)
39
40 const followUrl = getActorFollowActivityPubUrl(me, following)
41 const undoUrl = getUndoActivityPubUrl(followUrl)
42
43 const followActivity = buildFollowActivity(followUrl, 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 = getVideoLikeActivityPubUrl(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 = getVideoDislikeActivityPubUrl(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 videoId = redundancyModel.getVideo().id
83 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
84 const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
85
86 return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t })
87 }
88
89 // ---------------------------------------------------------------------------
90
91 export {
92 sendUndoFollow,
93 sendUndoLike,
94 sendUndoDislike,
95 sendUndoAnnounce,
96 sendUndoCacheFile
97 }
98
99 // ---------------------------------------------------------------------------
100
101 function undoActivityData (
102 url: string,
103 byActor: MActorAudience,
104 object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
105 audience?: ActivityAudience
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 )
118 }
119
120 async function sendUndoVideoRelatedActivity (options: {
121 byActor: MActor
122 video: MVideoAccountLight
123 url: string
124 activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce
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 }