aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/actor-custom-page.ts
blob: 8930231811771ea8ba2f526e338af26b0359e770 (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
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { CustomPage } from '@shared/models'
import { ActorModel } from '../actor/actor'
import { getServerActor } from '../application/application'

@Table({
  tableName: 'actorCustomPage',
  indexes: [
    {
      fields: [ 'actorId', 'type' ],
      unique: true
    }
  ]
})
export class ActorCustomPageModel extends Model {

  @AllowNull(true)
  @Column(DataType.TEXT)
  content: string

  @AllowNull(false)
  @Column
  type: 'homepage'

  @CreatedAt
  createdAt: Date

  @UpdatedAt
  updatedAt: Date

  @ForeignKey(() => ActorModel)
  @Column
  actorId: number

  @BelongsTo(() => ActorModel, {
    foreignKey: {
      name: 'actorId',
      allowNull: false
    },
    onDelete: 'cascade'
  })
  Actor: ActorModel

  static async updateInstanceHomepage (content: string) {
    const serverActor = await getServerActor()

    return ActorCustomPageModel.upsert({
      content,
      actorId: serverActor.id,
      type: 'homepage'
    })
  }

  static async loadInstanceHomepage () {
    const serverActor = await getServerActor()

    return ActorCustomPageModel.findOne({
      where: {
        actorId: serverActor.id
      }
    })
  }

  toFormattedJSON (): CustomPage {
    return {
      content: this.content
    }
  }
}