Fix clicking MXID in timeline going to matrix.to (#11263)

* Fix clicking MXID in timeline going to matrix.to

* Add tests

* Increase coverage
pull/28217/head
Michael Telatynski 2023-07-14 11:55:55 +01:00 committed by GitHub
parent 3a784c71e8
commit cb592dc709
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 7 deletions

View File

@ -139,7 +139,6 @@ export const options: Opts = {
const permalink = parsePermalink(href); const permalink = parsePermalink(href);
if (permalink?.userId) { if (permalink?.userId) {
return { return {
// @ts-ignore see https://linkify.js.org/docs/options.html
click: function (e: MouseEvent) { click: function (e: MouseEvent) {
onUserClick(e, permalink.userId!); onUserClick(e, permalink.userId!);
}, },
@ -150,7 +149,6 @@ export const options: Opts = {
if (localHref !== href) { if (localHref !== href) {
// it could be converted to a localHref -> therefore handle locally // it could be converted to a localHref -> therefore handle locally
return { return {
// @ts-ignore see https://linkify.js.org/docs/options.html
click: function (e: MouseEvent) { click: function (e: MouseEvent) {
e.preventDefault(); e.preventDefault();
window.location.hash = localHref; window.location.hash = localHref;
@ -165,17 +163,15 @@ export const options: Opts = {
} }
case Type.UserId: case Type.UserId:
return { return {
// @ts-ignore see https://linkify.js.org/docs/options.html
click: function (e: MouseEvent) { click: function (e: MouseEvent) {
const userId = parsePermalink(href)?.userId; const userId = parsePermalink(href)?.userId ?? href;
if (userId) onUserClick(e, userId); if (userId) onUserClick(e, userId);
}, },
}; };
case Type.RoomAlias: case Type.RoomAlias:
return { return {
// @ts-ignore see https://linkify.js.org/docs/options.html
click: function (e: MouseEvent) { click: function (e: MouseEvent) {
const alias = parsePermalink(href)?.roomIdOrAlias; const alias = parsePermalink(href)?.roomIdOrAlias ?? href;
if (alias) onAliasClick(e, alias); if (alias) onAliasClick(e, alias);
}, },
}; };

View File

@ -13,7 +13,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { linkify, Type } from "../src/linkify-matrix";
import { EventListeners } from "linkifyjs";
import { linkify, Type, options } from "../src/linkify-matrix";
import dispatcher from "../src/dispatcher/dispatcher";
import { Action } from "../src/dispatcher/actions";
describe("linkify-matrix", () => { describe("linkify-matrix", () => {
const linkTypesByInitialCharacter: Record<string, string> = { const linkTypesByInitialCharacter: Record<string, string> = {
@ -324,6 +329,26 @@ describe("linkify-matrix", () => {
describe("roomalias plugin", () => { describe("roomalias plugin", () => {
genTests("#"); genTests("#");
it("should intercept clicks with a ViewRoom dispatch", () => {
const dispatchSpy = jest.spyOn(dispatcher, "dispatch");
const handlers = (options.events as (href: string, type: string) => EventListeners)(
"#room:server.com",
"roomalias",
);
const event = new MouseEvent("mousedown");
event.preventDefault = jest.fn();
handlers.click(event);
expect(event.preventDefault).toHaveBeenCalled();
expect(dispatchSpy).toHaveBeenCalledWith(
expect.objectContaining({
action: Action.ViewRoom,
room_alias: "#room:server.com",
}),
);
});
}); });
describe("userid plugin", () => { describe("userid plugin", () => {
@ -344,6 +369,28 @@ describe("linkify-matrix", () => {
}, },
]); ]);
}); });
it("should intercept clicks with a ViewUser dispatch", () => {
const dispatchSpy = jest.spyOn(dispatcher, "dispatch");
const handlers = (options.events as (href: string, type: string) => EventListeners)(
"@localpart:server.com",
"userid",
);
const event = new MouseEvent("mousedown");
event.preventDefault = jest.fn();
handlers.click(event);
expect(event.preventDefault).toHaveBeenCalled();
expect(dispatchSpy).toHaveBeenCalledWith(
expect.objectContaining({
action: Action.ViewUser,
member: expect.objectContaining({
userId: "@localpart:server.com",
}),
}),
);
});
}); });
describe("matrix uri", () => { describe("matrix uri", () => {