]>
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, sendVideoRelatedActivity, unicastTo } from './utils' | |
15 | import { audiencify, getAudience } 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 sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) { | |
43 | logger.info('Creating job to undo announce %s.', videoShare.url) | |
44 | ||
45 | const undoUrl = getUndoActivityPubUrl(videoShare.url) | |
46 | ||
47 | const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t) | |
48 | const undoActivity = undoActivityData(undoUrl, byActor, announceActivity) | |
49 | ||
50 | const followersException = [ byActor ] | |
51 | return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) | |
52 | } | |
53 | ||
54 | async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
55 | logger.info('Creating job to undo a like of video %s.', video.url) | |
56 | ||
57 | const likeUrl = getVideoLikeActivityPubUrl(byActor, video) | |
58 | const likeActivity = buildLikeActivity(likeUrl, byActor, video) | |
59 | ||
60 | return sendUndoVideoRelatedActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t }) | |
61 | } | |
62 | ||
63 | async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) { | |
64 | logger.info('Creating job to undo a dislike of video %s.', video.url) | |
65 | ||
66 | const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video) | |
67 | const dislikeActivity = buildDislikeActivity(byActor, video) | |
68 | const createDislikeActivity = buildCreateActivity(dislikeUrl, byActor, dislikeActivity) | |
69 | ||
70 | return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: createDislikeActivity, 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 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.VideoFile.Video.id) | |
77 | const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject()) | |
78 | ||
79 | return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t }) | |
80 | } | |
81 | ||
82 | // --------------------------------------------------------------------------- | |
83 | ||
84 | export { | |
85 | sendUndoFollow, | |
86 | sendUndoLike, | |
87 | sendUndoDislike, | |
88 | sendUndoAnnounce, | |
89 | sendUndoCacheFile | |
90 | } | |
91 | ||
92 | // --------------------------------------------------------------------------- | |
93 | ||
94 | function undoActivityData ( | |
95 | url: string, | |
96 | byActor: ActorModel, | |
97 | object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce, | |
98 | audience?: ActivityAudience | |
99 | ): ActivityUndo { | |
100 | if (!audience) audience = getAudience(byActor) | |
101 | ||
102 | return audiencify( | |
103 | { | |
104 | type: 'Undo' as 'Undo', | |
105 | id: url, | |
106 | actor: byActor.url, | |
107 | object | |
108 | }, | |
109 | audience | |
110 | ) | |
111 | } | |
112 | ||
113 | async function sendUndoVideoRelatedActivity (options: { | |
114 | byActor: ActorModel, | |
115 | video: VideoModel, | |
116 | url: string, | |
117 | activity: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce, | |
118 | transaction: Transaction | |
119 | }) { | |
120 | const activityBuilder = (audience: ActivityAudience) => { | |
121 | const undoUrl = getUndoActivityPubUrl(options.url) | |
122 | ||
123 | return undoActivityData(undoUrl, options.byActor, options.activity, audience) | |
124 | } | |
125 | ||
126 | return sendVideoRelatedActivity(activityBuilder, options) | |
127 | } |