Refactor user build and express file middlewares
This commit is contained in:
parent
90370f7cc8
commit
d3d3deaa7a
|
@ -3,8 +3,9 @@ import RateLimit from 'express-rate-limit'
|
||||||
import { tokensRouter } from '@server/controllers/api/users/token'
|
import { tokensRouter } from '@server/controllers/api/users/token'
|
||||||
import { Hooks } from '@server/lib/plugins/hooks'
|
import { Hooks } from '@server/lib/plugins/hooks'
|
||||||
import { OAuthTokenModel } from '@server/models/oauth/oauth-token'
|
import { OAuthTokenModel } from '@server/models/oauth/oauth-token'
|
||||||
import { MUser, MUserAccountDefault } from '@server/types/models'
|
import { MUserAccountDefault } from '@server/types/models'
|
||||||
import { HttpStatusCode, UserAdminFlag, UserCreate, UserCreateResult, UserRegister, UserRight, UserRole, UserUpdate } from '@shared/models'
|
import { pick } from '@shared/core-utils'
|
||||||
|
import { HttpStatusCode, UserCreate, UserCreateResult, UserRegister, UserRight, UserUpdate } from '@shared/models'
|
||||||
import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger'
|
import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger'
|
||||||
import { logger } from '../../../helpers/logger'
|
import { logger } from '../../../helpers/logger'
|
||||||
import { generateRandomString, getFormattedObjects } from '../../../helpers/utils'
|
import { generateRandomString, getFormattedObjects } from '../../../helpers/utils'
|
||||||
|
@ -14,7 +15,7 @@ import { sequelizeTypescript } from '../../../initializers/database'
|
||||||
import { Emailer } from '../../../lib/emailer'
|
import { Emailer } from '../../../lib/emailer'
|
||||||
import { Notifier } from '../../../lib/notifier'
|
import { Notifier } from '../../../lib/notifier'
|
||||||
import { Redis } from '../../../lib/redis'
|
import { Redis } from '../../../lib/redis'
|
||||||
import { createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user'
|
import { buildUser, createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user'
|
||||||
import {
|
import {
|
||||||
asyncMiddleware,
|
asyncMiddleware,
|
||||||
asyncRetryTransactionMiddleware,
|
asyncRetryTransactionMiddleware,
|
||||||
|
@ -175,18 +176,11 @@ export {
|
||||||
async function createUser (req: express.Request, res: express.Response) {
|
async function createUser (req: express.Request, res: express.Response) {
|
||||||
const body: UserCreate = req.body
|
const body: UserCreate = req.body
|
||||||
|
|
||||||
const userToCreate = new UserModel({
|
const userToCreate = buildUser({
|
||||||
username: body.username,
|
...pick(body, [ 'username', 'password', 'email', 'role', 'videoQuota', 'videoQuotaDaily', 'adminFlags' ]),
|
||||||
password: body.password,
|
|
||||||
email: body.email,
|
emailVerified: null
|
||||||
nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
|
})
|
||||||
p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
|
|
||||||
autoPlayVideo: true,
|
|
||||||
role: body.role,
|
|
||||||
videoQuota: body.videoQuota,
|
|
||||||
videoQuotaDaily: body.videoQuotaDaily,
|
|
||||||
adminFlags: body.adminFlags || UserAdminFlag.NONE
|
|
||||||
}) as MUser
|
|
||||||
|
|
||||||
// NB: due to the validator usersAddValidator, password==='' can only be true if we can send the mail.
|
// NB: due to the validator usersAddValidator, password==='' can only be true if we can send the mail.
|
||||||
const createPassword = userToCreate.password === ''
|
const createPassword = userToCreate.password === ''
|
||||||
|
@ -225,16 +219,9 @@ async function createUser (req: express.Request, res: express.Response) {
|
||||||
async function registerUser (req: express.Request, res: express.Response) {
|
async function registerUser (req: express.Request, res: express.Response) {
|
||||||
const body: UserRegister = req.body
|
const body: UserRegister = req.body
|
||||||
|
|
||||||
const userToCreate = new UserModel({
|
const userToCreate = buildUser({
|
||||||
username: body.username,
|
...pick(body, [ 'username', 'password', 'email' ]),
|
||||||
password: body.password,
|
|
||||||
email: body.email,
|
|
||||||
nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
|
|
||||||
p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
|
|
||||||
autoPlayVideo: true,
|
|
||||||
role: UserRole.USER,
|
|
||||||
videoQuota: CONFIG.USER.VIDEO_QUOTA,
|
|
||||||
videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY,
|
|
||||||
emailVerified: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION ? false : null
|
emailVerified: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION ? false : null
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ import { VideoImportModel } from '../../../models/video/video-import'
|
||||||
|
|
||||||
const auditLogger = auditLoggerFactory('users')
|
const auditLogger = auditLoggerFactory('users')
|
||||||
|
|
||||||
const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR })
|
const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
|
||||||
|
|
||||||
const meRouter = express.Router()
|
const meRouter = express.Router()
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ import { resetSequelizeInstance } from '../../helpers/database-utils'
|
||||||
import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
|
import { buildNSFWFilter, createReqFiles, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
|
||||||
import { logger } from '../../helpers/logger'
|
import { logger } from '../../helpers/logger'
|
||||||
import { getFormattedObjects } from '../../helpers/utils'
|
import { getFormattedObjects } from '../../helpers/utils'
|
||||||
import { CONFIG } from '../../initializers/config'
|
|
||||||
import { MIMETYPES } from '../../initializers/constants'
|
import { MIMETYPES } from '../../initializers/constants'
|
||||||
import { sequelizeTypescript } from '../../initializers/database'
|
import { sequelizeTypescript } from '../../initializers/database'
|
||||||
import { sendUpdateActor } from '../../lib/activitypub/send'
|
import { sendUpdateActor } from '../../lib/activitypub/send'
|
||||||
|
@ -51,8 +50,8 @@ import { VideoChannelModel } from '../../models/video/video-channel'
|
||||||
import { VideoPlaylistModel } from '../../models/video/video-playlist'
|
import { VideoPlaylistModel } from '../../models/video/video-playlist'
|
||||||
|
|
||||||
const auditLogger = auditLoggerFactory('channels')
|
const auditLogger = auditLoggerFactory('channels')
|
||||||
const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR })
|
const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
|
||||||
const reqBannerFile = createReqFiles([ 'bannerfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { bannerfile: CONFIG.STORAGE.TMP_DIR })
|
const reqBannerFile = createReqFiles([ 'bannerfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
|
||||||
|
|
||||||
const videoChannelRouter = express.Router()
|
const videoChannelRouter = express.Router()
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ import { AccountModel } from '../../models/account/account'
|
||||||
import { VideoPlaylistModel } from '../../models/video/video-playlist'
|
import { VideoPlaylistModel } from '../../models/video/video-playlist'
|
||||||
import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'
|
import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'
|
||||||
|
|
||||||
const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR })
|
const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
|
||||||
|
|
||||||
const videoPlaylistRouter = express.Router()
|
const videoPlaylistRouter = express.Router()
|
||||||
|
|
||||||
|
|
|
@ -1,26 +1,19 @@
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
|
import { Hooks } from '@server/lib/plugins/hooks'
|
||||||
import { MVideoCaption } from '@server/types/models'
|
import { MVideoCaption } from '@server/types/models'
|
||||||
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
|
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
|
||||||
import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
|
import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils'
|
||||||
import { createReqFiles } from '../../../helpers/express-utils'
|
import { createReqFiles } from '../../../helpers/express-utils'
|
||||||
import { logger } from '../../../helpers/logger'
|
import { logger } from '../../../helpers/logger'
|
||||||
import { getFormattedObjects } from '../../../helpers/utils'
|
import { getFormattedObjects } from '../../../helpers/utils'
|
||||||
import { CONFIG } from '../../../initializers/config'
|
|
||||||
import { MIMETYPES } from '../../../initializers/constants'
|
import { MIMETYPES } from '../../../initializers/constants'
|
||||||
import { sequelizeTypescript } from '../../../initializers/database'
|
import { sequelizeTypescript } from '../../../initializers/database'
|
||||||
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
|
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
|
||||||
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
|
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares'
|
||||||
import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
|
import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators'
|
||||||
import { VideoCaptionModel } from '../../../models/video/video-caption'
|
import { VideoCaptionModel } from '../../../models/video/video-caption'
|
||||||
import { Hooks } from '@server/lib/plugins/hooks'
|
|
||||||
|
|
||||||
const reqVideoCaptionAdd = createReqFiles(
|
const reqVideoCaptionAdd = createReqFiles([ 'captionfile' ], MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
|
||||||
[ 'captionfile' ],
|
|
||||||
MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT,
|
|
||||||
{
|
|
||||||
captionfile: CONFIG.STORAGE.CAPTIONS_DIR
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const videoCaptionsRouter = express.Router()
|
const videoCaptionsRouter = express.Router()
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { createAnyReqFiles } from '@server/helpers/express-utils'
|
import { createAnyReqFiles } from '@server/helpers/express-utils'
|
||||||
import { CONFIG } from '@server/initializers/config'
|
|
||||||
import { MIMETYPES } from '@server/initializers/constants'
|
import { MIMETYPES } from '@server/initializers/constants'
|
||||||
import { JobQueue } from '@server/lib/job-queue'
|
import { JobQueue } from '@server/lib/job-queue'
|
||||||
import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-editor'
|
import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-editor'
|
||||||
|
@ -21,7 +20,6 @@ const editorRouter = express.Router()
|
||||||
|
|
||||||
const tasksFiles = createAnyReqFiles(
|
const tasksFiles = createAnyReqFiles(
|
||||||
MIMETYPES.VIDEO.MIMETYPE_EXT,
|
MIMETYPES.VIDEO.MIMETYPE_EXT,
|
||||||
CONFIG.STORAGE.TMP_DIR,
|
|
||||||
(req: express.Request, file: Express.Multer.File, cb: (err: Error, result?: boolean) => void) => {
|
(req: express.Request, file: Express.Multer.File, cb: (err: Error, result?: boolean) => void) => {
|
||||||
const body = req.body as VideoEditorCreateEdition
|
const body = req.body as VideoEditorCreateEdition
|
||||||
|
|
||||||
|
|
|
@ -61,12 +61,7 @@ const videoImportsRouter = express.Router()
|
||||||
|
|
||||||
const reqVideoFileImport = createReqFiles(
|
const reqVideoFileImport = createReqFiles(
|
||||||
[ 'thumbnailfile', 'previewfile', 'torrentfile' ],
|
[ 'thumbnailfile', 'previewfile', 'torrentfile' ],
|
||||||
Object.assign({}, MIMETYPES.TORRENT.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT),
|
{ ...MIMETYPES.TORRENT.MIMETYPE_EXT, ...MIMETYPES.IMAGE.MIMETYPE_EXT }
|
||||||
{
|
|
||||||
thumbnailfile: CONFIG.STORAGE.TMP_DIR,
|
|
||||||
previewfile: CONFIG.STORAGE.TMP_DIR,
|
|
||||||
torrentfile: CONFIG.STORAGE.TMP_DIR
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
videoImportsRouter.post('/imports',
|
videoImportsRouter.post('/imports',
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { createReqFiles } from '@server/helpers/express-utils'
|
import { createReqFiles } from '@server/helpers/express-utils'
|
||||||
import { CONFIG } from '@server/initializers/config'
|
|
||||||
import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
|
import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
|
||||||
import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
|
import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
|
||||||
import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
|
import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
|
||||||
|
@ -19,14 +18,7 @@ import { VideoModel } from '../../../models/video/video'
|
||||||
|
|
||||||
const liveRouter = express.Router()
|
const liveRouter = express.Router()
|
||||||
|
|
||||||
const reqVideoFileLive = createReqFiles(
|
const reqVideoFileLive = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
|
||||||
[ 'thumbnailfile', 'previewfile' ],
|
|
||||||
MIMETYPES.IMAGE.MIMETYPE_EXT,
|
|
||||||
{
|
|
||||||
thumbnailfile: CONFIG.STORAGE.TMP_DIR,
|
|
||||||
previewfile: CONFIG.STORAGE.TMP_DIR
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
liveRouter.post('/live',
|
liveRouter.post('/live',
|
||||||
authenticate,
|
authenticate,
|
||||||
|
|
|
@ -11,7 +11,6 @@ import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../
|
||||||
import { resetSequelizeInstance } from '../../../helpers/database-utils'
|
import { resetSequelizeInstance } from '../../../helpers/database-utils'
|
||||||
import { createReqFiles } from '../../../helpers/express-utils'
|
import { createReqFiles } from '../../../helpers/express-utils'
|
||||||
import { logger, loggerTagsFactory } from '../../../helpers/logger'
|
import { logger, loggerTagsFactory } from '../../../helpers/logger'
|
||||||
import { CONFIG } from '../../../initializers/config'
|
|
||||||
import { MIMETYPES } from '../../../initializers/constants'
|
import { MIMETYPES } from '../../../initializers/constants'
|
||||||
import { sequelizeTypescript } from '../../../initializers/database'
|
import { sequelizeTypescript } from '../../../initializers/database'
|
||||||
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
|
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
|
||||||
|
@ -26,14 +25,7 @@ const lTags = loggerTagsFactory('api', 'video')
|
||||||
const auditLogger = auditLoggerFactory('videos')
|
const auditLogger = auditLoggerFactory('videos')
|
||||||
const updateRouter = express.Router()
|
const updateRouter = express.Router()
|
||||||
|
|
||||||
const reqVideoFileUpdate = createReqFiles(
|
const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
|
||||||
[ 'thumbnailfile', 'previewfile' ],
|
|
||||||
MIMETYPES.IMAGE.MIMETYPE_EXT,
|
|
||||||
{
|
|
||||||
thumbnailfile: CONFIG.STORAGE.TMP_DIR,
|
|
||||||
previewfile: CONFIG.STORAGE.TMP_DIR
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
updateRouter.put('/:id',
|
updateRouter.put('/:id',
|
||||||
openapiOperationDoc({ operationId: 'putVideo' }),
|
openapiOperationDoc({ operationId: 'putVideo' }),
|
||||||
|
|
|
@ -24,9 +24,8 @@ import { HttpStatusCode, VideoCreate, VideoResolution, VideoState } from '@share
|
||||||
import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
|
import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
|
||||||
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
||||||
import { createReqFiles } from '../../../helpers/express-utils'
|
import { createReqFiles } from '../../../helpers/express-utils'
|
||||||
import { ffprobePromise, buildFileMetadata, getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg'
|
import { buildFileMetadata, ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS } from '../../../helpers/ffmpeg'
|
||||||
import { logger, loggerTagsFactory } from '../../../helpers/logger'
|
import { logger, loggerTagsFactory } from '../../../helpers/logger'
|
||||||
import { CONFIG } from '../../../initializers/config'
|
|
||||||
import { MIMETYPES } from '../../../initializers/constants'
|
import { MIMETYPES } from '../../../initializers/constants'
|
||||||
import { sequelizeTypescript } from '../../../initializers/database'
|
import { sequelizeTypescript } from '../../../initializers/database'
|
||||||
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
|
import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
|
||||||
|
@ -52,21 +51,13 @@ const uploadRouter = express.Router()
|
||||||
|
|
||||||
const reqVideoFileAdd = createReqFiles(
|
const reqVideoFileAdd = createReqFiles(
|
||||||
[ 'videofile', 'thumbnailfile', 'previewfile' ],
|
[ 'videofile', 'thumbnailfile', 'previewfile' ],
|
||||||
Object.assign({}, MIMETYPES.VIDEO.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT),
|
{ ...MIMETYPES.VIDEO.MIMETYPE_EXT, ...MIMETYPES.IMAGE.MIMETYPE_EXT }
|
||||||
{
|
|
||||||
videofile: CONFIG.STORAGE.TMP_DIR,
|
|
||||||
thumbnailfile: CONFIG.STORAGE.TMP_DIR,
|
|
||||||
previewfile: CONFIG.STORAGE.TMP_DIR
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const reqVideoFileAddResumable = createReqFiles(
|
const reqVideoFileAddResumable = createReqFiles(
|
||||||
[ 'thumbnailfile', 'previewfile' ],
|
[ 'thumbnailfile', 'previewfile' ],
|
||||||
MIMETYPES.IMAGE.MIMETYPE_EXT,
|
MIMETYPES.IMAGE.MIMETYPE_EXT,
|
||||||
{
|
getResumableUploadPath()
|
||||||
thumbnailfile: getResumableUploadPath(),
|
|
||||||
previewfile: getResumableUploadPath()
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
uploadRouter.post('/upload',
|
uploadRouter.post('/upload',
|
||||||
|
|
|
@ -68,11 +68,11 @@ function badRequest (_req: express.Request, res: express.Response) {
|
||||||
function createReqFiles (
|
function createReqFiles (
|
||||||
fieldNames: string[],
|
fieldNames: string[],
|
||||||
mimeTypes: { [id: string]: string | string[] },
|
mimeTypes: { [id: string]: string | string[] },
|
||||||
destinations: { [fieldName: string]: string }
|
destination = CONFIG.STORAGE.TMP_DIR
|
||||||
): RequestHandler {
|
): RequestHandler {
|
||||||
const storage = diskStorage({
|
const storage = diskStorage({
|
||||||
destination: (req, file, cb) => {
|
destination: (req, file, cb) => {
|
||||||
cb(null, destinations[file.fieldname])
|
cb(null, destination)
|
||||||
},
|
},
|
||||||
|
|
||||||
filename: (req, file, cb) => {
|
filename: (req, file, cb) => {
|
||||||
|
@ -93,12 +93,11 @@ function createReqFiles (
|
||||||
|
|
||||||
function createAnyReqFiles (
|
function createAnyReqFiles (
|
||||||
mimeTypes: { [id: string]: string | string[] },
|
mimeTypes: { [id: string]: string | string[] },
|
||||||
destinationDirectory: string,
|
|
||||||
fileFilter: (req: express.Request, file: Express.Multer.File, cb: (err: Error, result: boolean) => void) => void
|
fileFilter: (req: express.Request, file: Express.Multer.File, cb: (err: Error, result: boolean) => void) => void
|
||||||
): RequestHandler {
|
): RequestHandler {
|
||||||
const storage = diskStorage({
|
const storage = diskStorage({
|
||||||
destination: (req, file, cb) => {
|
destination: (req, file, cb) => {
|
||||||
cb(null, destinationDirectory)
|
cb(null, CONFIG.STORAGE.TMP_DIR)
|
||||||
},
|
},
|
||||||
|
|
||||||
filename: (req, file, cb) => {
|
filename: (req, file, cb) => {
|
||||||
|
|
|
@ -2,10 +2,9 @@ import { ensureDir, remove } from 'fs-extra'
|
||||||
import passwordGenerator from 'password-generator'
|
import passwordGenerator from 'password-generator'
|
||||||
import { UserRole } from '@shared/models'
|
import { UserRole } from '@shared/models'
|
||||||
import { logger } from '../helpers/logger'
|
import { logger } from '../helpers/logger'
|
||||||
import { createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user'
|
import { buildUser, createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user'
|
||||||
import { ApplicationModel } from '../models/application/application'
|
import { ApplicationModel } from '../models/application/application'
|
||||||
import { OAuthClientModel } from '../models/oauth/oauth-client'
|
import { OAuthClientModel } from '../models/oauth/oauth-client'
|
||||||
import { UserModel } from '../models/user/user'
|
|
||||||
import { applicationExist, clientsExist, usersExist } from './checker-after-init'
|
import { applicationExist, clientsExist, usersExist } from './checker-after-init'
|
||||||
import { CONFIG } from './config'
|
import { CONFIG } from './config'
|
||||||
import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION, RESUMABLE_UPLOAD_DIRECTORY } from './constants'
|
import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION, RESUMABLE_UPLOAD_DIRECTORY } from './constants'
|
||||||
|
@ -137,18 +136,15 @@ async function createOAuthAdminIfNotExist () {
|
||||||
password = passwordGenerator(16, true)
|
password = passwordGenerator(16, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
const userData = {
|
const user = buildUser({
|
||||||
username,
|
username,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
role,
|
role,
|
||||||
verified: true,
|
emailVerified: true,
|
||||||
nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
|
|
||||||
p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
|
|
||||||
videoQuota: -1,
|
videoQuota: -1,
|
||||||
videoQuotaDaily: -1
|
videoQuotaDaily: -1
|
||||||
}
|
})
|
||||||
const user = new UserModel(userData)
|
|
||||||
|
|
||||||
await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword })
|
await createUserAccountAndChannelAndPlaylist({ userToCreate: user, channelNames: undefined, validateUser: validatePassword })
|
||||||
logger.info('Username: ' + username)
|
logger.info('Username: ' + username)
|
||||||
|
|
|
@ -5,14 +5,14 @@ import { ActorModel } from '@server/models/actor/actor'
|
||||||
import { MOAuthClient } from '@server/types/models'
|
import { MOAuthClient } from '@server/types/models'
|
||||||
import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token'
|
import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token'
|
||||||
import { MUser } from '@server/types/models/user/user'
|
import { MUser } from '@server/types/models/user/user'
|
||||||
import { UserAdminFlag } from '@shared/models/users/user-flag.model'
|
import { pick } from '@shared/core-utils'
|
||||||
import { UserRole } from '@shared/models/users/user-role'
|
import { UserRole } from '@shared/models/users/user-role'
|
||||||
import { logger } from '../../helpers/logger'
|
import { logger } from '../../helpers/logger'
|
||||||
import { CONFIG } from '../../initializers/config'
|
import { CONFIG } from '../../initializers/config'
|
||||||
import { OAuthClientModel } from '../../models/oauth/oauth-client'
|
import { OAuthClientModel } from '../../models/oauth/oauth-client'
|
||||||
import { OAuthTokenModel } from '../../models/oauth/oauth-token'
|
import { OAuthTokenModel } from '../../models/oauth/oauth-token'
|
||||||
import { UserModel } from '../../models/user/user'
|
import { UserModel } from '../../models/user/user'
|
||||||
import { createUserAccountAndChannelAndPlaylist } from '../user'
|
import { buildUser, createUserAccountAndChannelAndPlaylist } from '../user'
|
||||||
import { TokensCache } from './tokens-cache'
|
import { TokensCache } from './tokens-cache'
|
||||||
|
|
||||||
type TokenInfo = {
|
type TokenInfo = {
|
||||||
|
@ -229,19 +229,13 @@ async function createUserFromExternal (pluginAuth: string, options: {
|
||||||
const actor = await ActorModel.loadLocalByName(options.username)
|
const actor = await ActorModel.loadLocalByName(options.username)
|
||||||
if (actor) return null
|
if (actor) return null
|
||||||
|
|
||||||
const userToCreate = new UserModel({
|
const userToCreate = buildUser({
|
||||||
username: options.username,
|
...pick(options, [ 'username', 'email', 'role' ]),
|
||||||
|
|
||||||
|
emailVerified: null,
|
||||||
password: null,
|
password: null,
|
||||||
email: options.email,
|
|
||||||
nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
|
|
||||||
p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
|
|
||||||
autoPlayVideo: true,
|
|
||||||
role: options.role,
|
|
||||||
videoQuota: CONFIG.USER.VIDEO_QUOTA,
|
|
||||||
videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY,
|
|
||||||
adminFlags: UserAdminFlag.NONE,
|
|
||||||
pluginAuth
|
pluginAuth
|
||||||
}) as MUser
|
})
|
||||||
|
|
||||||
const { user } = await createUserAccountAndChannelAndPlaylist({
|
const { user } = await createUserAccountAndChannelAndPlaylist({
|
||||||
userToCreate,
|
userToCreate,
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import { Transaction } from 'sequelize/types'
|
import { Transaction } from 'sequelize/types'
|
||||||
|
import { logger } from '@server/helpers/logger'
|
||||||
|
import { CONFIG } from '@server/initializers/config'
|
||||||
import { UserModel } from '@server/models/user/user'
|
import { UserModel } from '@server/models/user/user'
|
||||||
import { MActorDefault } from '@server/types/models/actor'
|
import { MActorDefault } from '@server/types/models/actor'
|
||||||
import { buildUUID } from '@shared/extra-utils'
|
import { buildUUID } from '@shared/extra-utils'
|
||||||
import { ActivityPubActorType } from '../../shared/models/activitypub'
|
import { ActivityPubActorType } from '../../shared/models/activitypub'
|
||||||
import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users'
|
import { UserAdminFlag, UserNotificationSetting, UserNotificationSettingValue, UserRole } from '../../shared/models/users'
|
||||||
import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants'
|
import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants'
|
||||||
import { sequelizeTypescript } from '../initializers/database'
|
import { sequelizeTypescript } from '../initializers/database'
|
||||||
import { AccountModel } from '../models/account/account'
|
import { AccountModel } from '../models/account/account'
|
||||||
|
@ -19,10 +21,56 @@ import { buildActorInstance } from './local-actor'
|
||||||
import { Redis } from './redis'
|
import { Redis } from './redis'
|
||||||
import { createLocalVideoChannel } from './video-channel'
|
import { createLocalVideoChannel } from './video-channel'
|
||||||
import { createWatchLaterPlaylist } from './video-playlist'
|
import { createWatchLaterPlaylist } from './video-playlist'
|
||||||
import { logger } from '@server/helpers/logger'
|
|
||||||
|
|
||||||
type ChannelNames = { name: string, displayName: string }
|
type ChannelNames = { name: string, displayName: string }
|
||||||
|
|
||||||
|
function buildUser (options: {
|
||||||
|
username: string
|
||||||
|
password: string
|
||||||
|
email: string
|
||||||
|
|
||||||
|
role?: UserRole // Default to UserRole.User
|
||||||
|
adminFlags?: UserAdminFlag // Default to UserAdminFlag.NONE
|
||||||
|
|
||||||
|
emailVerified: boolean | null
|
||||||
|
|
||||||
|
videoQuota?: number // Default to CONFIG.USER.VIDEO_QUOTA
|
||||||
|
videoQuotaDaily?: number // Default to CONFIG.USER.VIDEO_QUOTA_DAILY
|
||||||
|
|
||||||
|
pluginAuth?: string
|
||||||
|
}): MUser {
|
||||||
|
const {
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
email,
|
||||||
|
role = UserRole.USER,
|
||||||
|
emailVerified,
|
||||||
|
videoQuota = CONFIG.USER.VIDEO_QUOTA,
|
||||||
|
videoQuotaDaily = CONFIG.USER.VIDEO_QUOTA_DAILY,
|
||||||
|
adminFlags = UserAdminFlag.NONE,
|
||||||
|
pluginAuth
|
||||||
|
} = options
|
||||||
|
|
||||||
|
return new UserModel({
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
email,
|
||||||
|
|
||||||
|
nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
|
||||||
|
p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
|
||||||
|
autoPlayVideo: true,
|
||||||
|
|
||||||
|
role,
|
||||||
|
emailVerified,
|
||||||
|
adminFlags,
|
||||||
|
|
||||||
|
videoQuota: videoQuota,
|
||||||
|
videoQuotaDaily: videoQuotaDaily,
|
||||||
|
|
||||||
|
pluginAuth
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async function createUserAccountAndChannelAndPlaylist (parameters: {
|
async function createUserAccountAndChannelAndPlaylist (parameters: {
|
||||||
userToCreate: MUser
|
userToCreate: MUser
|
||||||
userDisplayName?: string
|
userDisplayName?: string
|
||||||
|
@ -118,7 +166,7 @@ async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) {
|
||||||
const email = isPendingEmail ? user.pendingEmail : user.email
|
const email = isPendingEmail ? user.pendingEmail : user.email
|
||||||
const username = user.username
|
const username = user.username
|
||||||
|
|
||||||
await Emailer.Instance.addVerifyEmailJob(username, email, url)
|
Emailer.Instance.addVerifyEmailJob(username, email, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getOriginalVideoFileTotalFromUser (user: MUserId) {
|
async function getOriginalVideoFileTotalFromUser (user: MUserId) {
|
||||||
|
@ -180,7 +228,8 @@ export {
|
||||||
createUserAccountAndChannelAndPlaylist,
|
createUserAccountAndChannelAndPlaylist,
|
||||||
createLocalAccountWithoutKeys,
|
createLocalAccountWithoutKeys,
|
||||||
sendVerifyUserEmail,
|
sendVerifyUserEmail,
|
||||||
isAbleToUploadVideo
|
isAbleToUploadVideo,
|
||||||
|
buildUser
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue