Prefer cached settings values in shouldHideEvent

Signed-off-by: Robin Townsend <robin@robin.town>
This commit is contained in:
Robin Townsend 2021-06-05 01:25:01 -04:00
parent 48d3e41351
commit 13196b8146

View file

@ -17,6 +17,7 @@
import {MatrixEvent} from "matrix-js-sdk/src/models/event"; import {MatrixEvent} from "matrix-js-sdk/src/models/event";
import SettingsStore from "./settings/SettingsStore"; import SettingsStore from "./settings/SettingsStore";
import {IState} from "./components/structures/RoomView";
interface IDiff { interface IDiff {
isMemberEvent: boolean; isMemberEvent: boolean;
@ -47,11 +48,18 @@ function memberEventDiff(ev: MatrixEvent): IDiff {
return diff; return diff;
} }
export default function shouldHideEvent(ev: MatrixEvent): boolean { /**
// Wrap getValue() for readability. Calling the SettingsStore can be * Determines whether the given event should be hidden from timelines.
// fairly resource heavy, so the checks below should avoid hitting it * @param ev The event
// where possible. * @param ctx An optional RoomContext to pull cached settings values from to avoid
const isEnabled = (name) => SettingsStore.getValue(name, ev.getRoomId()); * hitting the settings store
*/
export default function shouldHideEvent(ev: MatrixEvent, ctx?: IState): boolean {
// Accessing the settings store directly can be expensive if done frequently,
// so we should prefer using cached values if a RoomContext is available
const isEnabled = ctx ?
name => ctx[name] :
name => SettingsStore.getValue(name, ev.getRoomId());
// Hide redacted events // Hide redacted events
if (ev.isRedacted() && !isEnabled('showRedactions')) return true; if (ev.isRedacted() && !isEnabled('showRedactions')) return true;