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