diff --git a/backend/src/graphql/resolver/CommunityResolver.test.ts b/backend/src/graphql/resolver/CommunityResolver.test.ts index 368d88a2d..c9c76e306 100644 --- a/backend/src/graphql/resolver/CommunityResolver.test.ts +++ b/backend/src/graphql/resolver/CommunityResolver.test.ts @@ -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 diff --git a/backend/src/graphql/resolver/CommunityResolver.ts b/backend/src/graphql/resolver/CommunityResolver.ts index 6fac870b1..3162a625e 100644 --- a/backend/src/graphql/resolver/CommunityResolver.ts +++ b/backend/src/graphql/resolver/CommunityResolver.ts @@ -44,10 +44,10 @@ export class CommunityResolver { @Authorized([RIGHTS.COMMUNITIES]) @Query(() => Community) - async community(@Args() { communityIdentifier, foreign }: CommunityArgs): Promise { - const community = await getCommunity(communityIdentifier, foreign) + async community(@Args() communityArgs: CommunityArgs): Promise { + 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 { - const homeCom = await getCommunity(uuid) + const homeCom = await getCommunity({ communityIdentifier: uuid }) if (!homeCom) { throw new LogError('HomeCommunity with uuid not found: ', uuid) } diff --git a/backend/src/graphql/resolver/TransactionResolver.ts b/backend/src/graphql/resolver/TransactionResolver.ts index 00894ecd3..6db74f0f5 100644 --- a/backend/src/graphql/resolver/TransactionResolver.ts +++ b/backend/src/graphql/resolver/TransactionResolver.ts @@ -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( diff --git a/backend/src/graphql/resolver/util/communities.ts b/backend/src/graphql/resolver/util/communities.ts index fffe5d6b9..0d8850181 100644 --- a/backend/src/graphql/resolver/util/communities.ts +++ b/backend/src/graphql/resolver/util/communities.ts @@ -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 { +function getCommunityFindOptions({ + communityIdentifier, + foreign, +}: CommunityArgs): FindOptionsWhere { if (communityIdentifier === undefined && foreign === undefined) { throw new LogError('one of communityIdentifier or foreign must be set') } const where: FindOptionsWhere = {} - // != 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 { - return DbCommunity.findOne({ where: getCommunityFindOptions(communityIdentifier, foreign) }) +export async function getCommunity(communityArgs: CommunityArgs): Promise { + 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 { +export async function getCommunityOrFail(communityArgs: CommunityArgs): Promise { 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 { - return (await getCommunity(communityIdentifier, false)) !== null + return (await getCommunity({ communityIdentifier, foreign: false })) !== null } /** @@ -84,7 +67,7 @@ export async function isHomeCommunity(communityIdentifier: string): Promise { - return getCommunityOrFail(false) + return getCommunityOrFail({ foreign: false }) } /** @@ -93,24 +76,26 @@ export async function getHomeCommunity(): Promise { * @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 { - 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 { // 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 { - return (await getCommunity(communityIdentifier))?.name ?? '' + return (await getCommunity({ communityIdentifier }))?.name ?? '' }