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