]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/application/application.ts
Translated using Weblate (Vietnamese)
[github/Chocobozzz/PeerTube.git] / server / models / application / application.ts
index f3c0f1052735c2749110a0c7bf2296086763fa84..c51ceb2454b3990344c6d7c163400abc83fa3ceb 100644 (file)
@@ -1,10 +1,32 @@
-import { Transaction } from 'sequelize'
-import { AllowNull, Column, Default, IsInt, Model, Table } from 'sequelize-typescript'
+import memoizee from 'memoizee'
+import { AllowNull, Column, Default, DefaultScope, HasOne, IsInt, Model, Table } from 'sequelize-typescript'
+import { getNodeABIVersion } from '@server/helpers/version'
+import { AttributesOnly } from '@shared/typescript-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<ApplicationModel> {
+export class ApplicationModel extends Model<Partial<AttributesOnly<ApplicationModel>>> {
 
   @AllowNull(false)
   @Default(0)
@@ -12,24 +34,46 @@ export class ApplicationModel extends Model<ApplicationModel> {
   @Column
   migrationVersion: number
 
+  @AllowNull(true)
+  @Column
+  latestPeerTubeVersion: string
+
+  @AllowNull(false)
+  @Column
+  nodeVersion: string
+
+  @AllowNull(false)
+  @Column
+  nodeABIVersion: number
+
+  @HasOne(() => AccountModel, {
+    foreignKey: {
+      allowNull: true
+    },
+    onDelete: 'cascade'
+  })
+  Account: AccountModel
+
   static countTotal () {
     return ApplicationModel.count()
   }
 
-  static loadMigrationVersion () {
-    const query = {
-      attributes: [ 'migrationVersion' ]
-    }
+  static load () {
+    return ApplicationModel.findOne()
+  }
+
+  static async nodeABIChanged () {
+    const application = await this.load()
 
-    return ApplicationModel.findOne(query).then(data => data ? data.migrationVersion : null)
+    return application.nodeABIVersion !== getNodeABIVersion()
   }
 
-  static updateMigrationVersion (newVersion: number, transaction: Transaction) {
-    const options = {
-      where: {},
-      transaction: transaction
-    }
+  static async updateNodeVersions () {
+    const application = await this.load()
+
+    application.nodeABIVersion = getNodeABIVersion()
+    application.nodeVersion = process.version
 
-    return ApplicationModel.update({ migrationVersion: newVersion }, options)
+    await application.save()
   }
 }