Init commit
This commit is contained in:
101
server/core/models/shared/sql/actor-helpers.ts
Normal file
101
server/core/models/shared/sql/actor-helpers.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { ActorImageType } from '@peertube/peertube-models'
|
||||
|
||||
export function getActorJoin (options: {
|
||||
base?: string
|
||||
on: string
|
||||
required: boolean
|
||||
type: 'account' | 'channel'
|
||||
includeAvatars?: boolean // default false
|
||||
}) {
|
||||
const { base = '', on, includeAvatars = false, type, required } = options
|
||||
|
||||
const avatarsJoin = includeAvatars
|
||||
? getAvatarsJoin({ base: `${base}Actor`, on: `"${base}Actor"."id"` })
|
||||
: ''
|
||||
|
||||
const join = required
|
||||
? 'INNER JOIN'
|
||||
: 'LEFT JOIN'
|
||||
|
||||
const column = type === 'account'
|
||||
? 'accountId'
|
||||
: 'videoChannelId'
|
||||
|
||||
return ` ${join} "actor" "${base}Actor" ON "${base}Actor"."${column}" = ${on} ` +
|
||||
`LEFT JOIN "server" "${base}Actor->Server" ` +
|
||||
` ON "${base}Actor->Server"."id" = "${base}Actor"."serverId" ` +
|
||||
avatarsJoin
|
||||
}
|
||||
|
||||
export function getChannelJoin (options: {
|
||||
base?: string
|
||||
on: string
|
||||
includeAccount: boolean
|
||||
includeAvatars: boolean
|
||||
includeActors: boolean
|
||||
required: boolean
|
||||
}) {
|
||||
const { base = '', on, includeAccount, includeAvatars, includeActors, required } = options
|
||||
|
||||
const accountJoin = includeAccount
|
||||
? getAccountJoin({
|
||||
base: `${base}VideoChannel->`,
|
||||
on: `"${base}VideoChannel"."accountId"`,
|
||||
includeAvatars,
|
||||
includeActor: includeActors,
|
||||
required
|
||||
})
|
||||
: ''
|
||||
|
||||
const actorJoin = includeActors
|
||||
? getActorJoin({
|
||||
base: `${base}VideoChannel->`,
|
||||
on: `"${base}VideoChannel"."id"`,
|
||||
type: 'channel',
|
||||
includeAvatars,
|
||||
required
|
||||
})
|
||||
: ''
|
||||
|
||||
const join = required
|
||||
? 'INNER JOIN'
|
||||
: 'LEFT JOIN'
|
||||
|
||||
return ` ${join} "videoChannel" "${base}VideoChannel" ON "${base}VideoChannel"."id" = ${on} ` +
|
||||
actorJoin +
|
||||
accountJoin
|
||||
}
|
||||
|
||||
export function getAccountJoin (options: {
|
||||
base?: string
|
||||
on: string
|
||||
includeAvatars: boolean
|
||||
includeActor: boolean
|
||||
required: boolean
|
||||
}) {
|
||||
const { base = '', on, includeAvatars, includeActor, required } = options
|
||||
|
||||
const actorJoin = includeActor
|
||||
? getActorJoin({
|
||||
base: `${base}Account->`,
|
||||
on: `"${base}Account"."id"`,
|
||||
type: 'account',
|
||||
includeAvatars,
|
||||
required
|
||||
})
|
||||
: ''
|
||||
|
||||
return ` LEFT JOIN "account" "${base}Account" ON "${base}Account"."id" = ${on} ` +
|
||||
actorJoin
|
||||
}
|
||||
|
||||
export function getAvatarsJoin (options: {
|
||||
base?: string
|
||||
on: string
|
||||
}) {
|
||||
const { base = '', on } = options
|
||||
|
||||
return ` LEFT JOIN "actorImage" "${base}Avatars" ` +
|
||||
`ON "${base}Avatars"."actorId" = ${on} ` +
|
||||
`AND "${base}Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
}
|
||||
39
server/core/models/shared/sql/common-helpers.ts
Normal file
39
server/core/models/shared/sql/common-helpers.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { forceNumber } from '@peertube/peertube-core-utils'
|
||||
import { FollowState } from '@peertube/peertube-models'
|
||||
import { literal } from 'sequelize'
|
||||
import { Literal } from 'sequelize/types/utils'
|
||||
|
||||
// FIXME: have to specify the result type to not break peertube typings generation
|
||||
export function buildLocalAccountIdsIn (): Literal {
|
||||
return literal(
|
||||
'(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."accountId" = "account"."id" AND "actor"."serverId" IS NULL)'
|
||||
)
|
||||
}
|
||||
|
||||
// FIXME: have to specify the result type to not break peertube typings generation
|
||||
export function buildLocalActorIdsIn (): Literal {
|
||||
return literal(
|
||||
'(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)'
|
||||
)
|
||||
}
|
||||
|
||||
export function buildBlockedAccountSQL (blockerIds: number[]) {
|
||||
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."id" = actor."accountId" ' +
|
||||
'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
|
||||
'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
|
||||
}
|
||||
|
||||
export function buildServerIdsFollowedBy (actorId: any) {
|
||||
const actorIdNumber = forceNumber(actorId)
|
||||
const followState: FollowState = 'accepted'
|
||||
|
||||
return '(' +
|
||||
'SELECT "actor"."serverId" FROM "actorFollow" ' +
|
||||
'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
|
||||
`WHERE "actorFollow"."actorId" = ${actorIdNumber} AND "actorFollow"."state" = '${followState}'` +
|
||||
')'
|
||||
}
|
||||
Reference in New Issue
Block a user