Fix redundancy totalVideos stats
[oweals/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 { ActorModel } from '../../../models/activitypub/actor'
5 import { VideoModel } from '../../../models/video/video'
6 import { VideoAbuseModel } from '../../../models/video/video-abuse'
7 import { VideoCommentModel } from '../../../models/video/video-comment'
8 import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
9 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
10 import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
11 import { logger } from '../../../helpers/logger'
12 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
13
14 async function sendCreateVideo (video: VideoModel, t: Transaction) {
15   if (video.privacy === VideoPrivacy.PRIVATE) return undefined
16
17   logger.info('Creating job to send video creation of %s.', video.url)
18
19   const byActor = video.VideoChannel.Account.Actor
20   const videoObject = video.toActivityPubObject()
21
22   const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
23   const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
24
25   return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
26 }
27
28 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel) {
29   if (!video.VideoChannel.Account.Actor.serverId) return // Local
30
31   const url = getVideoAbuseActivityPubUrl(videoAbuse)
32
33   logger.info('Creating job to send video abuse %s.', url)
34
35   // Custom audience, we only send the abuse to the origin instance
36   const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
37   const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience)
38
39   return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
40 }
41
42 async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) {
43   logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
44
45   const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id)
46   const redundancyObject = fileRedundancy.toActivityPubObject()
47
48   return sendVideoRelatedCreateActivity({
49     byActor,
50     video,
51     url: fileRedundancy.url,
52     object: redundancyObject
53   })
54 }
55
56 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
57   logger.info('Creating job to send comment %s.', comment.url)
58
59   const isOrigin = comment.Video.isOwned()
60
61   const byActor = comment.Account.Actor
62   const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
63   const commentObject = comment.toActivityPubObject(threadParentComments)
64
65   const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
66   // Add the actor that commented too
67   actorsInvolvedInComment.push(byActor)
68
69   const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
70
71   let audience: ActivityAudience
72   if (isOrigin) {
73     audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
74   } else {
75     audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
76   }
77
78   const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
79
80   // This was a reply, send it to the parent actors
81   const actorsException = [ byActor ]
82   await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
83
84   // Broadcast to our followers
85   await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
86
87   // Send to actors involved in the comment
88   if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
89
90   // Send to origin
91   return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
92 }
93
94 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
95   logger.info('Creating job to send view of %s.', video.url)
96
97   const url = getVideoViewActivityPubUrl(byActor, video)
98   const viewActivity = buildViewActivity(byActor, video)
99
100   return sendVideoRelatedCreateActivity({
101     // Use the server actor to send the view
102     byActor,
103     video,
104     url,
105     object: viewActivity,
106     transaction: t
107   })
108 }
109
110 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
111   logger.info('Creating job to dislike %s.', video.url)
112
113   const url = getVideoDislikeActivityPubUrl(byActor, video)
114   const dislikeActivity = buildDislikeActivity(byActor, video)
115
116   return sendVideoRelatedCreateActivity({
117     byActor,
118     video,
119     url,
120     object: dislikeActivity,
121     transaction: t
122   })
123 }
124
125 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
126   if (!audience) audience = getAudience(byActor)
127
128   return audiencify(
129     {
130       type: 'Create' as 'Create',
131       id: url + '/activity',
132       actor: byActor.url,
133       object: audiencify(object, audience)
134     },
135     audience
136   )
137 }
138
139 function buildDislikeActivity (byActor: ActorModel, video: VideoModel) {
140   return {
141     type: 'Dislike',
142     actor: byActor.url,
143     object: video.url
144   }
145 }
146
147 function buildViewActivity (byActor: ActorModel, video: VideoModel) {
148   return {
149     type: 'View',
150     actor: byActor.url,
151     object: video.url
152   }
153 }
154
155 // ---------------------------------------------------------------------------
156
157 export {
158   sendCreateVideo,
159   sendVideoAbuse,
160   buildCreateActivity,
161   sendCreateView,
162   sendCreateDislike,
163   buildDislikeActivity,
164   sendCreateVideoComment,
165   sendCreateCacheFile
166 }
167
168 // ---------------------------------------------------------------------------
169
170 async function sendVideoRelatedCreateActivity (options: {
171   byActor: ActorModel,
172   video: VideoModel,
173   url: string,
174   object: any,
175   transaction?: Transaction
176 }) {
177   const activityBuilder = (audience: ActivityAudience) => {
178     return buildCreateActivity(options.url, options.byActor, options.object, audience)
179   }
180
181   return sendVideoRelatedActivity(activityBuilder, options)
182 }