PeerTube/server/core/models/shared/sql.ts

73 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-01-10 04:09:30 -06:00
import { literal, Model, ModelStatic } from 'sequelize'
import { Literal } from 'sequelize/types/utils'
import { forceNumber } from '@peertube/peertube-core-utils'
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
2023-01-10 04:09:30 -06:00
// FIXME: have to specify the result type to not break peertube typings generation
export function buildLocalAccountIdsIn (): Literal {
2023-01-10 04:09:30 -06:00
return literal(
'(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)'
)
}
// FIXME: have to specify the result type to not break peertube typings generation
export function buildLocalActorIdsIn (): Literal {
2023-01-10 04:09:30 -06:00
return literal(
'(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)'
)
}
export function buildBlockedAccountSQL (blockerIds: number[]) {
2023-01-10 04:09:30 -06:00
const blockerIdsString = blockerIds.join(', ')
return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
' UNION ' +
'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
}
export function buildServerIdsFollowedBy (actorId: any) {
2023-01-10 04:09:30 -06:00
const actorIdNumber = forceNumber(actorId)
return '(' +
'SELECT "actor"."serverId" FROM "actorFollow" ' +
'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
')'
}
export function buildSQLAttributes<M extends Model> (options: {
2023-01-10 04:09:30 -06:00
model: ModelStatic<M>
tableName: string
excludeAttributes?: Exclude<keyof AttributesOnly<M>, symbol>[]
aliasPrefix?: string
idBuilder?: string[]
2023-01-10 04:09:30 -06:00
}) {
const { model, tableName, aliasPrefix = '', excludeAttributes, idBuilder } = options
2023-01-10 04:09:30 -06:00
const attributes = Object.keys(model.getAttributes()) as Exclude<keyof AttributesOnly<M>, symbol>[]
const builtAttributes = attributes
2023-01-10 04:09:30 -06:00
.filter(a => {
if (!excludeAttributes) return true
if (excludeAttributes.includes(a)) return false
return true
})
.map(a => {
return `"${tableName}"."${a}" AS "${aliasPrefix}${a}"`
2023-01-10 04:09:30 -06:00
})
if (idBuilder) {
const idSelect = idBuilder.map(a => `"${tableName}"."${a}"`)
.join(` || '-' || `)
builtAttributes.push(`${idSelect} AS "${aliasPrefix}id"`)
}
2023-01-10 04:09:30 -06:00
return builtAttributes
2023-01-10 04:09:30 -06:00
}