]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
9db663be198c78a63c4494cc252697925b7a32f6
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-create.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
3 import { VideoPrivacy } from '../../../../shared/models/videos'
4 import { getServerActor } from '../../../helpers/utils'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoAbuseModel } from '../../../models/video/video-abuse'
8 import { VideoCommentModel } from '../../../models/video/video-comment'
9 import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
10 import {
11 audiencify, broadcastToActors, broadcastToFollowers, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience,
12 getOriginVideoAudience, getOriginVideoCommentAudience,
13 unicastTo
14 } from './misc'
15
16 async function sendCreateVideo (video: VideoModel, t: Transaction) {
17 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
18
19 const byActor = video.VideoChannel.Account.Actor
20 const videoObject = video.toActivityPubObject()
21
22 const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
23 const data = await createActivityData(video.url, byActor, videoObject, t, audience)
24
25 return broadcastToFollowers(data, byActor, [ byActor ], t)
26 }
27
28 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
29 const url = getVideoAbuseActivityPubUrl(videoAbuse)
30
31 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
32 const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
33
34 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
35 }
36
37 async function sendCreateVideoCommentToOrigin (comment: VideoCommentModel, t: Transaction) {
38 const byActor = comment.Account.Actor
39 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
40 const commentObject = comment.toActivityPubObject(threadParentComments)
41
42 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
43 actorsInvolvedInComment.push(byActor)
44 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
45
46 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
47
48 // This was a reply, send it to the parent actors
49 const actorsException = [ byActor ]
50 await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), t, actorsException)
51
52 // Broadcast to our followers
53 await broadcastToFollowers(data, byActor, [ byActor ], t)
54
55 // Send to origin
56 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl, t)
57 }
58
59 async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
60 const byActor = comment.Account.Actor
61 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
62 const commentObject = comment.toActivityPubObject(threadParentComments)
63
64 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
65 actorsInvolvedInComment.push(byActor)
66
67 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
68 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
69
70 // This was a reply, send it to the parent actors
71 const actorsException = [ byActor ]
72 await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), t, actorsException)
73
74 // Broadcast to our followers
75 await broadcastToFollowers(data, byActor, [ byActor ], t)
76
77 // Send to actors involved in the comment
78 return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
79 }
80
81 async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
82 const url = getVideoViewActivityPubUrl(byActor, video)
83 const viewActivityData = createViewActivityData(byActor, video)
84
85 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
86 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
87 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
88
89 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
90 }
91
92 async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
93 const url = getVideoViewActivityPubUrl(byActor, video)
94 const viewActivityData = createViewActivityData(byActor, video)
95
96 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
97 const audience = getObjectFollowersAudience(actorsToForwardView)
98 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
99
100 // Use the server actor to send the view
101 const serverActor = await getServerActor()
102 const actorsException = [ byActor ]
103 return broadcastToFollowers(data, serverActor, actorsToForwardView, t, actorsException)
104 }
105
106 async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
107 const url = getVideoDislikeActivityPubUrl(byActor, video)
108 const dislikeActivityData = createDislikeActivityData(byActor, video)
109
110 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
111 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
112 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
113
114 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
115 }
116
117 async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
118 const url = getVideoDislikeActivityPubUrl(byActor, video)
119 const dislikeActivityData = createDislikeActivityData(byActor, video)
120
121 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
122 const audience = getObjectFollowersAudience(actorsToForwardView)
123 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
124
125 const actorsException = [ byActor ]
126 return broadcastToFollowers(data, byActor, actorsToForwardView, t, actorsException)
127 }
128
129 async function createActivityData (
130 url: string,
131 byActor: ActorModel,
132 object: any,
133 t: Transaction,
134 audience?: ActivityAudience
135 ): Promise<ActivityCreate> {
136 if (!audience) {
137 audience = await getAudience(byActor, t)
138 }
139
140 return audiencify({
141 type: 'Create',
142 id: url,
143 actor: byActor.url,
144 object: audiencify(object, audience)
145 }, audience)
146 }
147
148 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
149 return {
150 type: 'Dislike',
151 actor: byActor.url,
152 object: video.url
153 }
154 }
155
156 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
157 return {
158 type: 'View',
159 actor: byActor.url,
160 object: video.url
161 }
162 }
163
164 // ---------------------------------------------------------------------------
165
166 export {
167 sendCreateVideo,
168 sendVideoAbuse,
169 createActivityData,
170 sendCreateViewToOrigin,
171 sendCreateViewToVideoFollowers,
172 sendCreateDislikeToOrigin,
173 sendCreateDislikeToVideoFollowers,
174 createDislikeActivityData,
175 sendCreateVideoCommentToOrigin,
176 sendCreateVideoCommentToVideoFollowers
177 }