]>
Commit | Line | Data |
---|---|---|
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 { buildCreateActivity, buildDislikeActivity } from './send-create' | |
17 | import { buildFollowActivity } from './send-follow' | |
18 | import { buildLikeActivity } from './send-like' | |
19 | import { VideoShareModel } from '../../../models/video/video-share' | |
20 | import { buildAnnounceWithVideoAudience } from './send-announce' | |
21 | import { logger } from '../../../helpers/logger' | |
22 | import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' | |
23 | ||
24 | async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) { | |
25 | const me = actorFollow.ActorFollower | |
26 | const following = actorFollow.ActorFollowing | |
27 | ||
28 | // Same server as ours | |
29 | if (!following.serverId) return | |
30 | ||
31 | logger.info('Creating job to send an unfollow request to %s.', following.url) | |
32 | ||
33 | const followUrl = getActorFollowActivityPubUrl(actorFollow) | |
34 | const undoUrl = getUndoActivityPubUrl(followUrl) | |
35 | ||
36 | const followActivity = buildFollowActivity(followUrl, me, following) | |
37 | const undoActivity = undoActivityData(undoUrl, me, followActivity) | |
38 | ||
39 | return unicastTo(undoActivity, me, following.inboxUrl) | |
40 | } | |
41 | ||
42 | async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
43 | logger.info('Creating job to undo a like of video %s.', video.url) | |
44 | ||
45 | const likeUrl = getVideoLikeActivityPubUrl(byActor, video) | |
46 | const undoUrl = getUndoActivityPubUrl(likeUrl) | |
47 | ||
48 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) | |
49 | const likeActivity = buildLikeActivity(likeUrl, byActor, video) | |
50 | ||
51 | // Send to origin | |
52 | if (video.isOwned() === false) { | |
53 | const audience = getVideoAudience(video, actorsInvolvedInVideo) | |
54 | const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience) | |
55 | ||
56 | return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) | |
57 | } | |
58 | ||
59 | const audience = getObjectFollowersAudience(actorsInvolvedInVideo) | |
60 | const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience) | |
61 | ||
62 | const followersException = [ byActor ] | |
63 | return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) | |
64 | } | |
65 | ||
66 | async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
67 | logger.info('Creating job to undo a dislike of video %s.', video.url) | |
68 | ||
69 | const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video) | |
70 | const undoUrl = getUndoActivityPubUrl(dislikeUrl) | |
71 | ||
72 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) | |
73 | const dislikeActivity = buildDislikeActivity(byActor, video) | |
74 | const createDislikeActivity = buildCreateActivity(dislikeUrl, byActor, dislikeActivity) | |
75 | ||
76 | if (video.isOwned() === false) { | |
77 | const audience = getVideoAudience(video, actorsInvolvedInVideo) | |
78 | const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity, audience) | |
79 | ||
80 | return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) | |
81 | } | |
82 | ||
83 | const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity) | |
84 | ||
85 | const followersException = [ byActor ] | |
86 | return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) | |
87 | } | |
88 | ||
89 | async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) { | |
90 | logger.info('Creating job to undo announce %s.', videoShare.url) | |
91 | ||
92 | const undoUrl = getUndoActivityPubUrl(videoShare.url) | |
93 | ||
94 | const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t) | |
95 | const undoActivity = undoActivityData(undoUrl, byActor, announceActivity) | |
96 | ||
97 | const followersException = [ byActor ] | |
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) | |
115 | } | |
116 | ||
117 | // --------------------------------------------------------------------------- | |
118 | ||
119 | export { | |
120 | sendUndoFollow, | |
121 | sendUndoLike, | |
122 | sendUndoDislike, | |
123 | sendUndoAnnounce, | |
124 | sendUndoCacheFile | |
125 | } | |
126 | ||
127 | // --------------------------------------------------------------------------- | |
128 | ||
129 | function undoActivityData ( | |
130 | url: string, | |
131 | byActor: ActorModel, | |
132 | object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce, | |
133 | audience?: ActivityAudience | |
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 | ) | |
146 | } |