]>
Commit | Line | Data |
---|---|---|
1 | import { Sequelize } from 'sequelize' | |
2 | import { ModelBuilder } from '@server/models/shared' | |
3 | import { MActorFollowActorsDefault } from '@server/types/models' | |
4 | import { ActivityPubActorType, FollowState } from '@shared/models' | |
5 | import { parseRowCountResult } from '../../shared' | |
6 | import { InstanceListFollowsQueryBuilder } from './shared/instance-list-follows-query-builder' | |
7 | ||
8 | export interface ListFollowingOptions { | |
9 | followerId: number | |
10 | start: number | |
11 | count: number | |
12 | sort: string | |
13 | state?: FollowState | |
14 | actorType?: ActivityPubActorType | |
15 | search?: string | |
16 | } | |
17 | ||
18 | export class InstanceListFollowingQueryBuilder extends InstanceListFollowsQueryBuilder <ListFollowingOptions> { | |
19 | ||
20 | constructor ( | |
21 | protected readonly sequelize: Sequelize, | |
22 | protected readonly options: ListFollowingOptions | |
23 | ) { | |
24 | super(sequelize, options) | |
25 | } | |
26 | ||
27 | async listFollowing () { | |
28 | this.buildListQuery() | |
29 | ||
30 | const results = await this.runQuery({ nest: true }) | |
31 | const modelBuilder = new ModelBuilder<MActorFollowActorsDefault>(this.sequelize) | |
32 | ||
33 | return modelBuilder.createModels(results, 'ActorFollow') | |
34 | } | |
35 | ||
36 | async countFollowing () { | |
37 | this.buildCountQuery() | |
38 | ||
39 | const result = await this.runQuery() | |
40 | ||
41 | return parseRowCountResult(result) | |
42 | } | |
43 | ||
44 | protected getWhere () { | |
45 | let where = 'WHERE "ActorFollowModel"."actorId" = :followerId ' | |
46 | this.replacements.followerId = this.options.followerId | |
47 | ||
48 | if (this.options.state) { | |
49 | where += 'AND "ActorFollowModel"."state" = :state ' | |
50 | this.replacements.state = this.options.state | |
51 | } | |
52 | ||
53 | if (this.options.search) { | |
54 | const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%') | |
55 | ||
56 | where += `AND (` + | |
57 | `"ActorFollowing->Server"."host" ILIKE ${escapedLikeSearch} ` + | |
58 | `OR "ActorFollowing"."preferredUsername" ILIKE ${escapedLikeSearch} ` + | |
59 | `)` | |
60 | } | |
61 | ||
62 | if (this.options.actorType) { | |
63 | where += `AND "ActorFollowing"."type" = :actorType ` | |
64 | this.replacements.actorType = this.options.actorType | |
65 | } | |
66 | ||
67 | return where | |
68 | } | |
69 | } |