]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { Transaction } from 'sequelize' |
0f320037 C |
2 | import { |
3 | ActivityAnnounce, | |
4 | ActivityAudience, | |
453e83ea C |
5 | ActivityCreate, |
6 | ActivityDislike, | |
0f320037 C |
7 | ActivityFollow, |
8 | ActivityLike, | |
9 | ActivityUndo | |
10 | } from '../../../../shared/models/activitypub' | |
3fd3ab2d | 11 | import { VideoModel } from '../../../models/video/video' |
50d6de9c | 12 | import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url' |
a2377d15 C |
13 | import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils' |
14 | import { audiencify, getAudience } from '../audience' | |
1e7eb25f | 15 | import { buildCreateActivity } from './send-create' |
c48e82b5 C |
16 | import { buildFollowActivity } from './send-follow' |
17 | import { buildLikeActivity } from './send-like' | |
c48e82b5 | 18 | import { buildAnnounceWithVideoAudience } from './send-announce' |
8e0fd45e | 19 | import { logger } from '../../../helpers/logger' |
1e7eb25f | 20 | import { buildDislikeActivity } from './send-dislike' |
453e83ea C |
21 | import { |
22 | MActor, MActorAudience, | |
23 | MActorFollowActors, | |
24 | MActorLight, | |
25 | MVideo, | |
26 | MVideoAccountLight, | |
27 | MVideoRedundancyVideo, | |
28 | MVideoShare | |
29 | } from '../../../typings/models' | |
30 | ||
a1587156 | 31 | function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) { |
50d6de9c C |
32 | const me = actorFollow.ActorFollower |
33 | const following = actorFollow.ActorFollowing | |
54141398 | 34 | |
06a05d5f C |
35 | // Same server as ours |
36 | if (!following.serverId) return | |
37 | ||
8e0fd45e C |
38 | logger.info('Creating job to send an unfollow request to %s.', following.url) |
39 | ||
5b9c965d | 40 | const followUrl = getActorFollowActivityPubUrl(me, following) |
54141398 C |
41 | const undoUrl = getUndoActivityPubUrl(followUrl) |
42 | ||
c48e82b5 C |
43 | const followActivity = buildFollowActivity(followUrl, me, following) |
44 | const undoActivity = undoActivityData(undoUrl, me, followActivity) | |
54141398 | 45 | |
2284f202 | 46 | t.afterCommit(() => unicastTo(undoActivity, me, following.inboxUrl)) |
54141398 C |
47 | } |
48 | ||
453e83ea | 49 | async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, t: Transaction) { |
a2377d15 | 50 | logger.info('Creating job to undo announce %s.', videoShare.url) |
8e0fd45e | 51 | |
a2377d15 | 52 | const undoUrl = getUndoActivityPubUrl(videoShare.url) |
0032ebe9 | 53 | |
a2377d15 C |
54 | const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t) |
55 | const undoActivity = undoActivityData(undoUrl, byActor, announceActivity) | |
0032ebe9 | 56 | |
a2377d15 C |
57 | const followersException = [ byActor ] |
58 | return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) | |
59 | } | |
0032ebe9 | 60 | |
453e83ea | 61 | async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { |
a2377d15 | 62 | logger.info('Creating job to undo a like of video %s.', video.url) |
0032ebe9 | 63 | |
a2377d15 C |
64 | const likeUrl = getVideoLikeActivityPubUrl(byActor, video) |
65 | const likeActivity = buildLikeActivity(likeUrl, byActor, video) | |
0032ebe9 | 66 | |
a2377d15 | 67 | return sendUndoVideoRelatedActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t }) |
0032ebe9 C |
68 | } |
69 | ||
453e83ea | 70 | async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { |
8e0fd45e C |
71 | logger.info('Creating job to undo a dislike of video %s.', video.url) |
72 | ||
50d6de9c | 73 | const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video) |
5c6d985f | 74 | const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video) |
0032ebe9 | 75 | |
1e7eb25f | 76 | return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t }) |
c48e82b5 C |
77 | } |
78 | ||
453e83ea | 79 | async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, t: Transaction) { |
c48e82b5 C |
80 | logger.info('Creating job to undo cache file %s.', redundancyModel.url) |
81 | ||
09209296 C |
82 | const videoId = redundancyModel.getVideo().id |
83 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) | |
c48e82b5 C |
84 | const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject()) |
85 | ||
a2377d15 | 86 | return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t }) |
0f320037 C |
87 | } |
88 | ||
54141398 C |
89 | // --------------------------------------------------------------------------- |
90 | ||
91 | export { | |
0032ebe9 | 92 | sendUndoFollow, |
07197db4 | 93 | sendUndoLike, |
0f320037 | 94 | sendUndoDislike, |
c48e82b5 C |
95 | sendUndoAnnounce, |
96 | sendUndoCacheFile | |
54141398 C |
97 | } |
98 | ||
99 | // --------------------------------------------------------------------------- | |
100 | ||
2186386c | 101 | function undoActivityData ( |
63c93323 | 102 | url: string, |
453e83ea | 103 | byActor: MActorAudience, |
1e7eb25f | 104 | object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce, |
63c93323 | 105 | audience?: ActivityAudience |
2186386c C |
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 | ) | |
54141398 | 118 | } |
a2377d15 C |
119 | |
120 | async function sendUndoVideoRelatedActivity (options: { | |
a1587156 C |
121 | byActor: MActor |
122 | video: MVideoAccountLight | |
123 | url: string | |
124 | activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce | |
a2377d15 C |
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 | } |