aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/application/application.ts
blob: d4590e0019fae76df4b6bcae06f76dae85c68ba7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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',
  timestamps: false
})
export class ApplicationModel extends Model<Partial<AttributesOnly<ApplicationModel>>> {

  @AllowNull(false)
  @Default(0)
  @IsInt
  @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 load () {
    return ApplicationModel.findOne()
  }

  static async nodeABIChanged () {
    const application = await this.load()

    return application.nodeABIVersion !== getNodeABIVersion()
  }

  static async updateNodeVersions () {
    const application = await this.load()

    application.nodeABIVersion = getNodeABIVersion()
    application.nodeVersion = process.version
  }
}