From 16c016e8b1d5ca46343d3363f9a49e24c5d7c944 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 12 May 2021 14:09:04 +0200 Subject: Stricter models typing --- server/controllers/api/users/index.ts | 22 ++++++++++++++-------- server/controllers/api/videos/import.ts | 9 ++++++--- server/controllers/api/videos/index.ts | 4 ++-- 3 files changed, 22 insertions(+), 13 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index c655d1648..f384f0f28 100644 --- a/server/controllers/api/users/index.ts +++ b/server/controllers/api/users/index.ts @@ -323,14 +323,20 @@ async function updateUser (req: express.Request, res: express.Response) { const oldUserAuditView = new UserAuditView(userToUpdate.toFormattedJSON()) const roleChanged = body.role !== undefined && body.role !== userToUpdate.role - if (body.password !== undefined) userToUpdate.password = body.password - if (body.email !== undefined) userToUpdate.email = body.email - if (body.emailVerified !== undefined) userToUpdate.emailVerified = body.emailVerified - if (body.videoQuota !== undefined) userToUpdate.videoQuota = body.videoQuota - if (body.videoQuotaDaily !== undefined) userToUpdate.videoQuotaDaily = body.videoQuotaDaily - if (body.role !== undefined) userToUpdate.role = body.role - if (body.adminFlags !== undefined) userToUpdate.adminFlags = body.adminFlags - if (body.pluginAuth !== undefined) userToUpdate.pluginAuth = body.pluginAuth + const keysToUpdate: (keyof UserUpdate)[] = [ + 'password', + 'email', + 'emailVerified', + 'videoQuota', + 'videoQuotaDaily', + 'role', + 'adminFlags', + 'pluginAuth' + ] + + for (const key of keysToUpdate) { + if (body[key] !== undefined) userToUpdate.set(key, body[key]) + } const user = await userToUpdate.save() diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index 4ed58f978..2c225315c 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts @@ -5,6 +5,7 @@ import * as parseTorrent from 'parse-torrent' import { join } from 'path' import { getEnabledResolutions } from '@server/lib/config' import { setVideoTags } from '@server/lib/video' +import { FilteredModelAttributes } from '@server/types' import { MChannelAccountDefault, MThumbnail, @@ -15,7 +16,7 @@ import { MVideoThumbnail, MVideoWithBlacklistLight } from '@server/types/models' -import { MVideoImport, MVideoImportFormattable } from '@server/types/models/video/video-import' +import { MVideoImportFormattable } from '@server/types/models/video/video-import' import { ServerErrorCode, VideoImportCreate, VideoImportState, VideoPrivacy, VideoState } from '../../../../shared' import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' @@ -253,7 +254,9 @@ function buildVideo (channelId: number, body: VideoImportCreate, importData: You privacy: body.privacy || VideoPrivacy.PRIVATE, duration: 0, // duration will be set by the import job channelId: channelId, - originallyPublishedAt: body.originallyPublishedAt || importData.originallyPublishedAt + originallyPublishedAt: body.originallyPublishedAt + ? new Date(body.originallyPublishedAt) + : importData.originallyPublishedAt } const video = new VideoModel(videoData) video.url = getLocalVideoActivityPubUrl(video) @@ -317,7 +320,7 @@ async function insertIntoDB (parameters: { previewModel: MThumbnail videoChannel: MChannelAccountDefault tags: string[] - videoImportAttributes: Partial + videoImportAttributes: FilteredModelAttributes user: MUser }): Promise { const { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes, user } = parameters diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index c32626d30..f5ce15074 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -308,7 +308,7 @@ async function addVideo (options: { if (videoInfo.scheduleUpdate) { await ScheduleVideoUpdateModel.create({ videoId: video.id, - updateAt: videoInfo.scheduleUpdate.updateAt, + updateAt: new Date(videoInfo.scheduleUpdate.updateAt), privacy: videoInfo.scheduleUpdate.privacy || null }, { transaction: t }) } @@ -435,7 +435,7 @@ async function updateVideo (req: express.Request, res: express.Response) { if (videoInfoToUpdate.scheduleUpdate) { await ScheduleVideoUpdateModel.upsert({ videoId: videoInstanceUpdated.id, - updateAt: videoInfoToUpdate.scheduleUpdate.updateAt, + updateAt: new Date(videoInfoToUpdate.scheduleUpdate.updateAt), privacy: videoInfoToUpdate.scheduleUpdate.privacy || null }, { transaction: t }) } else if (videoInfoToUpdate.scheduleUpdate === null) { -- cgit v1.2.3