]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-create.ts
Introduce tags to the logger
[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'
ea44f375 4import { VideoCommentModel } from '../../../models/video/video-comment'
a2377d15
C
5import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
6import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
452b3bea 7import { logger, loggerTagsFactory } from '../../../helpers/logger'
418d092a 8import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
453e83ea
C
9import {
10 MActorLight,
11 MCommentOwnerVideo,
12 MVideoAccountLight,
13 MVideoAP,
14 MVideoPlaylistFull,
15 MVideoRedundancyFileVideo,
16 MVideoRedundancyStreamingPlaylistVideo
26d6bf65 17} from '../../../types/models'
8dc8a34e 18import { getServerActor } from '@server/models/application/application'
e307e4fc 19import { ContextType } from '@shared/models/activitypub/context'
453e83ea 20
452b3bea
C
21const lTags = loggerTagsFactory('ap', 'create')
22
453e83ea 23async function sendCreateVideo (video: MVideoAP, t: Transaction) {
22a73cb8 24 if (!video.hasPrivacyForFederation()) return undefined
54141398 25
452b3bea 26 logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid))
8e0fd45e 27
e12a0092 28 const byActor = video.VideoChannel.Account.Actor
50d6de9c 29 const videoObject = video.toActivityPubObject()
e12a0092 30
2186386c 31 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
c48e82b5 32 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
54141398 33
c48e82b5 34 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
54141398
C
35}
36
453e83ea
C
37async function sendCreateCacheFile (
38 byActor: MActorLight,
39 video: MVideoAccountLight,
40 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
41) {
452b3bea 42 logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid))
c48e82b5 43
a2377d15
C
44 return sendVideoRelatedCreateActivity({
45 byActor,
46 video,
47 url: fileRedundancy.url,
084a2cd0
C
48 object: fileRedundancy.toActivityPubObject(),
49 contextType: 'CacheFile'
a2377d15 50 })
40ff5707
C
51}
52
453e83ea 53async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transaction) {
418d092a
C
54 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
55
452b3bea 56 logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid))
418d092a
C
57
58 const byActor = playlist.OwnerAccount.Actor
59 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
60
df0b219d 61 const object = await playlist.toActivityPubObject(null, t)
418d092a
C
62 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
63
64 const serverActor = await getServerActor()
65 const toFollowersOf = [ byActor, serverActor ]
66
67 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
68
69 return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
70}
71
453e83ea 72async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transaction) {
8e0fd45e
C
73 logger.info('Creating job to send comment %s.', comment.url)
74
07197db4
C
75 const isOrigin = comment.Video.isOwned()
76
ea44f375 77 const byActor = comment.Account.Actor
d7e70384
C
78 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
79 const commentObject = comment.toActivityPubObject(threadParentComments)
ea44f375 80
93ef8a9d 81 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
a2377d15 82 // Add the actor that commented too
93ef8a9d 83 actorsInvolvedInComment.push(byActor)
93ef8a9d 84
6cb55644
C
85 const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
86 .map(c => c.Account.Actor)
ea44f375 87
07197db4
C
88 let audience: ActivityAudience
89 if (isOrigin) {
90 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
91 } else {
a2377d15 92 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
07197db4 93 }
93ef8a9d 94
c48e82b5 95 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
ea44f375 96
93ef8a9d
C
97 // This was a reply, send it to the parent actors
98 const actorsException = [ byActor ]
2284f202 99 await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException)
93ef8a9d
C
100
101 // Broadcast to our followers
c48e82b5 102 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
93ef8a9d
C
103
104 // Send to actors involved in the comment
c48e82b5 105 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
07197db4
C
106
107 // Send to origin
47581df0 108 t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.getSharedInbox()))
ea44f375
C
109}
110
453e83ea 111function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
2186386c
C
112 if (!audience) audience = getAudience(byActor)
113
114 return audiencify(
115 {
116 type: 'Create' as 'Create',
117 id: url + '/activity',
118 actor: byActor.url,
119 object: audiencify(object, audience)
120 },
121 audience
122 )
54141398
C
123}
124
125// ---------------------------------------------------------------------------
126
127export {
50d6de9c 128 sendCreateVideo,
c48e82b5 129 buildCreateActivity,
c48e82b5 130 sendCreateVideoComment,
418d092a 131 sendCreateVideoPlaylist,
c48e82b5 132 sendCreateCacheFile
54141398 133}
a2377d15
C
134
135// ---------------------------------------------------------------------------
136
137async function sendVideoRelatedCreateActivity (options: {
a1587156
C
138 byActor: MActorLight
139 video: MVideoAccountLight
140 url: string
141 object: any
a2377d15 142 transaction?: Transaction
084a2cd0 143 contextType?: ContextType
a2377d15
C
144}) {
145 const activityBuilder = (audience: ActivityAudience) => {
146 return buildCreateActivity(options.url, options.byActor, options.object, audience)
147 }
148
149 return sendVideoRelatedActivity(activityBuilder, options)
150}