Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 739eec8

Browse files
bolu-tifeBoluwatife Omosowonturt2live
authored
Allows search to recognize full room links (#8275)
* fix matrix search link * fix matrix search link Signed-off-by: Boluwatife Omosowon <[email protected]> * fix: allow full link search Signed-off-by: Boluwatife Omosowon <[email protected]> * fix: allow full link search on new search feature Signed-off-by: Boluwatife Omosowon <[email protected]> * improve transformSearchTerm function Signed-off-by: Boluwatife Omosowon <[email protected]> * improve transformSearchTerm function Signed-off-by: Boluwatife Omosowon <[email protected]> * add review changes * Signed-off-by: Boluwatife Omosowon <[email protected]> added review correction changed the transformSearchTerm function to use parsePermaLink removed extra spaces * add angle brackets to copyright email title * removed extra space Signed-off-by: Boluwatife Omosowon <[email protected]> * Update src/utils/SearchInput.ts Co-authored-by: Travis Ralston <[email protected]> * fixed spolight dialog search for room and user links * added tests for transformSearchTerm * removed transformSearchTerm from room search bar * replaces two test cases to one that should return the primaryEntityId if the search term was a permalink * corrected ts issues * changed type of transformSearchTerm to string * changed return value from empty string to the original search term if the primaryEntityId of the parsedLink is null * changed return value from empty string to the original search term if the primaryEntityId of the parsedLink is null * refactored transformSearchTerm and added a new test case * rewrote transformSearchTerm doc * changed mocked return values of test case - should return the original search term if the search term is a permalink and the primaryEntityId is null * lint corrections --------- Signed-off-by: Boluwatife Omosowon <[email protected]> Co-authored-by: Boluwatife Omosowon <[email protected]> Co-authored-by: Travis Ralston <[email protected]>
1 parent bf641d1 commit 739eec8

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

src/components/views/dialogs/spotlight/SpotlightDialog.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import { isLocalRoom } from "../../../../utils/localRoom/isLocalRoom";
8989
import RoomAvatar from "../../avatars/RoomAvatar";
9090
import { useFeatureEnabled } from "../../../../hooks/useSettings";
9191
import { filterBoolean } from "../../../../utils/arrays";
92+
import { transformSearchTerm } from "../../../../utils/SearchInput";
9293

9394
const MAX_RECENT_SEARCHES = 10;
9495
const SECTION_LIMIT = 50; // only show 50 results per section for performance reasons
@@ -466,7 +467,7 @@ const SpotlightDialog: React.FC<IProps> = ({ initialText = "", initialFilter = n
466467
const [spaceResults, spaceResultsLoading] = useSpaceResults(activeSpace ?? undefined, query);
467468

468469
const setQuery = (e: ChangeEvent<HTMLInputElement>): void => {
469-
const newQuery = e.currentTarget.value;
470+
const newQuery = transformSearchTerm(e.currentTarget.value);
470471
_setQuery(newQuery);
471472
};
472473
useEffect(() => {

src/utils/SearchInput.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright 2023 Boluwatife Omosowon <[email protected]>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { parsePermalink } from "./permalinks/Permalinks";
18+
19+
/**
20+
* Returns the primaryEntityId(roomIdOrAlias or userId) if the search term
21+
* is a permalink and the primaryEntityId is not null. Otherwise, it returns
22+
* the original search term.
23+
* E.g https://matrix.to/#/#element-dev:matrix.org returns #element-dev:matrix.org
24+
* @param {string} searchTerm The search term.
25+
* @returns {string} The roomId, alias, userId, or the original search term
26+
*/
27+
export function transformSearchTerm(searchTerm: string): string {
28+
const parseLink = parsePermalink(searchTerm);
29+
return parseLink?.primaryEntityId ?? searchTerm;
30+
}

test/utils/SearchInput-test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2023 Boluwatife Omosowon <[email protected]>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
import { mocked } from "jest-mock";
17+
18+
import { parsePermalink } from "../../src/utils/permalinks/Permalinks";
19+
import { transformSearchTerm } from "../../src/utils/SearchInput";
20+
21+
jest.mock("../../src/utils/permalinks/Permalinks");
22+
23+
describe("transforming search term", () => {
24+
it("should return the primaryEntityId if the search term was a permalink", () => {
25+
const roomLink = "https://matrix.to/#/#element-dev:matrix.org";
26+
const parsedPermalink = "#element-dev:matrix.org";
27+
28+
mocked(parsePermalink).mockReturnValue({
29+
primaryEntityId: parsedPermalink,
30+
roomIdOrAlias: parsedPermalink,
31+
eventId: "",
32+
userId: "",
33+
viaServers: [],
34+
sigil: "",
35+
});
36+
37+
expect(transformSearchTerm(roomLink)).toBe(parsedPermalink);
38+
});
39+
40+
it("should return the original search term if the search term is a permalink and the primaryEntityId is null", () => {
41+
const searchTerm = "https://matrix.to/#/#random-link:matrix.org";
42+
43+
mocked(parsePermalink).mockReturnValue({
44+
primaryEntityId: null,
45+
roomIdOrAlias: null,
46+
eventId: null,
47+
userId: null,
48+
viaServers: null,
49+
sigil: "?",
50+
});
51+
52+
expect(transformSearchTerm(searchTerm)).toBe(searchTerm);
53+
});
54+
55+
it("should return the original search term if the search term was not a permalink", () => {
56+
const searchTerm = "search term";
57+
mocked(parsePermalink).mockReturnValue(null);
58+
expect(transformSearchTerm(searchTerm)).toBe(searchTerm);
59+
});
60+
});

0 commit comments

Comments
 (0)