]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-create.ts
Misc cleanup
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-create.ts
CommitLineData
54141398
C
1import { Transaction } from 'sequelize'
2import { ActivityCreate } from '../../../../shared/models/activitypub/activity'
0032ebe9 3import { getServerAccount } from '../../../helpers/utils'
54141398
C
4import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models'
5import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface'
0032ebe9
C
6import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
7import {
8 broadcastToFollowers,
9 getAccountsToForwardVideoAction,
10 getAudience,
11 getOriginVideoAudience,
12 getVideoFollowersAudience,
13 unicastTo
14} from './misc'
54141398
C
15
16async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
17 const byAccount = videoChannel.Account
18
19 const videoChannelObject = videoChannel.toActivityPubObject()
20 const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject)
21
22 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
23}
24
25async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
26 const url = getVideoAbuseActivityPubUrl(videoAbuse)
40ff5707
C
27
28 const audience = { to: [ video.VideoChannel.Account.url ], cc: [] }
29 const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject(), audience)
30
31 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
32}
33
34async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
35 const url = getVideoViewActivityPubUrl(byAccount, video)
36 const viewActivity = createViewActivityData(byAccount, video)
37
0032ebe9 38 const audience = getOriginVideoAudience(video)
40ff5707 39 const data = await createActivityData(url, byAccount, viewActivity, audience)
54141398
C
40
41 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
42}
43
40ff5707
C
44async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
45 const url = getVideoViewActivityPubUrl(byAccount, video)
46 const viewActivity = createViewActivityData(byAccount, video)
47
0032ebe9 48 const audience = getVideoFollowersAudience(video)
40ff5707
C
49 const data = await createActivityData(url, byAccount, viewActivity, audience)
50
51 const serverAccount = await getServerAccount()
0032ebe9
C
52 const accountsToForwardView = await getAccountsToForwardVideoAction(byAccount, video)
53
54 const followersException = [ byAccount ]
55 return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
56}
57
58async function sendCreateDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
59 const url = getVideoDislikeActivityPubUrl(byAccount, video)
60 const dislikeActivity = createDislikeActivityData(byAccount, video)
61
62 const audience = getOriginVideoAudience(video)
63 const data = await createActivityData(url, byAccount, dislikeActivity, audience)
64
65 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
66}
40ff5707 67
0032ebe9
C
68async function sendCreateDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
69 const url = getVideoDislikeActivityPubUrl(byAccount, video)
70 const dislikeActivity = createDislikeActivityData(byAccount, video)
71
72 const audience = getVideoFollowersAudience(video)
73 const data = await createActivityData(url, byAccount, dislikeActivity, audience)
74
75 const accountsToForwardView = await getAccountsToForwardVideoAction(byAccount, video)
76 const serverAccount = await getServerAccount()
40ff5707
C
77
78 const followersException = [ byAccount ]
79 return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
80}
81
82async function createActivityData (url: string, byAccount: AccountInstance, object: any, audience?: { to: string[], cc: string[] }) {
83 if (!audience) {
84 audience = await getAudience(byAccount)
85 }
c986175d 86
54141398
C
87 const activity: ActivityCreate = {
88 type: 'Create',
89 id: url,
90 actor: byAccount.url,
40ff5707
C
91 to: audience.to,
92 cc: audience.cc,
54141398
C
93 object
94 }
95
96 return activity
97}
98
0032ebe9
C
99function createDislikeActivityData (byAccount: AccountInstance, video: VideoInstance) {
100 const obj = {
101 type: 'Dislike',
102 actor: byAccount.url,
103 object: video.url
104 }
105
106 return obj
107}
108
54141398
C
109// ---------------------------------------------------------------------------
110
111export {
112 sendCreateVideoChannel,
113 sendVideoAbuse,
40ff5707
C
114 createActivityData,
115 sendCreateViewToOrigin,
0032ebe9
C
116 sendCreateViewToVideoFollowers,
117 sendCreateDislikeToOrigin,
118 sendCreateDislikeToVideoFollowers,
119 createDislikeActivityData
40ff5707
C
120}
121
122// ---------------------------------------------------------------------------
123
124function createViewActivityData (byAccount: AccountInstance, video: VideoInstance) {
125 const obj = {
126 type: 'View',
127 actor: byAccount.url,
128 object: video.url
129 }
130
131 return obj
54141398 132}