]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
Add mentions to comments
[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 { getServerActor } from '../../../helpers/utils'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoAbuseModel } from '../../../models/video/video-abuse'
8 import { VideoCommentModel } from '../../../models/video/video-comment'
9 import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
10 import {
11 audiencify, broadcastToFollowers, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience,
12 getOriginVideoAudience, getOriginVideoCommentAudience,
13 unicastTo
14 } from './misc'
15
16 async function sendCreateVideo (video: VideoModel, t: Transaction) {
17 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
18
19 const byActor = video.VideoChannel.Account.Actor
20 const videoObject = video.toActivityPubObject()
21
22 const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
23 const data = await createActivityData(video.url, byActor, videoObject, t, audience)
24
25 return broadcastToFollowers(data, byActor, [ byActor ], t)
26 }
27
28 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
29 const url = getVideoAbuseActivityPubUrl(videoAbuse)
30
31 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
32 const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
33
34 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
35 }
36
37 async function sendCreateVideoCommentToOrigin (comment: VideoCommentModel, t: Transaction) {
38 const byActor = comment.Account.Actor
39 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
40 const commentObject = comment.toActivityPubObject(threadParentComments)
41
42 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(comment.Video, t)
43 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInVideo)
44
45 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
46
47 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl, t)
48 }
49
50 async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
51 const byActor = comment.Account.Actor
52 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
53 const commentObject = comment.toActivityPubObject(threadParentComments)
54
55 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(comment.Video, t)
56 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInVideo)
57 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
58
59 const followersException = [ byActor ]
60 return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
61 }
62
63 async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
64 const url = getVideoViewActivityPubUrl(byActor, video)
65 const viewActivityData = createViewActivityData(byActor, video)
66
67 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
68 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
69 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
70
71 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
72 }
73
74 async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
75 const url = getVideoViewActivityPubUrl(byActor, video)
76 const viewActivityData = createViewActivityData(byActor, video)
77
78 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
79 const audience = getObjectFollowersAudience(actorsToForwardView)
80 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
81
82 // Use the server actor to send the view
83 const serverActor = await getServerActor()
84 const followersException = [ byActor ]
85 return broadcastToFollowers(data, serverActor, actorsToForwardView, t, followersException)
86 }
87
88 async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
89 const url = getVideoDislikeActivityPubUrl(byActor, video)
90 const dislikeActivityData = createDislikeActivityData(byActor, video)
91
92 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
93 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
94 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
95
96 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
97 }
98
99 async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
100 const url = getVideoDislikeActivityPubUrl(byActor, video)
101 const dislikeActivityData = createDislikeActivityData(byActor, video)
102
103 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
104 const audience = getObjectFollowersAudience(actorsToForwardView)
105 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
106
107 const followersException = [ byActor ]
108 return broadcastToFollowers(data, byActor, actorsToForwardView, t, followersException)
109 }
110
111 async function createActivityData (
112 url: string,
113 byActor: ActorModel,
114 object: any,
115 t: Transaction,
116 audience?: ActivityAudience
117 ): Promise<ActivityCreate> {
118 if (!audience) {
119 audience = await getAudience(byActor, t)
120 }
121
122 return audiencify({
123 type: 'Create',
124 id: url,
125 actor: byActor.url,
126 object: audiencify(object, audience)
127 }, audience)
128 }
129
130 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
131 return {
132 type: 'Dislike',
133 actor: byActor.url,
134 object: video.url
135 }
136 }
137
138 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
139 return {
140 type: 'View',
141 actor: byActor.url,
142 object: video.url
143 }
144 }
145
146 // ---------------------------------------------------------------------------
147
148 export {
149 sendCreateVideo,
150 sendVideoAbuse,
151 createActivityData,
152 sendCreateViewToOrigin,
153 sendCreateViewToVideoFollowers,
154 sendCreateDislikeToOrigin,
155 sendCreateDislikeToVideoFollowers,
156 createDislikeActivityData,
157 sendCreateVideoCommentToOrigin,
158 sendCreateVideoCommentToVideoFollowers
159 }