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