X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fapplication%2Fapplication.ts;h=5531d134a1b6f67c14b6b9bc2b8483cccb2eb90d;hb=16c016e8b1d5ca46343d3363f9a49e24c5d7c944;hp=f3c0f1052735c2749110a0c7bf2296086763fa84;hpb=3fd3ab2d34d512b160a5e6084d7609be7b4f4452;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/application/application.ts b/server/models/application/application.ts index f3c0f1052..5531d134a 100644 --- a/server/models/application/application.ts +++ b/server/models/application/application.ts @@ -1,10 +1,31 @@ -import { Transaction } from 'sequelize' -import { AllowNull, Column, Default, IsInt, Model, Table } from 'sequelize-typescript' +import * as memoizee from 'memoizee' +import { AllowNull, Column, Default, DefaultScope, HasOne, IsInt, Model, Table } from 'sequelize-typescript' +import { AttributesOnly } from '@shared/core-utils' +import { AccountModel } from '../account/account' +export const getServerActor = memoizee(async function () { + const application = await ApplicationModel.load() + if (!application) throw Error('Could not load Application from database.') + + const actor = application.Account.Actor + actor.Account = application.Account + + return actor +}, { promise: true }) + +@DefaultScope(() => ({ + include: [ + { + model: AccountModel, + required: true + } + ] +})) @Table({ - tableName: 'application' + tableName: 'application', + timestamps: false }) -export class ApplicationModel extends Model { +export class ApplicationModel extends Model>> { @AllowNull(false) @Default(0) @@ -12,24 +33,23 @@ export class ApplicationModel extends Model { @Column migrationVersion: number - static countTotal () { - return ApplicationModel.count() - } + @AllowNull(true) + @Column + latestPeerTubeVersion: string - static loadMigrationVersion () { - const query = { - attributes: [ 'migrationVersion' ] - } + @HasOne(() => AccountModel, { + foreignKey: { + allowNull: true + }, + onDelete: 'cascade' + }) + Account: AccountModel - return ApplicationModel.findOne(query).then(data => data ? data.migrationVersion : null) + static countTotal () { + return ApplicationModel.count() } - static updateMigrationVersion (newVersion: number, transaction: Transaction) { - const options = { - where: {}, - transaction: transaction - } - - return ApplicationModel.update({ migrationVersion: newVersion }, options) + static load () { + return ApplicationModel.findOne() } }