]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize' | |
2 | import { getServerActor } from '@server/models/application/application' | |
3 | import { ActivityAudience, ActivityDelete } from '@shared/models' | |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { ActorModel } from '../../../models/actor/actor' | |
6 | import { VideoCommentModel } from '../../../models/video/video-comment' | |
7 | import { VideoShareModel } from '../../../models/video/video-share' | |
8 | import { MActorUrl } from '../../../types/models' | |
9 | import { MCommentOwnerVideo, MVideoAccountLight, MVideoPlaylistFullSummary } from '../../../types/models/video' | |
10 | import { audiencify } from '../audience' | |
11 | import { getDeleteActivityPubUrl } from '../url' | |
12 | import { getActorsInvolvedInVideo, getVideoCommentAudience } from './shared' | |
13 | import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './shared/send-utils' | |
14 | ||
15 | async function sendDeleteVideo (video: MVideoAccountLight, transaction: Transaction) { | |
16 | logger.info('Creating job to broadcast delete of video %s.', video.url) | |
17 | ||
18 | const byActor = video.VideoChannel.Account.Actor | |
19 | ||
20 | const activityBuilder = (audience: ActivityAudience) => { | |
21 | const url = getDeleteActivityPubUrl(video.url) | |
22 | ||
23 | return buildDeleteActivity(url, video.url, byActor, audience) | |
24 | } | |
25 | ||
26 | return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction }) | |
27 | } | |
28 | ||
29 | async function sendDeleteActor (byActor: ActorModel, t: Transaction) { | |
30 | logger.info('Creating job to broadcast delete of actor %s.', byActor.url) | |
31 | ||
32 | const url = getDeleteActivityPubUrl(byActor.url) | |
33 | const activity = buildDeleteActivity(url, byActor.url, byActor) | |
34 | ||
35 | const actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t) | |
36 | ||
37 | // In case the actor did not have any videos | |
38 | const serverActor = await getServerActor() | |
39 | actorsInvolved.push(serverActor) | |
40 | ||
41 | actorsInvolved.push(byActor) | |
42 | ||
43 | return broadcastToFollowers(activity, byActor, actorsInvolved, t) | |
44 | } | |
45 | ||
46 | async function sendDeleteVideoComment (videoComment: MCommentOwnerVideo, t: Transaction) { | |
47 | logger.info('Creating job to send delete of comment %s.', videoComment.url) | |
48 | ||
49 | const isVideoOrigin = videoComment.Video.isOwned() | |
50 | ||
51 | const url = getDeleteActivityPubUrl(videoComment.url) | |
52 | const byActor = videoComment.isOwned() | |
53 | ? videoComment.Account.Actor | |
54 | : videoComment.Video.VideoChannel.Account.Actor | |
55 | ||
56 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t) | |
57 | const threadParentCommentsFiltered = threadParentComments.filter(c => !c.isDeleted()) | |
58 | ||
59 | const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t) | |
60 | actorsInvolvedInComment.push(byActor) // Add the actor that commented the video | |
61 | ||
62 | const audience = getVideoCommentAudience(videoComment, threadParentCommentsFiltered, actorsInvolvedInComment, isVideoOrigin) | |
63 | const activity = buildDeleteActivity(url, videoComment.url, byActor, audience) | |
64 | ||
65 | // This was a reply, send it to the parent actors | |
66 | const actorsException = [ byActor ] | |
67 | await broadcastToActors(activity, byActor, threadParentCommentsFiltered.map(c => c.Account.Actor), t, actorsException) | |
68 | ||
69 | // Broadcast to our followers | |
70 | await broadcastToFollowers(activity, byActor, [ byActor ], t) | |
71 | ||
72 | // Send to actors involved in the comment | |
73 | if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException) | |
74 | ||
75 | // Send to origin | |
76 | t.afterCommit(() => unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.getSharedInbox())) | |
77 | } | |
78 | ||
79 | async function sendDeleteVideoPlaylist (videoPlaylist: MVideoPlaylistFullSummary, t: Transaction) { | |
80 | logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url) | |
81 | ||
82 | const byActor = videoPlaylist.OwnerAccount.Actor | |
83 | ||
84 | const url = getDeleteActivityPubUrl(videoPlaylist.url) | |
85 | const activity = buildDeleteActivity(url, videoPlaylist.url, byActor) | |
86 | ||
87 | const serverActor = await getServerActor() | |
88 | const toFollowersOf = [ byActor, serverActor ] | |
89 | ||
90 | if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor) | |
91 | ||
92 | return broadcastToFollowers(activity, byActor, toFollowersOf, t) | |
93 | } | |
94 | ||
95 | // --------------------------------------------------------------------------- | |
96 | ||
97 | export { | |
98 | sendDeleteVideo, | |
99 | sendDeleteActor, | |
100 | sendDeleteVideoComment, | |
101 | sendDeleteVideoPlaylist | |
102 | } | |
103 | ||
104 | // --------------------------------------------------------------------------- | |
105 | ||
106 | function buildDeleteActivity (url: string, object: string, byActor: MActorUrl, audience?: ActivityAudience): ActivityDelete { | |
107 | const activity = { | |
108 | type: 'Delete' as 'Delete', | |
109 | id: url, | |
110 | actor: byActor.url, | |
111 | object | |
112 | } | |
113 | ||
114 | if (audience) return audiencify(activity, audience) | |
115 | ||
116 | return activity | |
117 | } |