]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/application/application.ts
Fix comments deleted display
[github/Chocobozzz/PeerTube.git] / server / models / application / application.ts
index 9fc07e8505c311c33667e53e7d0ab218ecf8fa3d..3bba2c70e3600476dc7d22693e3f0f9e747eb20b 100644 (file)
@@ -1,7 +1,28 @@
-import { AllowNull, Column, Default, IsInt, Model, Table } from 'sequelize-typescript'
+import { AllowNull, Column, Default, DefaultScope, HasOne, IsInt, Model, Table } from 'sequelize-typescript'
+import { AccountModel } from '../account/account'
+import * as memoizee from 'memoizee'
 
+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> {
 
@@ -11,7 +32,19 @@ export class ApplicationModel extends Model<ApplicationModel> {
   @Column
   migrationVersion: number
 
+  @HasOne(() => AccountModel, {
+    foreignKey: {
+      allowNull: true
+    },
+    onDelete: 'cascade'
+  })
+  Account: AccountModel
+
   static countTotal () {
     return ApplicationModel.count()
   }
+
+  static load () {
+    return ApplicationModel.findOne()
+  }
 }