]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize' | |
2 | import { | |
3 | ActivityAnnounce, | |
4 | ActivityAudience, | |
5 | ActivityCreate, ActivityDislike, | |
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, sendVideoRelatedActivity, unicastTo } from './utils' | |
15 | import { audiencify, getAudience } from '../audience' | |
16 | import { buildCreateActivity } 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 | import { buildDislikeActivity } from './send-dislike' | |
24 | ||
25 | async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) { | |
26 | const me = actorFollow.ActorFollower | |
27 | const following = actorFollow.ActorFollowing | |
28 | ||
29 | // Same server as ours | |
30 | if (!following.serverId) return | |
31 | ||
32 | logger.info('Creating job to send an unfollow request to %s.', following.url) | |
33 | ||
34 | const followUrl = getActorFollowActivityPubUrl(me, following) | |
35 | const undoUrl = getUndoActivityPubUrl(followUrl) | |
36 | ||
37 | const followActivity = buildFollowActivity(followUrl, me, following) | |
38 | const undoActivity = undoActivityData(undoUrl, me, followActivity) | |
39 | ||
40 | t.afterCommit(() => unicastTo(undoActivity, me, following.inboxUrl)) | |
41 | } | |
42 | ||
43 | async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) { | |
44 | logger.info('Creating job to undo announce %s.', videoShare.url) | |
45 | ||
46 | const undoUrl = getUndoActivityPubUrl(videoShare.url) | |
47 | ||
48 | const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t) | |
49 | const undoActivity = undoActivityData(undoUrl, byActor, announceActivity) | |
50 | ||
51 | const followersException = [ byActor ] | |
52 | return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) | |
53 | } | |
54 | ||
55 | async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
56 | logger.info('Creating job to undo a like of video %s.', video.url) | |
57 | ||
58 | const likeUrl = getVideoLikeActivityPubUrl(byActor, video) | |
59 | const likeActivity = buildLikeActivity(likeUrl, byActor, video) | |
60 | ||
61 | return sendUndoVideoRelatedActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t }) | |
62 | } | |
63 | ||
64 | async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
65 | logger.info('Creating job to undo a dislike of video %s.', video.url) | |
66 | ||
67 | const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video) | |
68 | const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video) | |
69 | ||
70 | return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t }) | |
71 | } | |
72 | ||
73 | async function sendUndoCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel, t: Transaction) { | |
74 | logger.info('Creating job to undo cache file %s.', redundancyModel.url) | |
75 | ||
76 | const videoId = redundancyModel.getVideo().id | |
77 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) | |
78 | const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject()) | |
79 | ||
80 | return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t }) | |
81 | } | |
82 | ||
83 | // --------------------------------------------------------------------------- | |
84 | ||
85 | export { | |
86 | sendUndoFollow, | |
87 | sendUndoLike, | |
88 | sendUndoDislike, | |
89 | sendUndoAnnounce, | |
90 | sendUndoCacheFile | |
91 | } | |
92 | ||
93 | // --------------------------------------------------------------------------- | |
94 | ||
95 | function undoActivityData ( | |
96 | url: string, | |
97 | byActor: ActorModel, | |
98 | object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce, | |
99 | audience?: ActivityAudience | |
100 | ): ActivityUndo { | |
101 | if (!audience) audience = getAudience(byActor) | |
102 | ||
103 | return audiencify( | |
104 | { | |
105 | type: 'Undo' as 'Undo', | |
106 | id: url, | |
107 | actor: byActor.url, | |
108 | object | |
109 | }, | |
110 | audience | |
111 | ) | |
112 | } | |
113 | ||
114 | async function sendUndoVideoRelatedActivity (options: { | |
115 | byActor: ActorModel, | |
116 | video: VideoModel, | |
117 | url: string, | |
118 | activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce, | |
119 | transaction: Transaction | |
120 | }) { | |
121 | const activityBuilder = (audience: ActivityAudience) => { | |
122 | const undoUrl = getUndoActivityPubUrl(options.url) | |
123 | ||
124 | return undoActivityData(undoUrl, options.byActor, options.activity, audience) | |
125 | } | |
126 | ||
127 | return sendVideoRelatedActivity(activityBuilder, options) | |
128 | } |