application.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import memoizee from 'memoizee'
  2. import { AllowNull, Column, Default, DefaultScope, HasOne, IsInt, Model, Table } from 'sequelize-typescript'
  3. import { getNodeABIVersion } from '@server/helpers/version'
  4. import { AttributesOnly } from '@shared/typescript-utils'
  5. import { AccountModel } from '../account/account'
  6. export const getServerActor = memoizee(async function () {
  7. const application = await ApplicationModel.load()
  8. if (!application) throw Error('Could not load Application from database.')
  9. const actor = application.Account.Actor
  10. actor.Account = application.Account
  11. return actor
  12. }, { promise: true })
  13. @DefaultScope(() => ({
  14. include: [
  15. {
  16. model: AccountModel,
  17. required: true
  18. }
  19. ]
  20. }))
  21. @Table({
  22. tableName: 'application',
  23. timestamps: false
  24. })
  25. export class ApplicationModel extends Model<Partial<AttributesOnly<ApplicationModel>>> {
  26. @AllowNull(false)
  27. @Default(0)
  28. @IsInt
  29. @Column
  30. migrationVersion: number
  31. @AllowNull(true)
  32. @Column
  33. latestPeerTubeVersion: string
  34. @AllowNull(false)
  35. @Column
  36. nodeVersion: string
  37. @AllowNull(false)
  38. @Column
  39. nodeABIVersion: number
  40. @HasOne(() => AccountModel, {
  41. foreignKey: {
  42. allowNull: true
  43. },
  44. onDelete: 'cascade'
  45. })
  46. Account: AccountModel
  47. static countTotal () {
  48. return ApplicationModel.count()
  49. }
  50. static load () {
  51. return ApplicationModel.findOne()
  52. }
  53. static async nodeABIChanged () {
  54. const application = await this.load()
  55. return application.nodeABIVersion !== getNodeABIVersion()
  56. }
  57. static async updateNodeVersions () {
  58. const application = await this.load()
  59. application.nodeABIVersion = getNodeABIVersion()
  60. application.nodeVersion = process.version
  61. await application.save()
  62. }
  63. }