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