From d4c49495d0da8eafa05c1211424341dcbb3815c6 Mon Sep 17 00:00:00 2001 From: HoonBaek Date: Wed, 30 Aug 2023 14:54:19 +0900 Subject: [PATCH 1/4] fix: apply metadataKey, metadataValues, and metadataStartsWith to the Channel.queries.channelListQuery --- src/utils/index.ts | 79 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index ea15f0f7c..420269927 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -449,10 +449,35 @@ export const filterMessageListParams = ( }; export const filterChannelListParams = (params: GroupChannelListQuery, channel: GroupChannel, currentUserId: string): boolean => { - if (!params?.includeEmpty && channel?.lastMessage === null) { + const { + includeEmpty, + includeFrozen, + searchFilter, + userIdsFilter, + customTypesFilter = [], + channelUrlsFilter = [], + customTypeStartsWithFilter, + channelNameContainsFilter, + nicknameContainsFilter, + myMemberStateFilter, + hiddenChannelFilter, + unreadChannelFilter, + publicChannelFilter, + superChannelFilter, + metadataKey = '', + metadataValues = ['a', 'b'], + metadataValueStartsWith, + } = params as GroupChannelListQuery & { + // make the properties to not optional + metadataKey: string, + customTypesFilter: Array, + channelUrlsFilter: Array, + metadataValues: Array, + }; + + if (!includeEmpty && channel?.lastMessage === null) { return false; } - const searchFilter = params?.searchFilter; if (searchFilter?.query && (searchFilter?.fields?.length ?? 0) > 0) { const searchQuery = searchFilter.query; const searchFields = searchFilter.fields; @@ -474,7 +499,6 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: } } } - const userIdsFilter = params?.userIdsFilter; if (userIdsFilter?.userIds?.length > 0) { const { includeMode, queryType } = userIdsFilter; const userIds: string[] = userIdsFilter.userIds; @@ -509,32 +533,32 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: } } } - if (params?.includeEmpty === false && channel?.lastMessage === null) { + if (includeEmpty === false && channel?.lastMessage === null) { return false; } - if (params?.includeFrozen === false && channel?.isFrozen === true) { + if (includeFrozen === false && channel?.isFrozen === true) { return false; } - if (params?.customTypesFilter?.length > 0 && !params.customTypesFilter.includes(channel?.customType)) { + if (customTypesFilter?.length > 0 && !customTypesFilter.includes(channel?.customType)) { return false; } - if (params?.customTypeStartsWithFilter && !new RegExp(`^${params.customTypeStartsWithFilter}`).test(channel?.customType)) { + if (customTypeStartsWithFilter && !new RegExp(`^${customTypeStartsWithFilter}`).test(channel?.customType)) { return false; } - if (params?.channelNameContainsFilter && !channel?.name?.toLowerCase().includes(params.channelNameContainsFilter.toLowerCase())) { + if (channelNameContainsFilter && !channel?.name?.toLowerCase().includes(channelNameContainsFilter.toLowerCase())) { return false; } - if (params?.nicknameContainsFilter) { - const lowerCasedSubString = params.nicknameContainsFilter.toLowerCase(); + if (nicknameContainsFilter) { + const lowerCasedSubString = nicknameContainsFilter.toLowerCase(); if (channel?.members?.every((member: Member) => !member.nickname.toLowerCase().includes(lowerCasedSubString))) { return false; } } - if (params?.channelUrlsFilter?.length > 0 && !params.channelUrlsFilter.includes(channel?.url)) { + if (channelUrlsFilter?.length > 0 && !channelUrlsFilter.includes(channel?.url)) { return false; } - if (params?.myMemberStateFilter) { - switch (params.myMemberStateFilter) { + if (myMemberStateFilter) { + switch (myMemberStateFilter) { case 'joined_only': if (channel?.myMemberState !== 'joined') { return false; @@ -557,8 +581,8 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: break; } } - if (params?.hiddenChannelFilter) { - switch (params.hiddenChannelFilter) { + if (hiddenChannelFilter) { + switch (hiddenChannelFilter) { case 'unhidden_only': if (channel?.isHidden || channel?.hiddenState !== 'unhidden') { return false; @@ -581,8 +605,8 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: break; } } - if (params?.unreadChannelFilter) { - switch (params.unreadChannelFilter) { + if (unreadChannelFilter) { + switch (unreadChannelFilter) { case 'unread_message': if (channel?.unreadMessageCount === 0) { return false; @@ -590,8 +614,8 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: break; } } - if (params?.publicChannelFilter) { - switch (params.publicChannelFilter) { + if (publicChannelFilter) { + switch (publicChannelFilter) { case 'public': if (!channel?.isPublic) { return false; @@ -604,8 +628,8 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: break; } } - if (params?.superChannelFilter) { - switch (params.superChannelFilter) { + if (superChannelFilter) { + switch (superChannelFilter) { case 'super': if (!channel?.isSuper) { return false; @@ -618,6 +642,19 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: break; } } + const { cachedMetaData } = channel; + const metadataValue: string = cachedMetaData[metadataKey]; + if (metadataKey && (metadataValues || metadataValueStartsWith)) { + if (!metadataValue) { + return false + } + if (metadataValues?.length > 0 && !metadataValues.every(value => metadataValue.includes(value))) { + return false; + } + if (metadataValueStartsWith && !metadataValue.startsWith(metadataValueStartsWith)) { + return false; + } + } return true; }; From 77527b68777eb9e84ebf0c5e71921970cb259a04 Mon Sep 17 00:00:00 2001 From: HoonBaek Date: Wed, 30 Aug 2023 15:02:12 +0900 Subject: [PATCH 2/4] chore: fix lint error --- src/utils/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 420269927..66cd35604 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -642,11 +642,11 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: break; } } - const { cachedMetaData } = channel; + const { cachedMetaData = {} } = channel; const metadataValue: string = cachedMetaData[metadataKey]; if (metadataKey && (metadataValues || metadataValueStartsWith)) { if (!metadataValue) { - return false + return false; } if (metadataValues?.length > 0 && !metadataValues.every(value => metadataValue.includes(value))) { return false; From 6c6cc6952138423c8a9a566c3a6b4bd0e8c9c3ba Mon Sep 17 00:00:00 2001 From: HoonBaek Date: Wed, 30 Aug 2023 16:36:41 +0900 Subject: [PATCH 3/4] fix: remove useless type assertion --- src/utils/index.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 66cd35604..ccd9875fa 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -467,13 +467,7 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: metadataKey = '', metadataValues = ['a', 'b'], metadataValueStartsWith, - } = params as GroupChannelListQuery & { - // make the properties to not optional - metadataKey: string, - customTypesFilter: Array, - channelUrlsFilter: Array, - metadataValues: Array, - }; + } = params; if (!includeEmpty && channel?.lastMessage === null) { return false; @@ -539,7 +533,7 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: if (includeFrozen === false && channel?.isFrozen === true) { return false; } - if (customTypesFilter?.length > 0 && !customTypesFilter.includes(channel?.customType)) { + if (customTypesFilter && !customTypesFilter.includes(channel?.customType)) { return false; } if (customTypeStartsWithFilter && !new RegExp(`^${customTypeStartsWithFilter}`).test(channel?.customType)) { @@ -554,7 +548,7 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: return false; } } - if (channelUrlsFilter?.length > 0 && !channelUrlsFilter.includes(channel?.url)) { + if (channelUrlsFilter && !channelUrlsFilter.includes(channel?.url)) { return false; } if (myMemberStateFilter) { @@ -643,12 +637,12 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: } } const { cachedMetaData = {} } = channel; - const metadataValue: string = cachedMetaData[metadataKey]; if (metadataKey && (metadataValues || metadataValueStartsWith)) { + const metadataValue: string = cachedMetaData[metadataKey]; if (!metadataValue) { return false; } - if (metadataValues?.length > 0 && !metadataValues.every(value => metadataValue.includes(value))) { + if (metadataValues && !metadataValues.every(value => metadataValue.includes(value))) { return false; } if (metadataValueStartsWith && !metadataValue.startsWith(metadataValueStartsWith)) { From 2a5afd97b7157016e136503941bb8f942d93494b Mon Sep 17 00:00:00 2001 From: HoonBaek Date: Wed, 30 Aug 2023 16:47:24 +0900 Subject: [PATCH 4/4] fix: do not set the default value of filterChannelListParams param --- src/utils/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index ccd9875fa..eee2fdce3 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -454,8 +454,8 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel: includeFrozen, searchFilter, userIdsFilter, - customTypesFilter = [], - channelUrlsFilter = [], + customTypesFilter, + channelUrlsFilter, customTypeStartsWithFilter, channelNameContainsFilter, nicknameContainsFilter,