]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
Correctly send Flag/Dislike/View activities
[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 { ActorModel } from '../../../models/activitypub/actor'
5 import { VideoModel } from '../../../models/video/video'
6 import { VideoCommentModel } from '../../../models/video/video-comment'
7 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
8 import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
9 import { logger } from '../../../helpers/logger'
10 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
11
12 async function sendCreateVideo (video: VideoModel, t: Transaction) {
13 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
14
15 logger.info('Creating job to send video creation of %s.', video.url)
16
17 const byActor = video.VideoChannel.Account.Actor
18 const videoObject = video.toActivityPubObject()
19
20 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
21 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
22
23 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
24 }
25
26 async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) {
27 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
28
29 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id)
30 const redundancyObject = fileRedundancy.toActivityPubObject()
31
32 return sendVideoRelatedCreateActivity({
33 byActor,
34 video,
35 url: fileRedundancy.url,
36 object: redundancyObject
37 })
38 }
39
40 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
41 logger.info('Creating job to send comment %s.', comment.url)
42
43 const isOrigin = comment.Video.isOwned()
44
45 const byActor = comment.Account.Actor
46 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
47 const commentObject = comment.toActivityPubObject(threadParentComments)
48
49 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
50 // Add the actor that commented too
51 actorsInvolvedInComment.push(byActor)
52
53 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
54
55 let audience: ActivityAudience
56 if (isOrigin) {
57 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
58 } else {
59 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
60 }
61
62 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
63
64 // This was a reply, send it to the parent actors
65 const actorsException = [ byActor ]
66 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
67
68 // Broadcast to our followers
69 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
70
71 // Send to actors involved in the comment
72 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
73
74 // Send to origin
75 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
76 }
77
78 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
79 if (!audience) audience = getAudience(byActor)
80
81 return audiencify(
82 {
83 type: 'Create' as 'Create',
84 id: url + '/activity',
85 actor: byActor.url,
86 object: audiencify(object, audience)
87 },
88 audience
89 )
90 }
91
92 // ---------------------------------------------------------------------------
93
94 export {
95 sendCreateVideo,
96 buildCreateActivity,
97 sendCreateVideoComment,
98 sendCreateCacheFile
99 }
100
101 // ---------------------------------------------------------------------------
102
103 async function sendVideoRelatedCreateActivity (options: {
104 byActor: ActorModel,
105 video: VideoModel,
106 url: string,
107 object: any,
108 transaction?: Transaction
109 }) {
110 const activityBuilder = (audience: ActivityAudience) => {
111 return buildCreateActivity(options.url, options.byActor, options.object, audience)
112 }
113
114 return sendVideoRelatedActivity(activityBuilder, options)
115 }