]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-undo.ts
Add subscriptions endpoints to REST API
[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 ActivityFollow,
7 ActivityLike,
8 ActivityUndo
9 } from '../../../../shared/models/activitypub'
10 import { ActorModel } from '../../../models/activitypub/actor'
11 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
12 import { VideoModel } from '../../../models/video/video'
13 import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
14 import { broadcastToFollowers, unicastTo } from './utils'
15 import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience, getVideoAudience } from '../audience'
16 import { createActivityData, createDislikeActivityData } from './send-create'
17 import { followActivityData } from './send-follow'
18 import { likeActivityData } from './send-like'
19 import { VideoShareModel } from '../../../models/video/video-share'
20 import { buildVideoAnnounce } from './send-announce'
21 import { logger } from '../../../helpers/logger'
22
23 async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
24 const me = actorFollow.ActorFollower
25 const following = actorFollow.ActorFollowing
26
27 // Same server as ours
28 if (!following.serverId) return
29
30 logger.info('Creating job to send an unfollow request to %s.', following.url)
31
32 const followUrl = getActorFollowActivityPubUrl(actorFollow)
33 const undoUrl = getUndoActivityPubUrl(followUrl)
34
35 const object = followActivityData(followUrl, me, following)
36 const data = undoActivityData(undoUrl, me, object)
37
38 return unicastTo(data, me, following.inboxUrl)
39 }
40
41 async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
42 logger.info('Creating job to undo a like of video %s.', video.url)
43
44 const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
45 const undoUrl = getUndoActivityPubUrl(likeUrl)
46
47 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
48 const object = likeActivityData(likeUrl, byActor, video)
49
50 // Send to origin
51 if (video.isOwned() === false) {
52 const audience = getVideoAudience(video, actorsInvolvedInVideo)
53 const data = undoActivityData(undoUrl, byActor, object, audience)
54
55 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
56 }
57
58 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
59 const data = undoActivityData(undoUrl, byActor, object, audience)
60
61 const followersException = [ byActor ]
62 return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
63 }
64
65 async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
66 logger.info('Creating job to undo a dislike of video %s.', video.url)
67
68 const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
69 const undoUrl = getUndoActivityPubUrl(dislikeUrl)
70
71 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
72 const dislikeActivity = createDislikeActivityData(byActor, video)
73 const object = createActivityData(dislikeUrl, byActor, dislikeActivity)
74
75 if (video.isOwned() === false) {
76 const audience = getVideoAudience(video, actorsInvolvedInVideo)
77 const data = undoActivityData(undoUrl, byActor, object, audience)
78
79 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
80 }
81
82 const data = undoActivityData(undoUrl, byActor, object)
83
84 const followersException = [ byActor ]
85 return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
86 }
87
88 async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
89 logger.info('Creating job to undo announce %s.', videoShare.url)
90
91 const undoUrl = getUndoActivityPubUrl(videoShare.url)
92
93 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
94 const object = await buildVideoAnnounce(byActor, videoShare, video, t)
95 const data = undoActivityData(undoUrl, byActor, object)
96
97 const followersException = [ byActor ]
98 return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
99 }
100
101 // ---------------------------------------------------------------------------
102
103 export {
104 sendUndoFollow,
105 sendUndoLike,
106 sendUndoDislike,
107 sendUndoAnnounce
108 }
109
110 // ---------------------------------------------------------------------------
111
112 function undoActivityData (
113 url: string,
114 byActor: ActorModel,
115 object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce,
116 audience?: ActivityAudience
117 ): ActivityUndo {
118 if (!audience) audience = getAudience(byActor)
119
120 return audiencify(
121 {
122 type: 'Undo' as 'Undo',
123 id: url,
124 actor: byActor.url,
125 object
126 },
127 audience
128 )
129 }