]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
Merge branch 'release/3.1.0' into develop
[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 { VideoCommentModel } from '../../../models/video/video-comment'
5 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
6 import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
7 import { logger, loggerTagsFactory } from '../../../helpers/logger'
8 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
9 import {
10 MActorLight,
11 MCommentOwnerVideo,
12 MVideoAccountLight,
13 MVideoAP,
14 MVideoPlaylistFull,
15 MVideoRedundancyFileVideo,
16 MVideoRedundancyStreamingPlaylistVideo
17 } from '../../../types/models'
18 import { getServerActor } from '@server/models/application/application'
19 import { ContextType } from '@shared/models/activitypub/context'
20
21 const lTags = loggerTagsFactory('ap', 'create')
22
23 async function sendCreateVideo (video: MVideoAP, t: Transaction) {
24 if (!video.hasPrivacyForFederation()) return undefined
25
26 logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid))
27
28 const byActor = video.VideoChannel.Account.Actor
29 const videoObject = video.toActivityPubObject()
30
31 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
32 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
33
34 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
35 }
36
37 async function sendCreateCacheFile (
38 byActor: MActorLight,
39 video: MVideoAccountLight,
40 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
41 ) {
42 logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid))
43
44 return sendVideoRelatedCreateActivity({
45 byActor,
46 video,
47 url: fileRedundancy.url,
48 object: fileRedundancy.toActivityPubObject(),
49 contextType: 'CacheFile'
50 })
51 }
52
53 async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transaction) {
54 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
55
56 logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid))
57
58 const byActor = playlist.OwnerAccount.Actor
59 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
60
61 const object = await playlist.toActivityPubObject(null, t)
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
72 async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transaction) {
73 logger.info('Creating job to send comment %s.', comment.url)
74
75 const isOrigin = comment.Video.isOwned()
76
77 const byActor = comment.Account.Actor
78 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
79 const commentObject = comment.toActivityPubObject(threadParentComments)
80
81 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
82 // Add the actor that commented too
83 actorsInvolvedInComment.push(byActor)
84
85 const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
86 .map(c => c.Account.Actor)
87
88 let audience: ActivityAudience
89 if (isOrigin) {
90 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
91 } else {
92 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
93 }
94
95 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
96
97 // This was a reply, send it to the parent actors
98 const actorsException = [ byActor ]
99 await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException)
100
101 // Broadcast to our followers
102 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
103
104 // Send to actors involved in the comment
105 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
106
107 // Send to origin
108 t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.getSharedInbox()))
109 }
110
111 function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
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 )
123 }
124
125 // ---------------------------------------------------------------------------
126
127 export {
128 sendCreateVideo,
129 buildCreateActivity,
130 sendCreateVideoComment,
131 sendCreateVideoPlaylist,
132 sendCreateCacheFile
133 }
134
135 // ---------------------------------------------------------------------------
136
137 async function sendVideoRelatedCreateActivity (options: {
138 byActor: MActorLight
139 video: MVideoAccountLight
140 url: string
141 object: any
142 transaction?: Transaction
143 contextType?: ContextType
144 }) {
145 const activityBuilder = (audience: ActivityAudience) => {
146 return buildCreateActivity(options.url, options.byActor, options.object, audience)
147 }
148
149 return sendVideoRelatedActivity(activityBuilder, options)
150 }