From b49f22d8f9a52ab75fd38db2d377249eb58fa678 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 8 Dec 2020 14:30:29 +0100 Subject: Upgrade sequelize to v6 --- server/models/account/account-blocklist.ts | 9 ++-- server/models/account/account-video-rate.ts | 53 +++++++++++--------- server/models/account/account.ts | 58 +++++++++++----------- server/models/account/user-notification-setting.ts | 2 +- server/models/account/user-notification.ts | 2 +- server/models/account/user-video-history.ts | 2 +- server/models/account/user.ts | 35 +++++++------ 7 files changed, 83 insertions(+), 78 deletions(-) (limited to 'server/models/account') diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index 577b7dc19..fe9168ab8 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { Op } from 'sequelize' import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/types/models' @@ -41,7 +40,7 @@ enum ScopeNames { } ] }) -export class AccountBlocklistModel extends Model { +export class AccountBlocklistModel extends Model { @CreatedAt createdAt: Date @@ -102,7 +101,7 @@ export class AccountBlocklistModel extends Model { }) } - static loadByAccountAndTarget (accountId: number, targetAccountId: number): Bluebird { + static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise { const query = { where: { accountId, @@ -151,9 +150,9 @@ export class AccountBlocklistModel extends Model { }) } - static listHandlesBlockedBy (accountIds: number[]): Bluebird { + static listHandlesBlockedBy (accountIds: number[]): Promise { const query = { - attributes: [], + attributes: [ 'id' ], where: { accountId: { [Op.in]: accountIds diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index 6955f45ee..d9c529491 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -1,22 +1,21 @@ import { values } from 'lodash' -import { FindOptions, Op, Transaction } from 'sequelize' +import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { VideoRateType } from '../../../shared/models/videos' -import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants' -import { VideoModel } from '../video/video' -import { AccountModel } from './account' -import { ActorModel } from '../activitypub/actor' -import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' -import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' -import { AccountVideoRate } from '../../../shared' -import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' -import * as Bluebird from 'bluebird' import { MAccountVideoRate, MAccountVideoRateAccountUrl, MAccountVideoRateAccountVideo, MAccountVideoRateFormattable } from '@server/types/models/video/video-rate' +import { AccountVideoRate } from '../../../shared' +import { VideoRateType } from '../../../shared/models/videos' +import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' +import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants' +import { ActorModel } from '../activitypub/actor' +import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' +import { VideoModel } from '../video/video' +import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' +import { AccountModel } from './account' /* Account rates per video. @@ -43,7 +42,7 @@ import { } ] }) -export class AccountVideoRateModel extends Model { +export class AccountVideoRateModel extends Model { @AllowNull(false) @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES))) @@ -84,7 +83,7 @@ export class AccountVideoRateModel extends Model { }) Account: AccountModel - static load (accountId: number, videoId: number, transaction?: Transaction): Bluebird { + static load (accountId: number, videoId: number, transaction?: Transaction): Promise { const options: FindOptions = { where: { accountId, @@ -96,7 +95,7 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findOne(options) } - static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Bluebird { + static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Promise { const options: FindOptions = { where: { [Op.or]: [ @@ -152,7 +151,7 @@ export class AccountVideoRateModel extends Model { accountName: string, videoId: number | string, t?: Transaction - ): Bluebird { + ): Promise { const options: FindOptions = { where: { videoId, @@ -240,17 +239,23 @@ export class AccountVideoRateModel extends Model { transaction: t } - const deleted = await AccountVideoRateModel.destroy(query) + await AccountVideoRateModel.destroy(query) - const options = { - transaction: t, - where: { - id: videoId - } - } + const field = type === 'like' + ? 'likes' + : 'dislikes' + + const rawQuery = `UPDATE "video" SET "${field}" = ` + + '(' + + 'SELECT COUNT(id) FROM "accountVideoRate" WHERE "accountVideoRate"."videoId" = "video"."id" AND type = :rateType' + + ') ' + + 'WHERE "video"."id" = :videoId' - if (type === 'like') await VideoModel.increment({ likes: -deleted }, options) - else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options) + return AccountVideoRateModel.sequelize.query(rawQuery, { + transaction: t, + replacements: { videoId, rateType: type }, + type: QueryTypes.UPDATE + }) }) } diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 8c244d432..c72f9c63d 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -1,5 +1,4 @@ -import * as Bluebird from 'bluebird' -import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' +import { FindOptions, Includeable, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -73,28 +72,29 @@ export type SummaryOptions = { required: false } - const query: FindOptions = { - attributes: [ 'id', 'name', 'actorId' ], - include: [ - { - attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], - model: ActorModel.unscoped(), - required: options.actorRequired ?? true, - where: whereActor, - include: [ - serverInclude, + const queryInclude: Includeable[] = [ + { + attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], + model: ActorModel.unscoped(), + required: options.actorRequired ?? true, + where: whereActor, + include: [ + serverInclude, - { - model: AvatarModel.unscoped(), - required: false - } - ] - } - ] + { + model: AvatarModel.unscoped(), + required: false + } + ] + } + ] + + const query: FindOptions = { + attributes: [ 'id', 'name', 'actorId' ] } if (options.withAccountBlockerIds) { - query.include.push({ + queryInclude.push({ attributes: [ 'id' ], model: AccountBlocklistModel.unscoped(), as: 'BlockedAccounts', @@ -120,6 +120,8 @@ export type SummaryOptions = { ] } + query.include = queryInclude + return query } })) @@ -138,7 +140,7 @@ export type SummaryOptions = { } ] }) -export class AccountModel extends Model { +export class AccountModel extends Model { @AllowNull(false) @Column @@ -244,11 +246,11 @@ export class AccountModel extends Model { return undefined } - static load (id: number, transaction?: Transaction): Bluebird { + static load (id: number, transaction?: Transaction): Promise { return AccountModel.findByPk(id, { transaction }) } - static loadByNameWithHost (nameWithHost: string): Bluebird { + static loadByNameWithHost (nameWithHost: string): Promise { const [ accountName, host ] = nameWithHost.split('@') if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName) @@ -256,7 +258,7 @@ export class AccountModel extends Model { return AccountModel.loadByNameAndHost(accountName, host) } - static loadLocalByName (name: string): Bluebird { + static loadLocalByName (name: string): Promise { const fun = () => { const query = { where: { @@ -296,7 +298,7 @@ export class AccountModel extends Model { }) } - static loadByNameAndHost (name: string, host: string): Bluebird { + static loadByNameAndHost (name: string, host: string): Promise { const query = { include: [ { @@ -321,7 +323,7 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static loadByUrl (url: string, transaction?: Transaction): Bluebird { + static loadByUrl (url: string, transaction?: Transaction): Promise { const query = { include: [ { @@ -354,7 +356,7 @@ export class AccountModel extends Model { }) } - static loadAccountIdFromVideo (videoId: number): Bluebird { + static loadAccountIdFromVideo (videoId: number): Promise { const query = { include: [ { @@ -377,7 +379,7 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static listLocalsForSitemap (sort: string): Bluebird { + static listLocalsForSitemap (sort: string): Promise { const query = { attributes: [ ], offset: 0, diff --git a/server/models/account/user-notification-setting.ts b/server/models/account/user-notification-setting.ts index acc192d53..ebab8b6d2 100644 --- a/server/models/account/user-notification-setting.ts +++ b/server/models/account/user-notification-setting.ts @@ -28,7 +28,7 @@ import { UserModel } from './user' } ] }) -export class UserNotificationSettingModel extends Model { +export class UserNotificationSettingModel extends Model { @AllowNull(false) @Default(null) diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index 452574dc8..52b792a5b 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts @@ -254,7 +254,7 @@ function buildAccountInclude (required: boolean, withActor = false) { } ] as (ModelIndexesOptions & { where?: WhereOptions })[] }) -export class UserNotificationModel extends Model { +export class UserNotificationModel extends Model { @AllowNull(false) @Default(null) diff --git a/server/models/account/user-video-history.ts b/server/models/account/user-video-history.ts index 76b469fbf..45171fc60 100644 --- a/server/models/account/user-video-history.ts +++ b/server/models/account/user-video-history.ts @@ -19,7 +19,7 @@ import { MUserAccountId, MUserId } from '@server/types/models' } ] }) -export class UserVideoHistoryModel extends Model { +export class UserVideoHistoryModel extends Model { @CreatedAt createdAt: Date diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 10117099b..8e437c3be 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -1,4 +1,3 @@ -import * as Bluebird from 'bluebird' import { values } from 'lodash' import { col, FindOptions, fn, literal, Op, QueryTypes, where, WhereOptions } from 'sequelize' import { @@ -16,11 +15,11 @@ import { HasOne, Is, IsEmail, + IsUUID, Model, Scopes, Table, - UpdatedAt, - IsUUID + UpdatedAt } from 'sequelize-typescript' import { MMyUserFormattable, @@ -220,7 +219,7 @@ enum ScopeNames { } ] }) -export class UserModel extends Model { +export class UserModel extends Model { @AllowNull(true) @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password', true)) @@ -483,7 +482,7 @@ export class UserModel extends Model { }) } - static listWithRight (right: UserRight): Bluebird { + static listWithRight (right: UserRight): Promise { const roles = Object.keys(USER_ROLE_LABELS) .map(k => parseInt(k, 10) as UserRole) .filter(role => hasUserRight(role, right)) @@ -499,7 +498,7 @@ export class UserModel extends Model { return UserModel.findAll(query) } - static listUserSubscribersOf (actorId: number): Bluebird { + static listUserSubscribersOf (actorId: number): Promise { const query = { include: [ { @@ -538,7 +537,7 @@ export class UserModel extends Model { return UserModel.unscoped().findAll(query) } - static listByUsernames (usernames: string[]): Bluebird { + static listByUsernames (usernames: string[]): Promise { const query = { where: { username: usernames @@ -548,11 +547,11 @@ export class UserModel extends Model { return UserModel.findAll(query) } - static loadById (id: number): Bluebird { + static loadById (id: number): Promise { return UserModel.unscoped().findByPk(id) } - static loadByIdWithChannels (id: number, withStats = false): Bluebird { + static loadByIdWithChannels (id: number, withStats = false): Promise { const scopes = [ ScopeNames.WITH_VIDEOCHANNELS ] @@ -562,7 +561,7 @@ export class UserModel extends Model { return UserModel.scope(scopes).findByPk(id) } - static loadByUsername (username: string): Bluebird { + static loadByUsername (username: string): Promise { const query = { where: { username: { [Op.iLike]: username } @@ -572,7 +571,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadForMeAPI (username: string): Bluebird { + static loadForMeAPI (username: string): Promise { const query = { where: { username: { [Op.iLike]: username } @@ -582,7 +581,7 @@ export class UserModel extends Model { return UserModel.scope(ScopeNames.FOR_ME_API).findOne(query) } - static loadByEmail (email: string): Bluebird { + static loadByEmail (email: string): Promise { const query = { where: { email @@ -592,7 +591,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByUsernameOrEmail (username: string, email?: string): Bluebird { + static loadByUsernameOrEmail (username: string, email?: string): Promise { if (!email) email = username const query = { @@ -608,7 +607,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByVideoId (videoId: number): Bluebird { + static loadByVideoId (videoId: number): Promise { const query = { include: [ { @@ -639,7 +638,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByVideoImportId (videoImportId: number): Bluebird { + static loadByVideoImportId (videoImportId: number): Promise { const query = { include: [ { @@ -656,7 +655,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByChannelActorId (videoChannelActorId: number): Bluebird { + static loadByChannelActorId (videoChannelActorId: number): Promise { const query = { include: [ { @@ -680,7 +679,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByAccountActorId (accountActorId: number): Bluebird { + static loadByAccountActorId (accountActorId: number): Promise { const query = { include: [ { @@ -697,7 +696,7 @@ export class UserModel extends Model { return UserModel.findOne(query) } - static loadByLiveId (liveId: number): Bluebird { + static loadByLiveId (liveId: number): Promise { const query = { include: [ { -- cgit v1.2.3