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