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