]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-create.ts
Fix announce activities
[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,
18 getOriginVideoCommentAudience,
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
ea44f375
C
43async function sendCreateVideoCommentToOrigin (comment: VideoCommentModel, t: Transaction) {
44 const byActor = comment.Account.Actor
d7e70384
C
45 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
46 const commentObject = comment.toActivityPubObject(threadParentComments)
ea44f375 47
93ef8a9d
C
48 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
49 actorsInvolvedInComment.push(byActor)
50 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
ea44f375 51
ea44f375
C
52 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
53
93ef8a9d
C
54 // This was a reply, send it to the parent actors
55 const actorsException = [ byActor ]
94a5ff8a 56 await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
93ef8a9d
C
57
58 // Broadcast to our followers
59 await broadcastToFollowers(data, byActor, [ byActor ], t)
60
61 // Send to origin
94a5ff8a 62 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
ea44f375
C
63}
64
65async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
66 const byActor = comment.Account.Actor
d7e70384
C
67 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
68 const commentObject = comment.toActivityPubObject(threadParentComments)
ea44f375 69
93ef8a9d
C
70 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
71 actorsInvolvedInComment.push(byActor)
72
73 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
ea44f375
C
74 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
75
93ef8a9d
C
76 // This was a reply, send it to the parent actors
77 const actorsException = [ byActor ]
94a5ff8a 78 await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
93ef8a9d
C
79
80 // Broadcast to our followers
81 await broadcastToFollowers(data, byActor, [ byActor ], t)
82
83 // Send to actors involved in the comment
84 return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
ea44f375
C
85}
86
50d6de9c
C
87async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
88 const url = getVideoViewActivityPubUrl(byActor, video)
ea44f375 89 const viewActivityData = createViewActivityData(byActor, video)
40ff5707 90
50d6de9c
C
91 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
92 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
ea44f375 93 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
54141398 94
94a5ff8a 95 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
54141398
C
96}
97
50d6de9c
C
98async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
99 const url = getVideoViewActivityPubUrl(byActor, video)
ea44f375 100 const viewActivityData = createViewActivityData(byActor, video)
40ff5707 101
50d6de9c
C
102 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
103 const audience = getObjectFollowersAudience(actorsToForwardView)
ea44f375 104 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
40ff5707 105
50d6de9c
C
106 // Use the server actor to send the view
107 const serverActor = await getServerActor()
93ef8a9d
C
108 const actorsException = [ byActor ]
109 return broadcastToFollowers(data, serverActor, actorsToForwardView, t, actorsException)
0032ebe9
C
110}
111
50d6de9c
C
112async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
113 const url = getVideoDislikeActivityPubUrl(byActor, video)
ea44f375 114 const dislikeActivityData = createDislikeActivityData(byActor, video)
0032ebe9 115
50d6de9c
C
116 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
117 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
ea44f375 118 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
0032ebe9 119
94a5ff8a 120 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
0032ebe9 121}
40ff5707 122
50d6de9c
C
123async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
124 const url = getVideoDislikeActivityPubUrl(byActor, video)
ea44f375 125 const dislikeActivityData = createDislikeActivityData(byActor, video)
0032ebe9 126
50d6de9c
C
127 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
128 const audience = getObjectFollowersAudience(actorsToForwardView)
ea44f375 129 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
0032ebe9 130
93ef8a9d
C
131 const actorsException = [ byActor ]
132 return broadcastToFollowers(data, byActor, actorsToForwardView, t, actorsException)
40ff5707
C
133}
134
3fd3ab2d
C
135async function createActivityData (
136 url: string,
50d6de9c 137 byActor: ActorModel,
3fd3ab2d
C
138 object: any,
139 t: Transaction,
140 audience?: ActivityAudience
141): Promise<ActivityCreate> {
40ff5707 142 if (!audience) {
50d6de9c 143 audience = await getAudience(byActor, t)
40ff5707 144 }
c986175d 145
e12a0092 146 return audiencify({
54141398
C
147 type: 'Create',
148 id: url,
50d6de9c 149 actor: byActor.url,
e12a0092
C
150 object: audiencify(object, audience)
151 }, audience)
54141398
C
152}
153
50d6de9c 154function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
3fd3ab2d 155 return {
0032ebe9 156 type: 'Dislike',
50d6de9c 157 actor: byActor.url,
0032ebe9
C
158 object: video.url
159 }
0032ebe9
C
160}
161
ea44f375
C
162function createViewActivityData (byActor: ActorModel, video: VideoModel) {
163 return {
164 type: 'View',
165 actor: byActor.url,
166 object: video.url
167 }
168}
169
54141398
C
170// ---------------------------------------------------------------------------
171
172export {
50d6de9c 173 sendCreateVideo,
54141398 174 sendVideoAbuse,
40ff5707
C
175 createActivityData,
176 sendCreateViewToOrigin,
0032ebe9
C
177 sendCreateViewToVideoFollowers,
178 sendCreateDislikeToOrigin,
179 sendCreateDislikeToVideoFollowers,
ea44f375
C
180 createDislikeActivityData,
181 sendCreateVideoCommentToOrigin,
182 sendCreateVideoCommentToVideoFollowers
54141398 183}