From 0374b6b5cd685316f924874b2a3068bb345eb0dd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 28 Jan 2020 14:45:17 +0100 Subject: Cleanup server fixme --- server/models/account/account-blocklist.ts | 2 +- server/models/account/user-notification.ts | 2 +- server/models/activitypub/actor.ts | 37 ++++++- server/models/server/server-blocklist.ts | 2 +- server/models/video/video-caption.ts | 2 +- server/models/video/video-channel.ts | 23 ++--- server/models/video/video-playlist.ts | 2 +- server/models/video/video.ts | 153 +++++++++++++---------------- 8 files changed, 116 insertions(+), 107 deletions(-) (limited to 'server/models') diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index 6ebe32556..e2f66d733 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts @@ -80,7 +80,7 @@ export class AccountBlocklistModel extends Model { attributes: [ 'accountId', 'id' ], where: { accountId: { - [Op.in]: accountIds // FIXME: sequelize ANY seems broken + [Op.in]: accountIds }, targetAccountId }, diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index a05f30175..f649b8f95 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts @@ -363,7 +363,7 @@ export class UserNotificationModel extends Model { where: { userId, id: { - [Op.in]: notificationIds // FIXME: sequelize ANY seems broken + [Op.in]: notificationIds } } } diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 007647ced..d651a281a 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -43,7 +43,7 @@ import { MActorFull, MActorHost, MActorServer, - MActorSummaryFormattable, + MActorSummaryFormattable, MActorUrl, MActorWithInboxes } from '../../typings/models' import * as Bluebird from 'bluebird' @@ -276,7 +276,8 @@ export class ActorModel extends Model { }) VideoChannel: VideoChannelModel - private static cache: { [ id: string ]: any } = {} + private static localNameCache: { [ id: string ]: any } = {} + private static localUrlCache: { [ id: string ]: any } = {} static load (id: number): Bluebird { return ActorModel.unscoped().findByPk(id) @@ -345,8 +346,8 @@ export class ActorModel extends Model { static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird { // The server actor never change, so we can easily cache it - if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.cache[preferredUsername]) { - return Bluebird.resolve(ActorModel.cache[preferredUsername]) + if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localNameCache[preferredUsername]) { + return Bluebird.resolve(ActorModel.localNameCache[preferredUsername]) } const query = { @@ -361,7 +362,33 @@ export class ActorModel extends Model { .findOne(query) .then(actor => { if (preferredUsername === SERVER_ACTOR_NAME) { - ActorModel.cache[ preferredUsername ] = actor + ActorModel.localNameCache[ preferredUsername ] = actor + } + + return actor + }) + } + + static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird { + // The server actor never change, so we can easily cache it + if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localUrlCache[preferredUsername]) { + return Bluebird.resolve(ActorModel.localUrlCache[preferredUsername]) + } + + const query = { + attributes: [ 'url' ], + where: { + preferredUsername, + serverId: null + }, + transaction + } + + return ActorModel.unscoped() + .findOne(query) + .then(actor => { + if (preferredUsername === SERVER_ACTOR_NAME) { + ActorModel.localUrlCache[ preferredUsername ] = actor } return actor diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts index b88df4fd5..883ae47ab 100644 --- a/server/models/server/server-blocklist.ts +++ b/server/models/server/server-blocklist.ts @@ -81,7 +81,7 @@ export class ServerBlocklistModel extends Model { attributes: [ 'accountId', 'id' ], where: { accountId: { - [Op.in]: accountIds // FIXME: sequelize ANY seems broken + [Op.in]: accountIds }, targetServerId }, diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index eeb2a4afd..6335d44e4 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts @@ -120,7 +120,7 @@ export class VideoCaptionModel extends Model { language } - return (VideoCaptionModel.upsert(values, { transaction, returning: true }) as any) // FIXME: typings + return VideoCaptionModel.upsert(values, { transaction, returning: true }) .then(([ caption ]) => caption) } diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index e10adcb3a..4e98e7ba3 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -43,18 +43,6 @@ import { MChannelSummaryFormattable } from '../../typings/models/video' -// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation -const indexes: ModelIndexesOptions[] = [ - buildTrigramSearchIndex('video_channel_name_trigram', 'name'), - - { - fields: [ 'accountId' ] - }, - { - fields: [ 'actorId' ] - } -] - export enum ScopeNames { FOR_API = 'FOR_API', WITH_ACCOUNT = 'WITH_ACCOUNT', @@ -176,7 +164,16 @@ export type SummaryOptions = { })) @Table({ tableName: 'videoChannel', - indexes + indexes: [ + buildTrigramSearchIndex('video_channel_name_trigram', 'name'), + + { + fields: [ 'accountId' ] + }, + { + fields: [ 'actorId' ] + } + ] }) export class VideoChannelModel extends Model { diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index bcdda36e5..ba1fc23ea 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -369,7 +369,7 @@ export class VideoPlaylistModel extends Model { model: VideoPlaylistElementModel.unscoped(), where: { videoId: { - [Op.in]: videoIds // FIXME: sequelize ANY seems broken + [Op.in]: videoIds } }, required: true diff --git a/server/models/video/video.ts b/server/models/video/video.ts index eacffe186..20e1f1c4a 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1,18 +1,7 @@ import * as Bluebird from 'bluebird' import { maxBy, minBy } from 'lodash' import { join } from 'path' -import { - CountOptions, - FindOptions, - IncludeOptions, - ModelIndexesOptions, - Op, - QueryTypes, - ScopeOptions, - Sequelize, - Transaction, - WhereOptions -} from 'sequelize' +import { CountOptions, FindOptions, IncludeOptions, Op, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -136,8 +125,7 @@ import { MVideoThumbnailBlacklist, MVideoWithAllFiles, MVideoWithFile, - MVideoWithRights, - MStreamingPlaylistFiles + MVideoWithRights } from '../../typings/models' import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../typings/models/video/video-file' import { MThumbnail } from '../../typings/models/video/thumbnail' @@ -145,74 +133,6 @@ import { VideoFile } from '@shared/models/videos/video-file.model' import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths' import validator from 'validator' -// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation -const indexes: (ModelIndexesOptions & { where?: WhereOptions })[] = [ - buildTrigramSearchIndex('video_name_trigram', 'name'), - - { fields: [ 'createdAt' ] }, - { - fields: [ - { name: 'publishedAt', order: 'DESC' }, - { name: 'id', order: 'ASC' } - ] - }, - { fields: [ 'duration' ] }, - { fields: [ 'views' ] }, - { fields: [ 'channelId' ] }, - { - fields: [ 'originallyPublishedAt' ], - where: { - originallyPublishedAt: { - [Op.ne]: null - } - } - }, - { - fields: [ 'category' ], // We don't care videos with an unknown category - where: { - category: { - [Op.ne]: null - } - } - }, - { - fields: [ 'licence' ], // We don't care videos with an unknown licence - where: { - licence: { - [Op.ne]: null - } - } - }, - { - fields: [ 'language' ], // We don't care videos with an unknown language - where: { - language: { - [Op.ne]: null - } - } - }, - { - fields: [ 'nsfw' ], // Most of the videos are not NSFW - where: { - nsfw: true - } - }, - { - fields: [ 'remote' ], // Only index local videos - where: { - remote: false - } - }, - { - fields: [ 'uuid' ], - unique: true - }, - { - fields: [ 'url' ], - unique: true - } -] - export enum ScopeNames { AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS', FOR_API = 'FOR_API', @@ -292,7 +212,7 @@ export type AvailableForListIDsOptions = { if (options.ids) { query.where = { id: { - [ Op.in ]: options.ids // FIXME: sequelize ANY seems broken + [ Op.in ]: options.ids } } } @@ -760,7 +680,72 @@ export type AvailableForListIDsOptions = { })) @Table({ tableName: 'video', - indexes + indexes: [ + buildTrigramSearchIndex('video_name_trigram', 'name'), + + { fields: [ 'createdAt' ] }, + { + fields: [ + { name: 'publishedAt', order: 'DESC' }, + { name: 'id', order: 'ASC' } + ] + }, + { fields: [ 'duration' ] }, + { fields: [ 'views' ] }, + { fields: [ 'channelId' ] }, + { + fields: [ 'originallyPublishedAt' ], + where: { + originallyPublishedAt: { + [Op.ne]: null + } + } + }, + { + fields: [ 'category' ], // We don't care videos with an unknown category + where: { + category: { + [Op.ne]: null + } + } + }, + { + fields: [ 'licence' ], // We don't care videos with an unknown licence + where: { + licence: { + [Op.ne]: null + } + } + }, + { + fields: [ 'language' ], // We don't care videos with an unknown language + where: { + language: { + [Op.ne]: null + } + } + }, + { + fields: [ 'nsfw' ], // Most of the videos are not NSFW + where: { + nsfw: true + } + }, + { + fields: [ 'remote' ], // Only index local videos + where: { + remote: false + } + }, + { + fields: [ 'uuid' ], + unique: true + }, + { + fields: [ 'url' ], + unique: true + } + ] }) export class VideoModel extends Model { -- cgit v1.2.3