X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Flib%2Fuser.ts;h=0d292ac90abcc0bf642db88c79e8b60d9df60d24;hb=d0800f7661f13fabe7bb6f4aa0ea50764f106405;hp=d3338f329467b4b6b184715256dac7eb4344cbc6;hpb=fb7194043d0486ce0a6a40b2ffbdf32878c33a6f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/user.ts b/server/lib/user.ts index d3338f329..0d292ac90 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts @@ -1,25 +1,25 @@ import { Transaction } from 'sequelize/types' -import { v4 as uuidv4 } from 'uuid' -import { UserModel } from '@server/models/account/user' +import { UserModel } from '@server/models/user/user' +import { MActorDefault } from '@server/types/models/actor' +import { buildUUID } from '@shared/extra-utils' import { ActivityPubActorType } from '../../shared/models/activitypub' import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' import { sequelizeTypescript } from '../initializers/database' import { AccountModel } from '../models/account/account' -import { UserNotificationSettingModel } from '../models/account/user-notification-setting' -import { ActorModel } from '../models/activitypub/actor' -import { MAccountDefault, MActorDefault, MChannelActor } from '../types/models' +import { ActorModel } from '../models/actor/actor' +import { UserNotificationSettingModel } from '../models/user/user-notification-setting' +import { MAccountDefault, MChannelActor } from '../types/models' import { MUser, MUserDefault, MUserId } from '../types/models/user' -import { buildActorInstance, setAsyncActorKeys } from './activitypub/actor' -import { getAccountActivityPubUrl } from './activitypub/url' +import { generateAndSaveActorKeys } from './activitypub/actors' +import { getLocalAccountActivityPubUrl } from './activitypub/url' import { Emailer } from './emailer' -import { LiveManager } from './live-manager' +import { LiveQuotaStore } from './live/live-quota-store' +import { buildActorInstance } from './local-actor' import { Redis } from './redis' import { createLocalVideoChannel } from './video-channel' import { createWatchLaterPlaylist } from './video-playlist' -import memoizee = require('memoizee') - type ChannelNames = { name: string, displayName: string } async function createUserAccountAndChannelAndPlaylist (parameters: { @@ -44,11 +44,11 @@ async function createUserAccountAndChannelAndPlaylist (parameters: { displayName: userDisplayName, userId: userCreated.id, applicationId: null, - t: t + t }) userCreated.Account = accountCreated - const channelAttributes = await buildChannelAttributes(userCreated, channelNames) + const channelAttributes = await buildChannelAttributes(userCreated, t, channelNames) const videoChannel = await createLocalVideoChannel(channelAttributes, accountCreated, t) const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t) @@ -57,8 +57,8 @@ async function createUserAccountAndChannelAndPlaylist (parameters: { }) const [ accountActorWithKeys, channelActorWithKeys ] = await Promise.all([ - setAsyncActorKeys(account.Actor), - setAsyncActorKeys(videoChannel.Actor) + generateAndSaveActorKeys(account.Actor), + generateAndSaveActorKeys(videoChannel.Actor) ]) account.Actor = accountActorWithKeys @@ -76,7 +76,7 @@ async function createLocalAccountWithoutKeys (parameters: { type?: ActivityPubActorType }) { const { name, displayName, userId, applicationId, t, type = 'Person' } = parameters - const url = getAccountActivityPubUrl(name) + const url = getLocalAccountActivityPubUrl(name) const actorInstance = buildActorInstance(type, url, name) const actorInstanceCreated: MActorDefault = await actorInstance.save({ transaction: t }) @@ -103,7 +103,7 @@ async function createApplicationActor (applicationId: number) { type: 'Application' }) - accountCreated.Actor = await setAsyncActorKeys(accountCreated.Actor) + accountCreated.Actor = await generateAndSaveActorKeys(accountCreated.Actor) return accountCreated } @@ -129,7 +129,7 @@ async function getOriginalVideoFileTotalFromUser (user: MUserId) { const base = await UserModel.getTotalRawQuery(query, user.id) - return base + LiveManager.Instance.getLiveQuotaUsedByUser(user.id) + return base + LiveQuotaStore.Instance.getLiveQuotaOf(user.id) } // Returns cumulative size of all video files uploaded in the last 24 hours. @@ -143,21 +143,21 @@ async function getOriginalVideoFileTotalDailyFromUser (user: MUserId) { const base = await UserModel.getTotalRawQuery(query, user.id) - return base + LiveManager.Instance.getLiveQuotaUsedByUser(user.id) + return base + LiveQuotaStore.Instance.getLiveQuotaOf(user.id) } -async function isAbleToUploadVideo (userId: number, size: number) { +async function isAbleToUploadVideo (userId: number, newVideoSize: number) { const user = await UserModel.loadById(userId) if (user.videoQuota === -1 && user.videoQuotaDaily === -1) return Promise.resolve(true) const [ totalBytes, totalBytesDaily ] = await Promise.all([ - getOriginalVideoFileTotalFromUser(user.id), - getOriginalVideoFileTotalDailyFromUser(user.id) + getOriginalVideoFileTotalFromUser(user), + getOriginalVideoFileTotalDailyFromUser(user) ]) - const uploadedTotal = size + totalBytes - const uploadedDaily = size + totalBytesDaily + const uploadedTotal = newVideoSize + totalBytes + const uploadedDaily = newVideoSize + totalBytesDaily if (user.videoQuotaDaily === -1) return uploadedTotal < user.videoQuota if (user.videoQuota === -1) return uploadedDaily < user.videoQuotaDaily @@ -195,20 +195,22 @@ function createDefaultUserNotificationSettings (user: MUserId, t: Transaction | newInstanceFollower: UserNotificationSettingValue.WEB, abuseNewMessage: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, abuseStateChange: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - autoInstanceFollowing: UserNotificationSettingValue.WEB + autoInstanceFollowing: UserNotificationSettingValue.WEB, + newPeerTubeVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newPluginVersion: UserNotificationSettingValue.WEB } return UserNotificationSettingModel.create(values, { transaction: t }) } -async function buildChannelAttributes (user: MUser, channelNames?: ChannelNames) { +async function buildChannelAttributes (user: MUser, transaction?: Transaction, channelNames?: ChannelNames) { if (channelNames) return channelNames let channelName = user.username + '_channel' // Conflict, generate uuid instead - const actor = await ActorModel.loadLocalByName(channelName) - if (actor) channelName = uuidv4() + const actor = await ActorModel.loadLocalByName(channelName, transaction) + if (actor) channelName = buildUUID() const videoChannelDisplayName = `Main ${user.username} channel`