]>
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 | |
57e4e1c1 | 10 | } from '@shared/models' |
8e0fd45e | 11 | import { logger } from '../../../helpers/logger' |
de94ac86 | 12 | import { VideoModel } from '../../../models/video/video' |
453e83ea | 13 | import { |
de94ac86 C |
14 | MActor, |
15 | MActorAudience, | |
453e83ea C |
16 | MActorFollowActors, |
17 | MActorLight, | |
18 | MVideo, | |
19 | MVideoAccountLight, | |
20 | MVideoRedundancyVideo, | |
21 | MVideoShare | |
26d6bf65 | 22 | } from '../../../types/models' |
de94ac86 C |
23 | import { audiencify, getAudience } from '../audience' |
24 | import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url' | |
25 | import { buildAnnounceWithVideoAudience } from './send-announce' | |
26 | import { buildCreateActivity } from './send-create' | |
27 | import { buildDislikeActivity } from './send-dislike' | |
28 | import { buildFollowActivity } from './send-follow' | |
29 | import { buildLikeActivity } from './send-like' | |
57e4e1c1 | 30 | import { broadcastToFollowers, sendVideoActivityToOrigin, sendVideoRelatedActivity, unicastTo } from './shared/send-utils' |
453e83ea | 31 | |
a1587156 | 32 | function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) { |
50d6de9c C |
33 | const me = actorFollow.ActorFollower |
34 | const following = actorFollow.ActorFollowing | |
54141398 | 35 | |
06a05d5f C |
36 | // Same server as ours |
37 | if (!following.serverId) return | |
38 | ||
8e0fd45e C |
39 | logger.info('Creating job to send an unfollow request to %s.', following.url) |
40 | ||
de94ac86 | 41 | const undoUrl = getUndoActivityPubUrl(actorFollow.url) |
54141398 | 42 | |
de94ac86 | 43 | const followActivity = buildFollowActivity(actorFollow.url, me, following) |
c48e82b5 | 44 | const undoActivity = undoActivityData(undoUrl, me, followActivity) |
54141398 | 45 | |
2284f202 | 46 | t.afterCommit(() => unicastTo(undoActivity, me, following.inboxUrl)) |
54141398 C |
47 | } |
48 | ||
57e4e1c1 C |
49 | // --------------------------------------------------------------------------- |
50 | ||
453e83ea | 51 | async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, t: Transaction) { |
a2377d15 | 52 | logger.info('Creating job to undo announce %s.', videoShare.url) |
8e0fd45e | 53 | |
a2377d15 | 54 | const undoUrl = getUndoActivityPubUrl(videoShare.url) |
0032ebe9 | 55 | |
a2377d15 C |
56 | const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t) |
57 | const undoActivity = undoActivityData(undoUrl, byActor, announceActivity) | |
0032ebe9 | 58 | |
a2377d15 C |
59 | const followersException = [ byActor ] |
60 | return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) | |
61 | } | |
0032ebe9 | 62 | |
57e4e1c1 C |
63 | async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, t: Transaction) { |
64 | logger.info('Creating job to undo cache file %s.', redundancyModel.url) | |
65 | ||
66 | const associatedVideo = redundancyModel.getVideo() | |
67 | if (!associatedVideo) { | |
68 | logger.warn('Cannot send undo activity for redundancy %s: no video files associated.', redundancyModel.url) | |
69 | return | |
70 | } | |
71 | ||
72 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(associatedVideo.id) | |
73 | const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject()) | |
74 | ||
75 | return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t }) | |
76 | } | |
77 | ||
78 | // --------------------------------------------------------------------------- | |
79 | ||
453e83ea | 80 | async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { |
a2377d15 | 81 | logger.info('Creating job to undo a like of video %s.', video.url) |
0032ebe9 | 82 | |
de94ac86 | 83 | const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video) |
a2377d15 | 84 | const likeActivity = buildLikeActivity(likeUrl, byActor, video) |
0032ebe9 | 85 | |
57e4e1c1 | 86 | return sendUndoVideoToOriginActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t }) |
0032ebe9 C |
87 | } |
88 | ||
453e83ea | 89 | async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { |
8e0fd45e C |
90 | logger.info('Creating job to undo a dislike of video %s.', video.url) |
91 | ||
de94ac86 | 92 | const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video) |
5c6d985f | 93 | const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video) |
0032ebe9 | 94 | |
57e4e1c1 | 95 | return sendUndoVideoToOriginActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t }) |
0f320037 C |
96 | } |
97 | ||
54141398 C |
98 | // --------------------------------------------------------------------------- |
99 | ||
100 | export { | |
0032ebe9 | 101 | sendUndoFollow, |
07197db4 | 102 | sendUndoLike, |
0f320037 | 103 | sendUndoDislike, |
c48e82b5 C |
104 | sendUndoAnnounce, |
105 | sendUndoCacheFile | |
54141398 C |
106 | } |
107 | ||
108 | // --------------------------------------------------------------------------- | |
109 | ||
2186386c | 110 | function undoActivityData ( |
63c93323 | 111 | url: string, |
453e83ea | 112 | byActor: MActorAudience, |
1e7eb25f | 113 | object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce, |
63c93323 | 114 | audience?: ActivityAudience |
2186386c C |
115 | ): ActivityUndo { |
116 | if (!audience) audience = getAudience(byActor) | |
117 | ||
118 | return audiencify( | |
119 | { | |
120 | type: 'Undo' as 'Undo', | |
121 | id: url, | |
122 | actor: byActor.url, | |
123 | object | |
124 | }, | |
125 | audience | |
126 | ) | |
54141398 | 127 | } |
a2377d15 C |
128 | |
129 | async function sendUndoVideoRelatedActivity (options: { | |
a1587156 C |
130 | byActor: MActor |
131 | video: MVideoAccountLight | |
132 | url: string | |
57e4e1c1 | 133 | activity: ActivityFollow | ActivityCreate | ActivityAnnounce |
a2377d15 C |
134 | transaction: Transaction |
135 | }) { | |
136 | const activityBuilder = (audience: ActivityAudience) => { | |
137 | const undoUrl = getUndoActivityPubUrl(options.url) | |
138 | ||
139 | return undoActivityData(undoUrl, options.byActor, options.activity, audience) | |
140 | } | |
141 | ||
142 | return sendVideoRelatedActivity(activityBuilder, options) | |
143 | } | |
57e4e1c1 C |
144 | |
145 | async function sendUndoVideoToOriginActivity (options: { | |
146 | byActor: MActor | |
147 | video: MVideoAccountLight | |
148 | url: string | |
149 | activity: ActivityLike | ActivityDislike | |
150 | transaction: Transaction | |
151 | }) { | |
152 | const activityBuilder = (audience: ActivityAudience) => { | |
153 | const undoUrl = getUndoActivityPubUrl(options.url) | |
154 | ||
155 | return undoActivityData(undoUrl, options.byActor, options.activity, audience) | |
156 | } | |
157 | ||
158 | return sendVideoActivityToOrigin(activityBuilder, options) | |
159 | } |