From 8fe7df91714d995e7ad3c61e147ec9a7e0494824 Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Tue, 27 Jul 2021 15:37:05 +0100 Subject: [PATCH] When a single message is pinned, link to it Signed-off-by: Paulo Pinto --- src/TextForEvent.tsx | 44 ++++++++++++++++++++ src/i18n/strings/en_EN.json | 1 + test/TextForEvent-test.ts | 34 ++++++++++++--- test/__snapshots__/TextForEvent-test.ts.snap | 42 ++++++++++++++++++- 4 files changed, 114 insertions(+), 7 deletions(-) diff --git a/src/TextForEvent.tsx b/src/TextForEvent.tsx index 7bad8eb50e..17fede0d21 100644 --- a/src/TextForEvent.tsx +++ b/src/TextForEvent.tsx @@ -408,6 +408,15 @@ function textForPowerEvent(event: MatrixEvent): () => string | null { }); } +const onPinnedOrUnpinnedMessageClick = (messageId: string, roomId: string): void => { + defaultDispatcher.dispatch({ + action: 'view_room', + event_id: messageId, + highlighted: true, + room_id: roomId, + }); +}; + const onPinnedMessagesClick = (): void => { defaultDispatcher.dispatch({ action: Action.SetRightPanelPhase, @@ -420,6 +429,41 @@ function textForPinnedEvent(event: MatrixEvent, allowJSX: boolean): () => string if (!SettingsStore.getValue("feature_pinning")) return null; const senderName = event.sender ? event.sender.name : event.getSender(); + const pinned = event.getContent().pinned ?? []; + const previouslyPinned = event.getPrevContent().pinned ?? []; + const newlyPinned = pinned.filter(item => previouslyPinned.indexOf(item) < 0); + + if (newlyPinned.length === 1) { + // A single message was pinned, include a link to that message. + if (allowJSX) { + const messageId = newlyPinned.pop(); + const roomId = event.getRoomId(); + + return () => ( + + { + _t( + "%(senderName)s pinned a message to this room. See all pinned messages.", + { senderName }, + { + "a": (sub) => + onPinnedOrUnpinnedMessageClick(messageId, roomId)}> + { sub } + , + "b": (sub) => + + { sub } + , + }, + ) + } + + ); + } + + return () => _t("%(senderName)s pinned a message to this room. See all pinned messages.", { senderName }); + } + if (allowJSX) { return () => ( diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 0ca7b9da9e..eb390afb32 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -545,6 +545,7 @@ "%(senderName)s made future room history visible to unknown (%(visibility)s).": "%(senderName)s made future room history visible to unknown (%(visibility)s).", "%(senderName)s changed the power level of %(powerLevelDiffText)s.": "%(senderName)s changed the power level of %(powerLevelDiffText)s.", "%(userId)s from %(fromPowerLevel)s to %(toPowerLevel)s": "%(userId)s from %(fromPowerLevel)s to %(toPowerLevel)s", + "%(senderName)s pinned a message to this room. See all pinned messages.": "%(senderName)s pinned a message to this room. See all pinned messages.", "%(senderName)s changed the pinned messages for the room.": "%(senderName)s changed the pinned messages for the room.", "%(senderName)s changed the pinned messages for the room.": "%(senderName)s changed the pinned messages for the room.", "%(widgetName)s widget modified by %(senderName)s": "%(widgetName)s widget modified by %(senderName)s", diff --git a/test/TextForEvent-test.ts b/test/TextForEvent-test.ts index fdfb1c5711..91c99c7048 100644 --- a/test/TextForEvent-test.ts +++ b/test/TextForEvent-test.ts @@ -23,20 +23,42 @@ function mockPinnedEvent( }); } -describe("TextForPinnedEvent", () => { +describe("TextForPinnedEvent - newly pinned message(s)", () => { SettingsStore.setValue("feature_pinning", null, SettingLevel.DEVICE, true); - it("should mention sender", () => { - const event = mockPinnedEvent(); + it("mentions message when a single message was pinned, with no previously pinned messages", () => { + const event = mockPinnedEvent(['message-1']); + expect(textForEvent(event)).toBe("@foo:example.com pinned a message to this room. See all pinned messages."); + }); + + it("mentions message when a single message was pinned, with multiple previously pinned messages", () => { + const event = mockPinnedEvent(['message-3'], ['message-1', 'message-2']); + expect(textForEvent(event)).toBe("@foo:example.com pinned a message to this room. See all pinned messages."); + }); + + it("shows generic text when multiple messages were pinned", () => { + const event = mockPinnedEvent(['message-2', 'message-3'], ['message-1']); expect(textForEvent(event)).toBe("@foo:example.com changed the pinned messages for the room."); }); }); -describe("TextForPinnedEvent (JSX)", () => { +describe("TextForPinnedEvent - newly pinned message(s) (JSX)", () => { SettingsStore.setValue("feature_pinning", null, SettingLevel.DEVICE, true); - it("should mention sender", () => { - const event = mockPinnedEvent(); + it("mentions message when a single message was pinned, with no previously pinned messages", () => { + const event = mockPinnedEvent(['message-1']); + const component = renderer.create(textForEvent(event, true)); + expect(component.toJSON()).toMatchSnapshot(); + }); + + it("mentions message when a single message was pinned, with multiple previously pinned messages", () => { + const event = mockPinnedEvent(['message-3'], ['message-1', 'message-2']); + const component = renderer.create(textForEvent(event, true)); + expect(component.toJSON()).toMatchSnapshot(); + }); + + it("shows generic text when multiple messages were pinned", () => { + const event = mockPinnedEvent(['message-2', 'message-3'], ['message-1']); const component = renderer.create(textForEvent(event, true)); expect(component.toJSON()).toMatchSnapshot(); }); diff --git a/test/__snapshots__/TextForEvent-test.ts.snap b/test/__snapshots__/TextForEvent-test.ts.snap index 78fd0910b8..41ccbc0857 100644 --- a/test/__snapshots__/TextForEvent-test.ts.snap +++ b/test/__snapshots__/TextForEvent-test.ts.snap @@ -1,6 +1,46 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`TextForPinnedEvent (JSX) should mention sender 1`] = ` +exports[`TextForPinnedEvent - newly pinned message(s) (JSX) mentions message when a single message was pinned, with multiple previously pinned messages 1`] = ` + + + @foo:example.com pinned + + a message + + to this room. See all + + pinned messages + + . + + +`; + +exports[`TextForPinnedEvent - newly pinned message(s) (JSX) mentions message when a single message was pinned, with no previously pinned messages 1`] = ` + + + @foo:example.com pinned + + a message + + to this room. See all + + pinned messages + + . + + +`; + +exports[`TextForPinnedEvent - newly pinned message(s) (JSX) shows generic text when multiple messages were pinned 1`] = ` @foo:example.com changed the