]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { Transaction } from 'sequelize' |
0f320037 C |
2 | import { |
3 | ActivityAnnounce, | |
4 | ActivityAudience, | |
5 | ActivityCreate, | |
6 | ActivityFollow, | |
7 | ActivityLike, | |
8 | ActivityUndo | |
9 | } from '../../../../shared/models/activitypub' | |
50d6de9c C |
10 | import { ActorModel } from '../../../models/activitypub/actor' |
11 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
3fd3ab2d | 12 | import { VideoModel } from '../../../models/video/video' |
50d6de9c | 13 | import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url' |
e251f170 C |
14 | import { broadcastToFollowers, unicastTo } from './utils' |
15 | import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience, getVideoAudience } from '../audience' | |
c48e82b5 C |
16 | import { buildCreateActivity, buildDislikeActivity } from './send-create' |
17 | import { buildFollowActivity } from './send-follow' | |
18 | import { buildLikeActivity } from './send-like' | |
0f320037 | 19 | import { VideoShareModel } from '../../../models/video/video-share' |
c48e82b5 | 20 | import { buildAnnounceWithVideoAudience } from './send-announce' |
8e0fd45e | 21 | import { logger } from '../../../helpers/logger' |
c48e82b5 | 22 | import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' |
54141398 | 23 | |
50d6de9c C |
24 | async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) { |
25 | const me = actorFollow.ActorFollower | |
26 | const following = actorFollow.ActorFollowing | |
54141398 | 27 | |
06a05d5f C |
28 | // Same server as ours |
29 | if (!following.serverId) return | |
30 | ||
8e0fd45e C |
31 | logger.info('Creating job to send an unfollow request to %s.', following.url) |
32 | ||
50d6de9c | 33 | const followUrl = getActorFollowActivityPubUrl(actorFollow) |
54141398 C |
34 | const undoUrl = getUndoActivityPubUrl(followUrl) |
35 | ||
c48e82b5 C |
36 | const followActivity = buildFollowActivity(followUrl, me, following) |
37 | const undoActivity = undoActivityData(undoUrl, me, followActivity) | |
54141398 | 38 | |
c48e82b5 | 39 | return unicastTo(undoActivity, me, following.inboxUrl) |
54141398 C |
40 | } |
41 | ||
07197db4 | 42 | async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) { |
8e0fd45e C |
43 | logger.info('Creating job to undo a like of video %s.', video.url) |
44 | ||
50d6de9c | 45 | const likeUrl = getVideoLikeActivityPubUrl(byActor, video) |
0032ebe9 C |
46 | const undoUrl = getUndoActivityPubUrl(likeUrl) |
47 | ||
50d6de9c | 48 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) |
c48e82b5 | 49 | const likeActivity = buildLikeActivity(likeUrl, byActor, video) |
0032ebe9 | 50 | |
07197db4 C |
51 | // Send to origin |
52 | if (video.isOwned() === false) { | |
e251f170 | 53 | const audience = getVideoAudience(video, actorsInvolvedInVideo) |
c48e82b5 | 54 | const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience) |
0032ebe9 | 55 | |
c48e82b5 | 56 | return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) |
07197db4 | 57 | } |
0032ebe9 | 58 | |
07197db4 | 59 | const audience = getObjectFollowersAudience(actorsInvolvedInVideo) |
c48e82b5 | 60 | const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience) |
0032ebe9 | 61 | |
50d6de9c | 62 | const followersException = [ byActor ] |
c48e82b5 | 63 | return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) |
0032ebe9 C |
64 | } |
65 | ||
07197db4 | 66 | async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) { |
8e0fd45e C |
67 | logger.info('Creating job to undo a dislike of video %s.', video.url) |
68 | ||
50d6de9c | 69 | const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video) |
0032ebe9 C |
70 | const undoUrl = getUndoActivityPubUrl(dislikeUrl) |
71 | ||
50d6de9c | 72 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) |
c48e82b5 C |
73 | const dislikeActivity = buildDislikeActivity(byActor, video) |
74 | const createDislikeActivity = buildCreateActivity(dislikeUrl, byActor, dislikeActivity) | |
0032ebe9 | 75 | |
07197db4 | 76 | if (video.isOwned() === false) { |
e251f170 | 77 | const audience = getVideoAudience(video, actorsInvolvedInVideo) |
c48e82b5 | 78 | const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity, audience) |
0032ebe9 | 79 | |
c48e82b5 | 80 | return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) |
07197db4 | 81 | } |
0032ebe9 | 82 | |
c48e82b5 | 83 | const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity) |
0032ebe9 | 84 | |
50d6de9c | 85 | const followersException = [ byActor ] |
c48e82b5 | 86 | return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) |
0032ebe9 C |
87 | } |
88 | ||
0f320037 | 89 | async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) { |
8e0fd45e C |
90 | logger.info('Creating job to undo announce %s.', videoShare.url) |
91 | ||
0f320037 C |
92 | const undoUrl = getUndoActivityPubUrl(videoShare.url) |
93 | ||
c48e82b5 C |
94 | const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t) |
95 | const undoActivity = undoActivityData(undoUrl, byActor, announceActivity) | |
0f320037 C |
96 | |
97 | const followersException = [ byActor ] | |
c48e82b5 C |
98 | return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) |
99 | } | |
100 | ||
101 | async function sendUndoCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel, t: Transaction) { | |
102 | logger.info('Creating job to undo cache file %s.', redundancyModel.url) | |
103 | ||
104 | const undoUrl = getUndoActivityPubUrl(redundancyModel.url) | |
105 | ||
106 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.VideoFile.Video.id) | |
107 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) | |
108 | ||
109 | const audience = getVideoAudience(video, actorsInvolvedInVideo) | |
110 | const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject()) | |
111 | ||
112 | const undoActivity = undoActivityData(undoUrl, byActor, createActivity, audience) | |
113 | ||
114 | return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) | |
0f320037 C |
115 | } |
116 | ||
54141398 C |
117 | // --------------------------------------------------------------------------- |
118 | ||
119 | export { | |
0032ebe9 | 120 | sendUndoFollow, |
07197db4 | 121 | sendUndoLike, |
0f320037 | 122 | sendUndoDislike, |
c48e82b5 C |
123 | sendUndoAnnounce, |
124 | sendUndoCacheFile | |
54141398 C |
125 | } |
126 | ||
127 | // --------------------------------------------------------------------------- | |
128 | ||
2186386c | 129 | function undoActivityData ( |
63c93323 | 130 | url: string, |
50d6de9c | 131 | byActor: ActorModel, |
0f320037 | 132 | object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce, |
63c93323 | 133 | audience?: ActivityAudience |
2186386c C |
134 | ): ActivityUndo { |
135 | if (!audience) audience = getAudience(byActor) | |
136 | ||
137 | return audiencify( | |
138 | { | |
139 | type: 'Undo' as 'Undo', | |
140 | id: url, | |
141 | actor: byActor.url, | |
142 | object | |
143 | }, | |
144 | audience | |
145 | ) | |
54141398 | 146 | } |