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

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

Merged
merged 3 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/linkify-matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export const options: Opts = {
const permalink = parsePermalink(href);
if (permalink?.userId) {
return {
// @ts-ignore see https://linkify.js.org/docs/options.html
click: function (e: MouseEvent) {
onUserClick(e, permalink.userId!);
},
Expand All @@ -150,7 +149,6 @@ export const options: Opts = {
if (localHref !== href) {
// it could be converted to a localHref -> therefore handle locally
return {
// @ts-ignore see https://linkify.js.org/docs/options.html
click: function (e: MouseEvent) {
e.preventDefault();
window.location.hash = localHref;
Expand All @@ -165,17 +163,15 @@ export const options: Opts = {
}
case Type.UserId:
return {
// @ts-ignore see https://linkify.js.org/docs/options.html
click: function (e: MouseEvent) {
const userId = parsePermalink(href)?.userId;
const userId = parsePermalink(href)?.userId ?? href;
if (userId) onUserClick(e, userId);
},
};
case Type.RoomAlias:
return {
// @ts-ignore see https://linkify.js.org/docs/options.html
click: function (e: MouseEvent) {
const alias = parsePermalink(href)?.roomIdOrAlias;
const alias = parsePermalink(href)?.roomIdOrAlias ?? href;
if (alias) onAliasClick(e, alias);
},
};
Expand Down
49 changes: 48 additions & 1 deletion test/linkify-matrix-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
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", () => {
const linkTypesByInitialCharacter: Record<string, string> = {
Expand Down Expand Up @@ -324,6 +329,26 @@ describe("linkify-matrix", () => {

describe("roomalias plugin", () => {
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", () => {
Expand All @@ -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", () => {
Expand Down