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

Commit cb592dc

Browse files
authored
Fix clicking MXID in timeline going to matrix.to (#11263)
* Fix clicking MXID in timeline going to matrix.to * Add tests * Increase coverage
1 parent 3a784c7 commit cb592dc

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/linkify-matrix.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ export const options: Opts = {
139139
const permalink = parsePermalink(href);
140140
if (permalink?.userId) {
141141
return {
142-
// @ts-ignore see https://linkify.js.org/docs/options.html
143142
click: function (e: MouseEvent) {
144143
onUserClick(e, permalink.userId!);
145144
},
@@ -150,7 +149,6 @@ export const options: Opts = {
150149
if (localHref !== href) {
151150
// it could be converted to a localHref -> therefore handle locally
152151
return {
153-
// @ts-ignore see https://linkify.js.org/docs/options.html
154152
click: function (e: MouseEvent) {
155153
e.preventDefault();
156154
window.location.hash = localHref;
@@ -165,17 +163,15 @@ export const options: Opts = {
165163
}
166164
case Type.UserId:
167165
return {
168-
// @ts-ignore see https://linkify.js.org/docs/options.html
169166
click: function (e: MouseEvent) {
170-
const userId = parsePermalink(href)?.userId;
167+
const userId = parsePermalink(href)?.userId ?? href;
171168
if (userId) onUserClick(e, userId);
172169
},
173170
};
174171
case Type.RoomAlias:
175172
return {
176-
// @ts-ignore see https://linkify.js.org/docs/options.html
177173
click: function (e: MouseEvent) {
178-
const alias = parsePermalink(href)?.roomIdOrAlias;
174+
const alias = parsePermalink(href)?.roomIdOrAlias ?? href;
179175
if (alias) onAliasClick(e, alias);
180176
},
181177
};

test/linkify-matrix-test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
import { linkify, Type } from "../src/linkify-matrix";
16+
17+
import { EventListeners } from "linkifyjs";
18+
19+
import { linkify, Type, options } from "../src/linkify-matrix";
20+
import dispatcher from "../src/dispatcher/dispatcher";
21+
import { Action } from "../src/dispatcher/actions";
1722

1823
describe("linkify-matrix", () => {
1924
const linkTypesByInitialCharacter: Record<string, string> = {
@@ -324,6 +329,26 @@ describe("linkify-matrix", () => {
324329

325330
describe("roomalias plugin", () => {
326331
genTests("#");
332+
333+
it("should intercept clicks with a ViewRoom dispatch", () => {
334+
const dispatchSpy = jest.spyOn(dispatcher, "dispatch");
335+
336+
const handlers = (options.events as (href: string, type: string) => EventListeners)(
337+
"#room:server.com",
338+
"roomalias",
339+
);
340+
341+
const event = new MouseEvent("mousedown");
342+
event.preventDefault = jest.fn();
343+
handlers.click(event);
344+
expect(event.preventDefault).toHaveBeenCalled();
345+
expect(dispatchSpy).toHaveBeenCalledWith(
346+
expect.objectContaining({
347+
action: Action.ViewRoom,
348+
room_alias: "#room:server.com",
349+
}),
350+
);
351+
});
327352
});
328353

329354
describe("userid plugin", () => {
@@ -344,6 +369,28 @@ describe("linkify-matrix", () => {
344369
},
345370
]);
346371
});
372+
373+
it("should intercept clicks with a ViewUser dispatch", () => {
374+
const dispatchSpy = jest.spyOn(dispatcher, "dispatch");
375+
376+
const handlers = (options.events as (href: string, type: string) => EventListeners)(
377+
"@localpart:server.com",
378+
"userid",
379+
);
380+
381+
const event = new MouseEvent("mousedown");
382+
event.preventDefault = jest.fn();
383+
handlers.click(event);
384+
expect(event.preventDefault).toHaveBeenCalled();
385+
expect(dispatchSpy).toHaveBeenCalledWith(
386+
expect.objectContaining({
387+
action: Action.ViewUser,
388+
member: expect.objectContaining({
389+
userId: "@localpart:server.com",
390+
}),
391+
}),
392+
);
393+
});
347394
});
348395

349396
describe("matrix uri", () => {

0 commit comments

Comments
 (0)