Move reaction message previews out of labs (#10601)
* Update reaction message previews to match designs * Delabs reaction message previews * tsc strict * Iterate * Add test * Iterate
This commit is contained in:
@@ -18,37 +18,39 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
||||
|
||||
import { IPreview } from "./IPreview";
|
||||
import { TagID } from "../models";
|
||||
import { getSenderName, isSelf, shouldPrefixMessagesIn } from "./utils";
|
||||
import { getSenderName, isSelf } from "./utils";
|
||||
import { _t } from "../../../languageHandler";
|
||||
import SettingsStore from "../../../settings/SettingsStore";
|
||||
import DMRoomMap from "../../../utils/DMRoomMap";
|
||||
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
||||
import { MessagePreviewStore } from "../MessagePreviewStore";
|
||||
|
||||
export class ReactionEventPreview implements IPreview {
|
||||
public getTextFor(event: MatrixEvent, tagId?: TagID, isThread?: boolean): string | null {
|
||||
const showDms = SettingsStore.getValue("feature_roomlist_preview_reactions_dms");
|
||||
const showAll = SettingsStore.getValue("feature_roomlist_preview_reactions_all");
|
||||
|
||||
const roomId = event.getRoomId();
|
||||
if (!roomId) return null; // not a room event
|
||||
|
||||
// If we're not showing all reactions, see if we're showing DMs instead
|
||||
if (!showAll) {
|
||||
// If we're not showing reactions on DMs, or we are and the room isn't a DM, skip
|
||||
if (!(showDms && DMRoomMap.shared().getUserIdForRoomId(roomId))) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const relation = event.getRelation();
|
||||
if (!relation) return null; // invalid reaction (probably redacted)
|
||||
|
||||
const reaction = relation.key;
|
||||
if (!reaction) return null; // invalid reaction (unknown format)
|
||||
|
||||
if (isThread || isSelf(event) || !shouldPrefixMessagesIn(roomId, tagId)) {
|
||||
return reaction;
|
||||
} else {
|
||||
return _t("%(senderName)s: %(reaction)s", { senderName: getSenderName(event), reaction });
|
||||
const cli = MatrixClientPeg.get();
|
||||
const room = cli?.getRoom(roomId);
|
||||
const relatedEvent = relation.event_id ? room?.findEventById(relation.event_id) : null;
|
||||
if (!relatedEvent) return null;
|
||||
|
||||
const message = MessagePreviewStore.instance.generatePreviewForEvent(relatedEvent);
|
||||
if (isSelf(event)) {
|
||||
return _t("You reacted %(reaction)s to %(message)s", {
|
||||
reaction,
|
||||
message,
|
||||
});
|
||||
}
|
||||
|
||||
return _t("%(sender)s reacted %(reaction)s to %(message)s", {
|
||||
sender: getSenderName(event),
|
||||
reaction,
|
||||
message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
||||
import { DefaultTagID, TagID } from "../models";
|
||||
|
||||
export function isSelf(event: MatrixEvent): boolean {
|
||||
const selfUserId = MatrixClientPeg.get().getUserId();
|
||||
const selfUserId = MatrixClientPeg.get().getSafeUserId();
|
||||
if (event.getType() === "m.room.member") {
|
||||
return event.getStateKey() === selfUserId;
|
||||
}
|
||||
@@ -37,5 +37,5 @@ export function shouldPrefixMessagesIn(roomId: string, tagId?: TagID): boolean {
|
||||
}
|
||||
|
||||
export function getSenderName(event: MatrixEvent): string {
|
||||
return event.sender ? event.sender.name : event.getSender() || "";
|
||||
return event.sender?.name ?? event.getSender() ?? "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user