don't longer misuse communityIdentifier as boolean

This commit is contained in:
einhornimmond 2024-02-20 08:59:59 +01:00
parent cad31c8211
commit f0edd68619
4 changed files with 31 additions and 46 deletions

View File

@ -460,7 +460,7 @@ describe('CommunityResolver', () => {
await mutate({ mutation: login, variables: peterLoginData })
// HomeCommunity is still created in userFactory
homeCom = await getCommunity(admin.communityUuid)
homeCom = await getCommunity({ communityIdentifier: admin.communityUuid })
foreignCom1 = DbCommunity.create()
foreignCom1.foreign = true

View File

@ -44,10 +44,10 @@ export class CommunityResolver {
@Authorized([RIGHTS.COMMUNITIES])
@Query(() => Community)
async community(@Args() { communityIdentifier, foreign }: CommunityArgs): Promise<Community> {
const community = await getCommunity(communityIdentifier, foreign)
async community(@Args() communityArgs: CommunityArgs): Promise<Community> {
const community = await getCommunity(communityArgs)
if (!community) {
throw new LogError('community not found', communityIdentifier, foreign)
throw new LogError('community not found', communityArgs)
}
return new Community(community)
}
@ -55,7 +55,7 @@ export class CommunityResolver {
@Authorized([RIGHTS.COMMUNITY_UPDATE])
@Mutation(() => Community)
async updateHomeCommunity(@Args() { uuid, gmsApiKey }: EditCommunityInput): Promise<Community> {
const homeCom = await getCommunity(uuid)
const homeCom = await getCommunity({ communityIdentifier: uuid })
if (!homeCom) {
throw new LogError('HomeCommunity with uuid not found: ', uuid)
}

View File

@ -452,7 +452,7 @@ export class TransactionResolver {
if (!CONFIG.FEDERATION_XCOM_SENDCOINS_ENABLED) {
throw new LogError('X-Community sendCoins disabled per configuration!')
}
const recipCom = await getCommunity(recipientCommunityIdentifier)
const recipCom = await getCommunity({ communityIdentifier: recipientCommunityIdentifier })
logger.debug('recipient commuity: ', recipCom)
if (recipCom === null) {
throw new LogError(

View File

@ -2,29 +2,26 @@ import { FindOptionsWhere } from '@dbTools/typeorm'
import { Community as DbCommunity } from '@entity/Community'
import { isURL } from 'class-validator'
import { CommunityArgs } from '@/graphql/arg/CommunityArgs'
import { LogError } from '@/server/LogError'
import { isUUID4 } from '@/util/validate'
function getCommunityFindOptions(
communityIdentifier?: string | boolean | null,
foreign?: boolean | null,
): FindOptionsWhere<DbCommunity> {
function getCommunityFindOptions({
communityIdentifier,
foreign,
}: CommunityArgs): FindOptionsWhere<DbCommunity> {
if (communityIdentifier === undefined && foreign === undefined) {
throw new LogError('one of communityIdentifier or foreign must be set')
}
const where: FindOptionsWhere<DbCommunity> = {}
// != null cover !== null and !== undefined
if (communityIdentifier != null) {
if (typeof communityIdentifier === 'boolean') {
if (typeof foreign === 'boolean') {
throw new LogError('communityIdentifier cannot be boolean if foreign is set separately ')
}
where.foreign = communityIdentifier
} else if (isURL(communityIdentifier)) {
if (isURL(communityIdentifier)) {
where.url = communityIdentifier
} else if (isUUID4(communityIdentifier)) {
where.communityUuid = communityIdentifier
} else {
where.name = communityIdentifier
}
}
@ -36,37 +33,23 @@ function getCommunityFindOptions(
/**
* Retrieves a community from the database based on the provided identifier and foreign status.
* If communityIdentifier is a string, it can represent either the URL, UUID, or name of the community.
* If communityIdentifier is a boolean, it represents the foreign status of the community.
* If foreign is provided separately and is a boolean, it represents the foreign status of the community.
* communityIdentifier and foreign cannot both be boolean
* @param communityIdentifier The identifier (URL, UUID, or name) of the community, or a boolean representing the foreign status.
* @param foreign Optional. If provided and is a boolean, it represents the foreign status of the community.
* @param communityIdentifier The identifier (URL, UUID, or name) of the community
* @param foreign Optional. If provided it represents the foreign status of the community.
* @returns A promise that resolves to a DbCommunity object if found, or null if not found.
*/
export async function getCommunity(
communityIdentifier?: string | boolean | null,
foreign?: boolean | null,
): Promise<DbCommunity | null> {
return DbCommunity.findOne({ where: getCommunityFindOptions(communityIdentifier, foreign) })
export async function getCommunity(communityArgs: CommunityArgs): Promise<DbCommunity | null> {
return DbCommunity.findOne({ where: getCommunityFindOptions(communityArgs) })
}
/**
* Retrieves a community from the database based on the provided identifier and foreign status.
* If communityIdentifier is a string, it can represent either the URL, UUID, or name of the community.
* If communityIdentifier is a boolean, it represents the foreign status of the community.
* If foreign is provided separately and is a boolean, it represents the foreign status of the community.
* communityIdentifier and foreign cannot both be boolean
* @param communityIdentifier The identifier (URL, UUID, or name) of the community, or a boolean representing the foreign status.
* @param foreign Optional. If provided and is a boolean, it represents the foreign status of the community.
* @param communityIdentifier The identifier (URL, UUID, or name) of the community
* @param foreign Optional. If provided it represents the foreign status of the community.
* @returns A promise that resolves to a DbCommunity object if found, or throw if not found.
*/
export async function getCommunityOrFail(
communityIdentifier?: string | boolean | null,
foreign?: boolean | null,
): Promise<DbCommunity> {
export async function getCommunityOrFail(communityArgs: CommunityArgs): Promise<DbCommunity> {
return DbCommunity.findOneOrFail({
where: getCommunityFindOptions(communityIdentifier, foreign),
where: getCommunityFindOptions(communityArgs),
})
}
@ -76,7 +59,7 @@ export async function getCommunityOrFail(
* @returns A promise that resolves to true if a non-foreign community exists with the given identifier, otherwise false.
*/
export async function isHomeCommunity(communityIdentifier: string): Promise<boolean> {
return (await getCommunity(communityIdentifier, false)) !== null
return (await getCommunity({ communityIdentifier, foreign: false })) !== null
}
/**
@ -84,7 +67,7 @@ export async function isHomeCommunity(communityIdentifier: string): Promise<bool
* @returns A promise that resolves to the home community, or throw if no home community was found
*/
export async function getHomeCommunity(): Promise<DbCommunity> {
return getCommunityOrFail(false)
return getCommunityOrFail({ foreign: false })
}
/**
@ -93,24 +76,26 @@ export async function getHomeCommunity(): Promise<DbCommunity> {
* @returns A promise that resolves to the URL of the community or throw if no community with this identifier was found
*/
export async function getCommunityUrl(communityIdentifier: string): Promise<string> {
return (await getCommunityOrFail(communityIdentifier)).url
return (await getCommunityOrFail({ communityIdentifier })).url
}
/**
* Checks if a community with the given identifier exists and has an authenticatedAt property set.
* @param communityIdentifier The identifier (URL, UUID, or name) of the community.
* @returns A promise that resolves to true if a community with an authenticatedAt property exists with the given identifier, otherwise false.
* @returns A promise that resolves to true if a community with an authenticatedAt property exists with the given identifier,
* otherwise false.
*/
export async function isCommunityAuthenticated(communityIdentifier: string): Promise<boolean> {
// The !! operator is used to convert the result to a boolean value.
return !!(await getCommunity(communityIdentifier))?.authenticatedAt
return !!(await getCommunity({ communityIdentifier }))?.authenticatedAt
}
/**
* Retrieves the name of the community with the given identifier.
* @param communityIdentifier The identifier (URL, UUID, or name) of the community.
* @returns A promise that resolves to the name of the community. If the community does not exist or has no name, an empty string is returned.
* @returns A promise that resolves to the name of the community. If the community does not exist or has no name,
* an empty string is returned.
*/
export async function getCommunityName(communityIdentifier: string): Promise<string> {
return (await getCommunity(communityIdentifier))?.name ?? ''
return (await getCommunity({ communityIdentifier }))?.name ?? ''
}