From 2660e25d6e932627a0814cd7d3b34a4d26a9865a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 13 Jul 2021 13:04:37 +0200 Subject: [PATCH] Deduplicate some code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/views/rooms/EventTile.tsx | 31 ++----------------- src/components/views/rooms/ReplyTile.tsx | 24 ++------------- src/utils/EventUtils.ts | 38 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 49 deletions(-) diff --git a/src/components/views/rooms/EventTile.tsx b/src/components/views/rooms/EventTile.tsx index bf2438d267..b1e75443a0 100644 --- a/src/components/views/rooms/EventTile.tsx +++ b/src/components/views/rooms/EventTile.tsx @@ -54,6 +54,7 @@ import TooltipButton from '../elements/TooltipButton'; import ReadReceiptMarker from "./ReadReceiptMarker"; import MessageActionBar from "../messages/MessageActionBar"; import ReactionsRow from '../messages/ReactionsRow'; +import { getEventDisplayInfo } from '../../../utils/EventUtils'; const eventTileTypes = { [EventType.RoomMessage]: 'messages.MessageEvent', @@ -845,35 +846,9 @@ export default class EventTile extends React.Component { }; render() { - //console.info("EventTile showUrlPreview for %s is %s", this.props.mxEvent.getId(), this.props.showUrlPreview); + const msgtype = this.props.mxEvent.getContent().msgtype; + const { tileHandler, isBubbleMessage, isInfoMessage } = getEventDisplayInfo(this.props.mxEvent); - const content = this.props.mxEvent.getContent(); - const msgtype = content.msgtype; - const eventType = this.props.mxEvent.getType(); - - let tileHandler = getHandlerTile(this.props.mxEvent); - - // Info messages are basically information about commands processed on a room - let isBubbleMessage = eventType.startsWith("m.key.verification") || - (eventType === EventType.RoomMessage && msgtype && msgtype.startsWith("m.key.verification")) || - (eventType === EventType.RoomCreate) || - (eventType === EventType.RoomEncryption) || - (tileHandler === "messages.MJitsiWidgetEvent"); - let isInfoMessage = ( - !isBubbleMessage && eventType !== EventType.RoomMessage && - eventType !== EventType.Sticker && eventType !== EventType.RoomCreate - ); - - // If we're showing hidden events in the timeline, we should use the - // source tile when there's no regular tile for an event and also for - // replace relations (which otherwise would display as a confusing - // duplicate of the thing they are replacing). - if (SettingsStore.getValue("showHiddenEventsInTimeline") && !haveTileForEvent(this.props.mxEvent)) { - tileHandler = "messages.ViewSourceEvent"; - isBubbleMessage = false; - // Reuse info message avatar and sender profile styling - isInfoMessage = true; - } // This shouldn't happen: the caller should check we support this type // before trying to instantiate us if (!tileHandler) { diff --git a/src/components/views/rooms/ReplyTile.tsx b/src/components/views/rooms/ReplyTile.tsx index 19da345579..054a920d64 100644 --- a/src/components/views/rooms/ReplyTile.tsx +++ b/src/components/views/rooms/ReplyTile.tsx @@ -28,6 +28,7 @@ import MImageReplyBody from "../messages/MImageReplyBody"; import * as sdk from '../../../index'; import { EventType, MsgType, RelationType } from 'matrix-js-sdk/src/@types/event'; import { replaceableComponent } from '../../../utils/replaceableComponent'; +import { getEventDisplayInfo } from '../../../utils/EventUtils'; interface IProps { mxEvent: MatrixEvent; @@ -80,28 +81,9 @@ export default class ReplyTile extends React.PureComponent { }; render() { - const content = this.props.mxEvent.getContent(); - const msgtype = content.msgtype; - const eventType = this.props.mxEvent.getType(); + const msgtype = this.props.mxEvent.getContent().msgtype; - // Info messages are basically information about commands processed on a room - let isInfoMessage = ![ - EventType.RoomMessage, - EventType.Sticker, - EventType.RoomCreate, - ].includes(eventType as EventType); - - let tileHandler = getHandlerTile(this.props.mxEvent); - // If we're showing hidden events in the timeline, we should use the - // source tile when there's no regular tile for an event and also for - // replace relations (which otherwise would display as a confusing - // duplicate of the thing they are replacing). - const useSource = !tileHandler || this.props.mxEvent.isRelation(RelationType.Replace); - if (useSource && SettingsStore.getValue("showHiddenEventsInTimeline")) { - tileHandler = "messages.ViewSourceEvent"; - // Reuse info message avatar and sender profile styling - isInfoMessage = true; - } + const { tileHandler, isInfoMessage } = getEventDisplayInfo(this.props.mxEvent); // This shouldn't happen: the caller should check we support this type // before trying to instantiate us if (!tileHandler) { diff --git a/src/utils/EventUtils.ts b/src/utils/EventUtils.ts index 1a467b157f..d69c285e18 100644 --- a/src/utils/EventUtils.ts +++ b/src/utils/EventUtils.ts @@ -19,6 +19,9 @@ import { MatrixEvent, EventStatus } from 'matrix-js-sdk/src/models/event'; import { MatrixClientPeg } from '../MatrixClientPeg'; import shouldHideEvent from "../shouldHideEvent"; +import { getHandlerTile, haveTileForEvent } from "../components/views/rooms/EventTile"; +import SettingsStore from "../settings/SettingsStore"; +import { EventType } from "matrix-js-sdk/src/@types/event"; /** * Returns whether an event should allow actions like reply, reactions, edit, etc. @@ -96,3 +99,38 @@ export function findEditableEvent(room: Room, isForward: boolean, fromEventId: s } } +export function getEventDisplayInfo(mxEvent: MatrixEvent): { + isInfoMessage: boolean; + tileHandler; + isBubbleMessage: boolean; +} { + const content = mxEvent.getContent(); + const msgtype = content.msgtype; + const eventType = mxEvent.getType(); + + let tileHandler = getHandlerTile(mxEvent); + + // Info messages are basically information about commands processed on a room + let isBubbleMessage = eventType.startsWith("m.key.verification") || + (eventType === EventType.RoomMessage && msgtype && msgtype.startsWith("m.key.verification")) || + (eventType === EventType.RoomCreate) || + (eventType === EventType.RoomEncryption) || + (tileHandler === "messages.MJitsiWidgetEvent"); + let isInfoMessage = ( + !isBubbleMessage && eventType !== EventType.RoomMessage && + eventType !== EventType.Sticker && eventType !== EventType.RoomCreate + ); + + // If we're showing hidden events in the timeline, we should use the + // source tile when there's no regular tile for an event and also for + // replace relations (which otherwise would display as a confusing + // duplicate of the thing they are replacing). + if (SettingsStore.getValue("showHiddenEventsInTimeline") && !haveTileForEvent(mxEvent)) { + tileHandler = "messages.ViewSourceEvent"; + isBubbleMessage = false; + // Reuse info message avatar and sender profile styling + isInfoMessage = true; + } + + return { tileHandler, isInfoMessage, isBubbleMessage }; +}