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