aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/actor/sql/shared/instance-list-follows-query-builder.ts
blob: 1d70fbe702e8e5377b548d5aaadcfb9ddf2c21b7 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Sequelize } from 'sequelize'
import { AbstractRunQuery } from '@server/models/shared'
import { getInstanceFollowsSort } from '@server/models/utils'
import { ActorImageType } from '@shared/models'
import { ActorFollowTableAttributes } from './actor-follow-table-attributes'

type BaseOptions = {
  sort: string
  count: number
  start: number
}

export abstract class InstanceListFollowsQueryBuilder <T extends BaseOptions> extends AbstractRunQuery {
  protected readonly tableAttributes = new ActorFollowTableAttributes()

  protected innerQuery: string

  constructor (
    protected readonly sequelize: Sequelize,
    protected readonly options: T
  ) {
    super(sequelize)
  }

  protected abstract getWhere (): string

  protected getJoins () {
    return 'INNER JOIN "actor" "ActorFollower" ON "ActorFollower"."id" = "ActorFollowModel"."actorId" ' +
      'INNER JOIN "actor" "ActorFollowing" ON "ActorFollowing"."id" = "ActorFollowModel"."targetActorId" '
  }

  protected getServerJoin (actorName: string) {
    return `LEFT JOIN "server" "${actorName}->Server" ON "${actorName}"."serverId" = "${actorName}->Server"."id" `
  }

  protected getAvatarsJoin (actorName: string) {
    return `LEFT JOIN "actorImage" "${actorName}->Avatars" ON "${actorName}.id" = "${actorName}->Avatars"."actorId" ` +
      `AND "${actorName}->Avatars"."type" = ${ActorImageType.AVATAR}`
  }

  private buildInnerQuery () {
    this.innerQuery = `${this.getInnerSelect()} ` +
      `FROM "actorFollow" AS "ActorFollowModel" ` +
      `${this.getJoins()} ` +
      `${this.getServerJoin('ActorFollowing')} ` +
      `${this.getServerJoin('ActorFollower')} ` +
      `${this.getWhere()} ` +
      `${this.getOrder()} ` +
      `LIMIT :limit OFFSET :offset `

    this.replacements.limit = this.options.count
    this.replacements.offset = this.options.start
  }

  protected buildListQuery () {
    this.buildInnerQuery()

    this.query = `${this.getSelect()} ` +
      `FROM (${this.innerQuery}) AS "ActorFollowModel" ` +
      `${this.getAvatarsJoin('ActorFollower')} ` +
      `${this.getAvatarsJoin('ActorFollowing')} ` +
      `${this.getOrder()}`
  }

  protected buildCountQuery () {
    this.query = `SELECT COUNT(*) AS "total" ` +
      `FROM "actorFollow" AS "ActorFollowModel" ` +
      `${this.getJoins()} ` +
      `${this.getServerJoin('ActorFollowing')} ` +
      `${this.getServerJoin('ActorFollower')} ` +
      `${this.getWhere()}`
  }

  private getInnerSelect () {
    return this.buildSelect([
      this.tableAttributes.getFollowAttributes(),
      this.tableAttributes.getActorAttributes('ActorFollower'),
      this.tableAttributes.getActorAttributes('ActorFollowing'),
      this.tableAttributes.getServerAttributes('ActorFollower'),
      this.tableAttributes.getServerAttributes('ActorFollowing')
    ])
  }

  private getSelect () {
    return this.buildSelect([
      '"ActorFollowModel".*',
      this.tableAttributes.getAvatarAttributes('ActorFollower'),
      this.tableAttributes.getAvatarAttributes('ActorFollowing')
    ])
  }

  private getOrder () {
    const orders = getInstanceFollowsSort(this.options.sort)

    return 'ORDER BY ' + orders.map(o => `"${o[0]}" ${o[1]}`).join(', ')
  }
}