]>
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' | |
d8553faa | 16 | import { createActivityData, createDislikeActivityData } from './send-create' |
54141398 | 17 | import { followActivityData } from './send-follow' |
0032ebe9 | 18 | import { likeActivityData } from './send-like' |
0f320037 C |
19 | import { VideoShareModel } from '../../../models/video/video-share' |
20 | import { buildVideoAnnounce } from './send-announce' | |
8e0fd45e | 21 | import { logger } from '../../../helpers/logger' |
54141398 | 22 | |
50d6de9c C |
23 | async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) { |
24 | const me = actorFollow.ActorFollower | |
25 | const following = actorFollow.ActorFollowing | |
54141398 | 26 | |
06a05d5f C |
27 | // Same server as ours |
28 | if (!following.serverId) return | |
29 | ||
8e0fd45e C |
30 | logger.info('Creating job to send an unfollow request to %s.', following.url) |
31 | ||
50d6de9c | 32 | const followUrl = getActorFollowActivityPubUrl(actorFollow) |
54141398 C |
33 | const undoUrl = getUndoActivityPubUrl(followUrl) |
34 | ||
25ed141c | 35 | const object = followActivityData(followUrl, me, following) |
2186386c | 36 | const data = undoActivityData(undoUrl, me, object) |
54141398 | 37 | |
94a5ff8a | 38 | return unicastTo(data, me, following.inboxUrl) |
54141398 C |
39 | } |
40 | ||
07197db4 | 41 | async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) { |
8e0fd45e C |
42 | logger.info('Creating job to undo a like of video %s.', video.url) |
43 | ||
50d6de9c | 44 | const likeUrl = getVideoLikeActivityPubUrl(byActor, video) |
0032ebe9 C |
45 | const undoUrl = getUndoActivityPubUrl(likeUrl) |
46 | ||
50d6de9c | 47 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) |
2186386c | 48 | const object = likeActivityData(likeUrl, byActor, video) |
0032ebe9 | 49 | |
07197db4 C |
50 | // Send to origin |
51 | if (video.isOwned() === false) { | |
e251f170 | 52 | const audience = getVideoAudience(video, actorsInvolvedInVideo) |
2186386c | 53 | const data = undoActivityData(undoUrl, byActor, object, audience) |
0032ebe9 | 54 | |
07197db4 C |
55 | return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) |
56 | } | |
0032ebe9 | 57 | |
07197db4 | 58 | const audience = getObjectFollowersAudience(actorsInvolvedInVideo) |
2186386c | 59 | const data = undoActivityData(undoUrl, byActor, object, audience) |
0032ebe9 | 60 | |
50d6de9c | 61 | const followersException = [ byActor ] |
07197db4 | 62 | return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException) |
0032ebe9 C |
63 | } |
64 | ||
07197db4 | 65 | async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) { |
8e0fd45e C |
66 | logger.info('Creating job to undo a dislike of video %s.', video.url) |
67 | ||
50d6de9c | 68 | const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video) |
0032ebe9 C |
69 | const undoUrl = getUndoActivityPubUrl(dislikeUrl) |
70 | ||
50d6de9c | 71 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) |
50d6de9c | 72 | const dislikeActivity = createDislikeActivityData(byActor, video) |
2186386c | 73 | const object = createActivityData(dislikeUrl, byActor, dislikeActivity) |
0032ebe9 | 74 | |
07197db4 | 75 | if (video.isOwned() === false) { |
e251f170 | 76 | const audience = getVideoAudience(video, actorsInvolvedInVideo) |
2186386c | 77 | const data = undoActivityData(undoUrl, byActor, object, audience) |
0032ebe9 | 78 | |
07197db4 C |
79 | return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) |
80 | } | |
0032ebe9 | 81 | |
2186386c | 82 | const data = undoActivityData(undoUrl, byActor, object) |
0032ebe9 | 83 | |
50d6de9c | 84 | const followersException = [ byActor ] |
07197db4 | 85 | return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException) |
0032ebe9 C |
86 | } |
87 | ||
0f320037 | 88 | async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) { |
8e0fd45e C |
89 | logger.info('Creating job to undo announce %s.', videoShare.url) |
90 | ||
0f320037 C |
91 | const undoUrl = getUndoActivityPubUrl(videoShare.url) |
92 | ||
93 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) | |
94 | const object = await buildVideoAnnounce(byActor, videoShare, video, t) | |
2186386c | 95 | const data = undoActivityData(undoUrl, byActor, object) |
0f320037 C |
96 | |
97 | const followersException = [ byActor ] | |
98 | return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException) | |
99 | } | |
100 | ||
54141398 C |
101 | // --------------------------------------------------------------------------- |
102 | ||
103 | export { | |
0032ebe9 | 104 | sendUndoFollow, |
07197db4 | 105 | sendUndoLike, |
0f320037 C |
106 | sendUndoDislike, |
107 | sendUndoAnnounce | |
54141398 C |
108 | } |
109 | ||
110 | // --------------------------------------------------------------------------- | |
111 | ||
2186386c | 112 | function undoActivityData ( |
63c93323 | 113 | url: string, |
50d6de9c | 114 | byActor: ActorModel, |
0f320037 | 115 | object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce, |
63c93323 | 116 | audience?: ActivityAudience |
2186386c C |
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 | ) | |
54141398 | 129 | } |