From 2cebd797014561ebc0bfee07ee8b5d83820adb66 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 26 Jul 2018 10:45:10 +0200 Subject: [PATCH] Fix last commit --- server/helpers/custom-validators/videos.ts | 8 ++++---- server/helpers/utils.ts | 3 ++- server/initializers/database.ts | 9 +++++---- server/initializers/migrations/0055-video-uuid.ts | 5 +++-- server/lib/activitypub/videos.ts | 6 ++---- server/lib/redis.ts | 4 ++-- server/models/migrations/index.ts | 4 ++++ server/models/utils.ts | 2 +- server/models/video/video-comment.ts | 2 +- 9 files changed, 24 insertions(+), 19 deletions(-) diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 70904af0c..338c96582 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -110,7 +110,7 @@ function isScheduleVideoUpdatePrivacyValid (value: number) { ) } -function isVideoFileInfoHashValid (value: string) { +function isVideoFileInfoHashValid (value: string | null | undefined) { return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) } @@ -158,7 +158,7 @@ async function isVideoExist (id: string, res: Response) { video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id) } - if (video && video !== null) { + if (video === null) { res.status(404) .json({ error: 'Video not found' }) .end() @@ -173,7 +173,7 @@ async function isVideoExist (id: string, res: Response) { async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) { if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) - if (videoChannel && videoChannel !== null) { + if (videoChannel === null) { res.status(400) .json({ error: 'Unknown video video channel on this instance.' }) .end() @@ -186,7 +186,7 @@ async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, } const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id) - if (videoChannel && videoChannel !== null) { + if (videoChannel === null) { res.status(400) .json({ error: 'Unknown video video channel for this account.' }) .end() diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index 7ff1556e3..cfb427570 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts @@ -144,7 +144,8 @@ let serverActor: ActorModel async function getServerActor () { if (serverActor === undefined) { const application = await ApplicationModel.load() - if (!application) throw Error('Could not application.') + if (!application) throw Error('Could not load Application from database.') + serverActor = application.Account.Actor } diff --git a/server/initializers/database.ts b/server/initializers/database.ts index d95e34bce..1a9ce5a61 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts @@ -125,10 +125,11 @@ async function checkPostgresExtensions () { } async function createFunctions () { - const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(varchar) - RETURNS text AS $$ - SELECT unaccent($1) - $$ LANGUAGE sql IMMUTABLE;` + const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(text) + RETURNS text AS +$func$ +SELECT public.unaccent('public.unaccent', $1::text) +$func$ LANGUAGE sql IMMUTABLE;` return sequelizeTypescript.query(query, { raw: true }) } diff --git a/server/initializers/migrations/0055-video-uuid.ts b/server/initializers/migrations/0055-video-uuid.ts index 6db25f193..e0f904080 100644 --- a/server/initializers/migrations/0055-video-uuid.ts +++ b/server/initializers/migrations/0055-video-uuid.ts @@ -1,5 +1,6 @@ import * as Sequelize from 'sequelize' import * as Promise from 'bluebird' +import { Migration } from '../../models/migrations' function up (utils: { transaction: Sequelize.Transaction, @@ -12,7 +13,7 @@ function up (utils: { type: Sequelize.UUID, defaultValue: Sequelize.UUIDV4, allowNull: true - } + } as Migration.UUID return q.addColumn('Videos', 'uuid', dataUUID) .then(() => { @@ -24,7 +25,7 @@ function up (utils: { return utils.sequelize.query(query) }) .then(() => { - dataUUID.defaultValue = null // FIXME:default value cannot be null if string + dataUUID.defaultValue = null return q.changeColumn('Videos', 'uuid', dataUUID) }) diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index 2944cb729..b6f57e0ab 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -38,7 +38,7 @@ async function federateVideoIfNeeded (video: VideoModel, isNewVideo: boolean, tr }) as VideoCaptionModel[] } - if (isNewVideo === true) { + if (isNewVideo) { // Now we'll add the video's meta data to our followers await sendCreateVideo(video, transaction) await shareVideoByServerAndChannel(video, transaction) @@ -153,9 +153,7 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObje if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.href) const parsed = magnetUtil.decode(magnet.href) - if (!parsed || - (parsed.infoHash && - (isVideoFileInfoHashValid(parsed.infoHash) === false))) { + if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) { throw new Error('Cannot parse magnet URI ' + magnet.href) } diff --git a/server/lib/redis.ts b/server/lib/redis.ts index e547537c3..941f7d557 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -6,8 +6,8 @@ import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../in type CachedRoute = { body: string, - contentType: string - statusCode: string + contentType?: string + statusCode?: string } class Redis { diff --git a/server/models/migrations/index.ts b/server/models/migrations/index.ts index c2b31b05e..24badb166 100644 --- a/server/models/migrations/index.ts +++ b/server/models/migrations/index.ts @@ -16,6 +16,10 @@ declare namespace Migration { interface BigInteger extends Sequelize.DefineAttributeColumnOptions { defaultValue: Sequelize.DataTypeBigInt | number | null } + + interface UUID extends Sequelize.DefineAttributeColumnOptions { + defaultValue: Sequelize.DataTypeUUIDv4 | null + } } export { diff --git a/server/models/utils.ts b/server/models/utils.ts index 393f8f036..99e146583 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -76,7 +76,7 @@ export { // --------------------------------------------------------------------------- function searchTrigramNormalizeValue (value: string) { - return Sequelize.fn('lower', Sequelize.fn('unaccent', value)) + return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value)) } function searchTrigramNormalizeCol (col: string) { diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 03122dc03..f84c1880c 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -417,7 +417,7 @@ export class VideoCommentModel extends Model { toActivityPubObject (threadParentComments: VideoCommentModel[]): VideoCommentObject { let inReplyTo: string // New thread, so in AS we reply to the video - if ((this.inReplyToCommentId !== null) || (this.InReplyToVideoComment !== null)) { + if (this.inReplyToCommentId === null) { inReplyTo = this.Video.url } else { inReplyTo = this.InReplyToVideoComment.url