2
1

actor-custom-page.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
  2. import { CustomPage } from '@shared/models'
  3. import { ActorModel } from '../actor/actor'
  4. import { getServerActor } from '../application/application'
  5. @Table({
  6. tableName: 'actorCustomPage',
  7. indexes: [
  8. {
  9. fields: [ 'actorId', 'type' ],
  10. unique: true
  11. }
  12. ]
  13. })
  14. export class ActorCustomPageModel extends Model {
  15. @AllowNull(true)
  16. @Column(DataType.TEXT)
  17. content: string
  18. @AllowNull(false)
  19. @Column
  20. type: 'homepage'
  21. @CreatedAt
  22. createdAt: Date
  23. @UpdatedAt
  24. updatedAt: Date
  25. @ForeignKey(() => ActorModel)
  26. @Column
  27. actorId: number
  28. @BelongsTo(() => ActorModel, {
  29. foreignKey: {
  30. name: 'actorId',
  31. allowNull: false
  32. },
  33. onDelete: 'cascade'
  34. })
  35. Actor: ActorModel
  36. static async updateInstanceHomepage (content: string) {
  37. const serverActor = await getServerActor()
  38. return ActorCustomPageModel.upsert({
  39. content,
  40. actorId: serverActor.id,
  41. type: 'homepage'
  42. })
  43. }
  44. static async loadInstanceHomepage () {
  45. const serverActor = await getServerActor()
  46. return ActorCustomPageModel.findOne({
  47. where: {
  48. actorId: serverActor.id
  49. }
  50. })
  51. }
  52. toFormattedJSON (): CustomPage {
  53. return {
  54. content: this.content
  55. }
  56. }
  57. }