]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize/types' | |
2 | import { v4 as uuidv4 } from 'uuid' | |
3 | import { UserModel } from '@server/models/user/user' | |
4 | import { MActorDefault } from '@server/types/models/actor' | |
5 | import { ActivityPubActorType } from '../../shared/models/activitypub' | |
6 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' | |
7 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' | |
8 | import { sequelizeTypescript } from '../initializers/database' | |
9 | import { AccountModel } from '../models/account/account' | |
10 | import { ActorModel } from '../models/actor/actor' | |
11 | import { UserNotificationSettingModel } from '../models/user/user-notification-setting' | |
12 | import { MAccountDefault, MChannelActor } from '../types/models' | |
13 | import { MUser, MUserDefault, MUserId } from '../types/models/user' | |
14 | import { buildActorInstance, generateAndSaveActorKeys } from './activitypub/actor' | |
15 | import { getLocalAccountActivityPubUrl } from './activitypub/url' | |
16 | import { Emailer } from './emailer' | |
17 | import { LiveManager } from './live-manager' | |
18 | import { Redis } from './redis' | |
19 | import { createLocalVideoChannel } from './video-channel' | |
20 | import { createWatchLaterPlaylist } from './video-playlist' | |
21 | ||
22 | type ChannelNames = { name: string, displayName: string } | |
23 | ||
24 | async function createUserAccountAndChannelAndPlaylist (parameters: { | |
25 | userToCreate: MUser | |
26 | userDisplayName?: string | |
27 | channelNames?: ChannelNames | |
28 | validateUser?: boolean | |
29 | }): Promise<{ user: MUserDefault, account: MAccountDefault, videoChannel: MChannelActor }> { | |
30 | const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters | |
31 | ||
32 | const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => { | |
33 | const userOptions = { | |
34 | transaction: t, | |
35 | validate: validateUser | |
36 | } | |
37 | ||
38 | const userCreated: MUserDefault = await userToCreate.save(userOptions) | |
39 | userCreated.NotificationSetting = await createDefaultUserNotificationSettings(userCreated, t) | |
40 | ||
41 | const accountCreated = await createLocalAccountWithoutKeys({ | |
42 | name: userCreated.username, | |
43 | displayName: userDisplayName, | |
44 | userId: userCreated.id, | |
45 | applicationId: null, | |
46 | t: t | |
47 | }) | |
48 | userCreated.Account = accountCreated | |
49 | ||
50 | const channelAttributes = await buildChannelAttributes(userCreated, channelNames) | |
51 | const videoChannel = await createLocalVideoChannel(channelAttributes, accountCreated, t) | |
52 | ||
53 | const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t) | |
54 | ||
55 | return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist } | |
56 | }) | |
57 | ||
58 | const [ accountActorWithKeys, channelActorWithKeys ] = await Promise.all([ | |
59 | generateAndSaveActorKeys(account.Actor), | |
60 | generateAndSaveActorKeys(videoChannel.Actor) | |
61 | ]) | |
62 | ||
63 | account.Actor = accountActorWithKeys | |
64 | videoChannel.Actor = channelActorWithKeys | |
65 | ||
66 | return { user, account, videoChannel } | |
67 | } | |
68 | ||
69 | async function createLocalAccountWithoutKeys (parameters: { | |
70 | name: string | |
71 | displayName?: string | |
72 | userId: number | null | |
73 | applicationId: number | null | |
74 | t: Transaction | undefined | |
75 | type?: ActivityPubActorType | |
76 | }) { | |
77 | const { name, displayName, userId, applicationId, t, type = 'Person' } = parameters | |
78 | const url = getLocalAccountActivityPubUrl(name) | |
79 | ||
80 | const actorInstance = buildActorInstance(type, url, name) | |
81 | const actorInstanceCreated: MActorDefault = await actorInstance.save({ transaction: t }) | |
82 | ||
83 | const accountInstance = new AccountModel({ | |
84 | name: displayName || name, | |
85 | userId, | |
86 | applicationId, | |
87 | actorId: actorInstanceCreated.id | |
88 | }) | |
89 | ||
90 | const accountInstanceCreated: MAccountDefault = await accountInstance.save({ transaction: t }) | |
91 | accountInstanceCreated.Actor = actorInstanceCreated | |
92 | ||
93 | return accountInstanceCreated | |
94 | } | |
95 | ||
96 | async function createApplicationActor (applicationId: number) { | |
97 | const accountCreated = await createLocalAccountWithoutKeys({ | |
98 | name: SERVER_ACTOR_NAME, | |
99 | userId: null, | |
100 | applicationId: applicationId, | |
101 | t: undefined, | |
102 | type: 'Application' | |
103 | }) | |
104 | ||
105 | accountCreated.Actor = await generateAndSaveActorKeys(accountCreated.Actor) | |
106 | ||
107 | return accountCreated | |
108 | } | |
109 | ||
110 | async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) { | |
111 | const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id) | |
112 | let url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString | |
113 | ||
114 | if (isPendingEmail) url += '&isPendingEmail=true' | |
115 | ||
116 | const email = isPendingEmail ? user.pendingEmail : user.email | |
117 | const username = user.username | |
118 | ||
119 | await Emailer.Instance.addVerifyEmailJob(username, email, url) | |
120 | } | |
121 | ||
122 | async function getOriginalVideoFileTotalFromUser (user: MUserId) { | |
123 | // Don't use sequelize because we need to use a sub query | |
124 | const query = UserModel.generateUserQuotaBaseSQL({ | |
125 | withSelect: true, | |
126 | whereUserId: '$userId' | |
127 | }) | |
128 | ||
129 | const base = await UserModel.getTotalRawQuery(query, user.id) | |
130 | ||
131 | return base + LiveManager.Instance.getLiveQuotaUsedByUser(user.id) | |
132 | } | |
133 | ||
134 | // Returns cumulative size of all video files uploaded in the last 24 hours. | |
135 | async function getOriginalVideoFileTotalDailyFromUser (user: MUserId) { | |
136 | // Don't use sequelize because we need to use a sub query | |
137 | const query = UserModel.generateUserQuotaBaseSQL({ | |
138 | withSelect: true, | |
139 | whereUserId: '$userId', | |
140 | where: '"video"."createdAt" > now() - interval \'24 hours\'' | |
141 | }) | |
142 | ||
143 | const base = await UserModel.getTotalRawQuery(query, user.id) | |
144 | ||
145 | return base + LiveManager.Instance.getLiveQuotaUsedByUser(user.id) | |
146 | } | |
147 | ||
148 | async function isAbleToUploadVideo (userId: number, size: number) { | |
149 | const user = await UserModel.loadById(userId) | |
150 | ||
151 | if (user.videoQuota === -1 && user.videoQuotaDaily === -1) return Promise.resolve(true) | |
152 | ||
153 | const [ totalBytes, totalBytesDaily ] = await Promise.all([ | |
154 | getOriginalVideoFileTotalFromUser(user), | |
155 | getOriginalVideoFileTotalDailyFromUser(user) | |
156 | ]) | |
157 | ||
158 | const uploadedTotal = size + totalBytes | |
159 | const uploadedDaily = size + totalBytesDaily | |
160 | ||
161 | if (user.videoQuotaDaily === -1) return uploadedTotal < user.videoQuota | |
162 | if (user.videoQuota === -1) return uploadedDaily < user.videoQuotaDaily | |
163 | ||
164 | return uploadedTotal < user.videoQuota && uploadedDaily < user.videoQuotaDaily | |
165 | } | |
166 | ||
167 | // --------------------------------------------------------------------------- | |
168 | ||
169 | export { | |
170 | getOriginalVideoFileTotalFromUser, | |
171 | getOriginalVideoFileTotalDailyFromUser, | |
172 | createApplicationActor, | |
173 | createUserAccountAndChannelAndPlaylist, | |
174 | createLocalAccountWithoutKeys, | |
175 | sendVerifyUserEmail, | |
176 | isAbleToUploadVideo | |
177 | } | |
178 | ||
179 | // --------------------------------------------------------------------------- | |
180 | ||
181 | function createDefaultUserNotificationSettings (user: MUserId, t: Transaction | undefined) { | |
182 | const values: UserNotificationSetting & { userId: number } = { | |
183 | userId: user.id, | |
184 | newVideoFromSubscription: UserNotificationSettingValue.WEB, | |
185 | newCommentOnMyVideo: UserNotificationSettingValue.WEB, | |
186 | myVideoImportFinished: UserNotificationSettingValue.WEB, | |
187 | myVideoPublished: UserNotificationSettingValue.WEB, | |
188 | abuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
189 | videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
190 | blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
191 | newUserRegistration: UserNotificationSettingValue.WEB, | |
192 | commentMention: UserNotificationSettingValue.WEB, | |
193 | newFollow: UserNotificationSettingValue.WEB, | |
194 | newInstanceFollower: UserNotificationSettingValue.WEB, | |
195 | abuseNewMessage: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
196 | abuseStateChange: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
197 | autoInstanceFollowing: UserNotificationSettingValue.WEB, | |
198 | newPeerTubeVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | |
199 | newPluginVersion: UserNotificationSettingValue.WEB | |
200 | } | |
201 | ||
202 | return UserNotificationSettingModel.create(values, { transaction: t }) | |
203 | } | |
204 | ||
205 | async function buildChannelAttributes (user: MUser, channelNames?: ChannelNames) { | |
206 | if (channelNames) return channelNames | |
207 | ||
208 | let channelName = user.username + '_channel' | |
209 | ||
210 | // Conflict, generate uuid instead | |
211 | const actor = await ActorModel.loadLocalByName(channelName) | |
212 | if (actor) channelName = uuidv4() | |
213 | ||
214 | const videoChannelDisplayName = `Main ${user.username} channel` | |
215 | ||
216 | return { | |
217 | name: channelName, | |
218 | displayName: videoChannelDisplayName | |
219 | } | |
220 | } |