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