1 import { literal, Model, ModelStatic } from 'sequelize'
2 import { forceNumber } from '@shared/core-utils'
3 import { AttributesOnly } from '@shared/typescript-utils'
5 function buildLocalAccountIdsIn () {
7 '(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)'
11 function buildLocalActorIdsIn () {
13 '(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)'
17 function buildBlockedAccountSQL (blockerIds: number[]) {
18 const blockerIdsString = blockerIds.join(', ')
20 return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
22 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
23 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
24 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
27 function buildServerIdsFollowedBy (actorId: any) {
28 const actorIdNumber = forceNumber(actorId)
31 'SELECT "actor"."serverId" FROM "actorFollow" ' +
32 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
33 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
37 function buildSQLAttributes<M extends Model> (options: {
41 excludeAttributes?: Exclude<keyof AttributesOnly<M>, symbol>[]
44 const { model, tableName, aliasPrefix, excludeAttributes } = options
46 const attributes = Object.keys(model.getAttributes()) as Exclude<keyof AttributesOnly<M>, symbol>[]
50 if (!excludeAttributes) return true
51 if (excludeAttributes.includes(a)) return false
56 return `"${tableName}"."${a}" AS "${aliasPrefix || ''}${a}"`
60 // ---------------------------------------------------------------------------
64 buildBlockedAccountSQL,
65 buildServerIdsFollowedBy,
66 buildLocalAccountIdsIn,