From 3acceeefb21bba18c94a87bf365348dbec7dd85d Mon Sep 17 00:00:00 2001 From: Vishal Date: Mon, 16 Sep 2024 13:58:51 +0530 Subject: [PATCH] quote names when using recent mention search (#28119) --- webapp/channels/src/actions/views/rhs.test.ts | 42 ++++++++++++++++--- webapp/channels/src/actions/views/rhs.ts | 16 +++---- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/webapp/channels/src/actions/views/rhs.test.ts b/webapp/channels/src/actions/views/rhs.test.ts index 762411286f..11f23f375e 100644 --- a/webapp/channels/src/actions/views/rhs.test.ts +++ b/webapp/channels/src/actions/views/rhs.test.ts @@ -53,6 +53,8 @@ import type {ViewsState} from 'types/store/views'; const currentChannelId = '123'; const currentTeamId = '321'; const currentUserId = 'user123'; +const currentUsername = 'user-name'; +const currentUserFirstName = 'first-name'; const pluggableId = 'pluggableId'; const previousSelectedPost = { id: 'post123', @@ -102,6 +104,8 @@ describe('rhs view actions', () => { currentUserId, profiles: { user123: { + username: currentUsername, + first_name: currentUserFirstName, timezone: { useAutomaticTimezone: true, automaticTimezone: '', @@ -214,22 +218,50 @@ describe('rhs view actions', () => { }); describe('performSearch', () => { - const terms = '@here test search'; + // timezone offset in seconds + const timeZoneOffset = getBrowserUtcOffset() * 60; test('it dispatches searchPosts correctly', () => { + const terms = '@here test search'; store.dispatch(performSearch(terms, false)); - // timezone offset in seconds - const timeZoneOffset = getBrowserUtcOffset() * 60; - const compareStore = mockStore(initialState); compareStore.dispatch(SearchActions.searchPostsWithParams(currentTeamId, {include_deleted_channels: false, terms, is_or_search: false, time_zone_offset: timeZoneOffset, page: 0, per_page: 20})); compareStore.dispatch(SearchActions.searchFilesWithParams(currentTeamId, {include_deleted_channels: false, terms, is_or_search: false, time_zone_offset: timeZoneOffset, page: 0, per_page: 20})); expect(store.getActions()).toEqual(compareStore.getActions()); + }); + test('it dispatches searchFiles correctly', () => { + store = mockStore({ + ...initialState, + views: { + ...initialState.views, + rhs: { + ...initialState.views.rhs, + filesSearchExtFilter: ['txt', 'jpeg'], + }, + } as ViewsState, + }); + + const terms = '@here test search'; + store.dispatch(performSearch(terms, false)); + + const filesExtTerms = '@here test search ext:txt ext:jpeg'; + const compareStore = mockStore(initialState); + compareStore.dispatch(SearchActions.searchPostsWithParams(currentTeamId, {include_deleted_channels: false, terms, is_or_search: false, time_zone_offset: timeZoneOffset, page: 0, per_page: 20})); + compareStore.dispatch(SearchActions.searchFilesWithParams(currentTeamId, {include_deleted_channels: false, terms: filesExtTerms, is_or_search: false, time_zone_offset: timeZoneOffset, page: 0, per_page: 20})); + + expect(store.getActions()).toEqual(compareStore.getActions()); + }); + + test('it dispatches searchPosts correctly for Recent Mentions', () => { + const terms = `@here test search ${currentUsername} @${currentUsername} ${currentUserFirstName}`; store.dispatch(performSearch(terms, true)); - compareStore.dispatch(SearchActions.searchPostsWithParams('', {include_deleted_channels: false, terms, is_or_search: true, time_zone_offset: timeZoneOffset, page: 0, per_page: 20})); + + const mentionsQuotedTerms = `@here test search "${currentUsername}" "@${currentUsername}" "${currentUserFirstName}"`; + const compareStore = mockStore(initialState); + compareStore.dispatch(SearchActions.searchPostsWithParams('', {include_deleted_channels: false, terms: mentionsQuotedTerms, is_or_search: true, time_zone_offset: timeZoneOffset, page: 0, per_page: 20})); compareStore.dispatch(SearchActions.searchFilesWithParams(currentTeamId, {include_deleted_channels: false, terms, is_or_search: true, time_zone_offset: timeZoneOffset, page: 0, per_page: 20})); expect(store.getActions()).toEqual(compareStore.getActions()); diff --git a/webapp/channels/src/actions/views/rhs.ts b/webapp/channels/src/actions/views/rhs.ts index 4eb51ad74c..2c92b1d69a 100644 --- a/webapp/channels/src/actions/views/rhs.ts +++ b/webapp/channels/src/actions/views/rhs.ts @@ -202,16 +202,18 @@ export function performSearch(terms: string, isMentionSearch?: boolean): ThunkAc } if (isMentionSearch) { - // Username should be quoted to allow specific search - // in case username is made with multiple words splitted by dashes or other symbols. + // Username and FirstName should be quoted to allow specific search + // in case the name is made with multiple words splitted by dashes or other symbols. const user = getCurrentUser(getState()); const termsArr = searchTerms.split(' ').filter((t) => Boolean(t && t.trim())); - const username = '@' + user.username; - const quotedUsername = `"${username}"`; + const atUsername = '@' + user.username; for (let i = 0; i < termsArr.length; i++) { - if (termsArr[i] === username) { - termsArr[i] = quotedUsername; - break; + if (termsArr[i] === atUsername) { + termsArr[i] = `"${atUsername}"`; + } else if (termsArr[i] === user.username) { + termsArr[i] = `"${user.username}"`; + } else if (termsArr[i] === user.first_name) { + termsArr[i] = `"${user.first_name}"`; } } searchTerms = termsArr.join(' ');