Conform more code to strict null checking (#10153)
* Conform more code to strict null checking * Conform more code to strict null checking * Iterate * Iterate
This commit is contained in:
parent
a4ff959aa1
commit
145a5a8a8d
89 changed files with 520 additions and 551 deletions
|
@ -43,12 +43,12 @@ import { getSenderName } from "./utils/event/getSenderName";
|
|||
function getRoomMemberDisplayname(event: MatrixEvent, userId = event.getSender()): string {
|
||||
const client = MatrixClientPeg.get();
|
||||
const roomId = event.getRoomId();
|
||||
const member = client.getRoom(roomId)?.getMember(userId);
|
||||
const member = client.getRoom(roomId)?.getMember(userId!);
|
||||
return member?.name || member?.rawDisplayName || userId || _t("Someone");
|
||||
}
|
||||
|
||||
function textForCallEvent(event: MatrixEvent): () => string {
|
||||
const roomName = MatrixClientPeg.get().getRoom(event.getRoomId()!).name;
|
||||
const roomName = MatrixClientPeg.get().getRoom(event.getRoomId()!)?.name;
|
||||
const isSupported = MatrixClientPeg.get().supportsVoip();
|
||||
|
||||
return isSupported
|
||||
|
@ -60,7 +60,7 @@ function textForCallEvent(event: MatrixEvent): () => string {
|
|||
// any text to display at all. For this reason they return deferred values
|
||||
// to avoid the expense of looking up translations when they're not needed.
|
||||
|
||||
function textForCallInviteEvent(event: MatrixEvent): () => string | null {
|
||||
function textForCallInviteEvent(event: MatrixEvent): (() => string) | null {
|
||||
const senderName = getSenderName(event);
|
||||
// FIXME: Find a better way to determine this from the event?
|
||||
const isVoice = !event.getContent().offer?.sdp?.includes("m=video");
|
||||
|
@ -78,9 +78,11 @@ function textForCallInviteEvent(event: MatrixEvent): () => string | null {
|
|||
} else if (!isVoice && !isSupported) {
|
||||
return () => _t("%(senderName)s placed a video call. (not supported by this browser)", { senderName });
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function textForMemberEvent(ev: MatrixEvent, allowJSX: boolean, showHiddenEvents?: boolean): () => string | null {
|
||||
function textForMemberEvent(ev: MatrixEvent, allowJSX: boolean, showHiddenEvents?: boolean): (() => string) | null {
|
||||
// XXX: SYJS-16 "sender is sometimes null for join messages"
|
||||
const senderName = ev.sender?.name || getRoomMemberDisplayname(ev);
|
||||
const targetName = ev.target?.name || getRoomMemberDisplayname(ev, ev.getStateKey());
|
||||
|
@ -187,9 +189,11 @@ function textForMemberEvent(ev: MatrixEvent, allowJSX: boolean, showHiddenEvents
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function textForTopicEvent(ev: MatrixEvent): () => string | null {
|
||||
function textForTopicEvent(ev: MatrixEvent): (() => string) | null {
|
||||
const senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
|
||||
return () =>
|
||||
_t('%(senderDisplayName)s changed the topic to "%(topic)s".', {
|
||||
|
@ -198,12 +202,12 @@ function textForTopicEvent(ev: MatrixEvent): () => string | null {
|
|||
});
|
||||
}
|
||||
|
||||
function textForRoomAvatarEvent(ev: MatrixEvent): () => string | null {
|
||||
function textForRoomAvatarEvent(ev: MatrixEvent): (() => string) | null {
|
||||
const senderDisplayName = ev?.sender?.name || ev.getSender();
|
||||
return () => _t("%(senderDisplayName)s changed the room avatar.", { senderDisplayName });
|
||||
}
|
||||
|
||||
function textForRoomNameEvent(ev: MatrixEvent): () => string | null {
|
||||
function textForRoomNameEvent(ev: MatrixEvent): (() => string) | null {
|
||||
const senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
|
||||
|
||||
if (!ev.getContent().name || ev.getContent().name.trim().length === 0) {
|
||||
|
@ -224,7 +228,7 @@ function textForRoomNameEvent(ev: MatrixEvent): () => string | null {
|
|||
});
|
||||
}
|
||||
|
||||
function textForTombstoneEvent(ev: MatrixEvent): () => string | null {
|
||||
function textForTombstoneEvent(ev: MatrixEvent): (() => string) | null {
|
||||
const senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
|
||||
return () => _t("%(senderDisplayName)s upgraded this room.", { senderDisplayName });
|
||||
}
|
||||
|
@ -281,7 +285,7 @@ function textForJoinRulesEvent(ev: MatrixEvent, allowJSX: boolean): () => Render
|
|||
}
|
||||
}
|
||||
|
||||
function textForGuestAccessEvent(ev: MatrixEvent): () => string | null {
|
||||
function textForGuestAccessEvent(ev: MatrixEvent): (() => string) | null {
|
||||
const senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
|
||||
switch (ev.getContent().guest_access) {
|
||||
case GuestAccess.CanJoin:
|
||||
|
@ -298,7 +302,7 @@ function textForGuestAccessEvent(ev: MatrixEvent): () => string | null {
|
|||
}
|
||||
}
|
||||
|
||||
function textForServerACLEvent(ev: MatrixEvent): () => string | null {
|
||||
function textForServerACLEvent(ev: MatrixEvent): (() => string) | null {
|
||||
const senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
|
||||
const prevContent = ev.getPrevContent();
|
||||
const current = ev.getContent();
|
||||
|
@ -308,7 +312,7 @@ function textForServerACLEvent(ev: MatrixEvent): () => string | null {
|
|||
allow_ip_literals: prevContent.allow_ip_literals !== false,
|
||||
};
|
||||
|
||||
let getText: () => string = null;
|
||||
let getText: () => string;
|
||||
if (prev.deny.length === 0 && prev.allow.length === 0) {
|
||||
getText = () => _t("%(senderDisplayName)s set the server ACLs for this room.", { senderDisplayName });
|
||||
} else {
|
||||
|
@ -328,7 +332,7 @@ function textForServerACLEvent(ev: MatrixEvent): () => string | null {
|
|||
return getText;
|
||||
}
|
||||
|
||||
function textForMessageEvent(ev: MatrixEvent): () => string | null {
|
||||
function textForMessageEvent(ev: MatrixEvent): (() => string) | null {
|
||||
if (isLocationEvent(ev)) {
|
||||
return textForLocationEvent(ev);
|
||||
}
|
||||
|
@ -354,7 +358,7 @@ function textForMessageEvent(ev: MatrixEvent): () => string | null {
|
|||
};
|
||||
}
|
||||
|
||||
function textForCanonicalAliasEvent(ev: MatrixEvent): () => string | null {
|
||||
function textForCanonicalAliasEvent(ev: MatrixEvent): (() => string) | null {
|
||||
const senderName = getSenderName(ev);
|
||||
const oldAlias = ev.getPrevContent().alias;
|
||||
const oldAltAliases = ev.getPrevContent().alt_aliases || [];
|
||||
|
@ -414,7 +418,7 @@ function textForCanonicalAliasEvent(ev: MatrixEvent): () => string | null {
|
|||
});
|
||||
}
|
||||
|
||||
function textForThreePidInviteEvent(event: MatrixEvent): () => string | null {
|
||||
function textForThreePidInviteEvent(event: MatrixEvent): (() => string) | null {
|
||||
const senderName = getSenderName(event);
|
||||
|
||||
if (!isValid3pidInvite(event)) {
|
||||
|
@ -432,7 +436,7 @@ function textForThreePidInviteEvent(event: MatrixEvent): () => string | null {
|
|||
});
|
||||
}
|
||||
|
||||
function textForHistoryVisibilityEvent(event: MatrixEvent): () => string | null {
|
||||
function textForHistoryVisibilityEvent(event: MatrixEvent): (() => string) | null {
|
||||
const senderName = getSenderName(event);
|
||||
switch (event.getContent().history_visibility) {
|
||||
case HistoryVisibility.Invited:
|
||||
|
@ -463,7 +467,7 @@ function textForHistoryVisibilityEvent(event: MatrixEvent): () => string | null
|
|||
}
|
||||
|
||||
// Currently will only display a change if a user's power level is changed
|
||||
function textForPowerEvent(event: MatrixEvent): () => string | null {
|
||||
function textForPowerEvent(event: MatrixEvent): (() => string) | null {
|
||||
const senderName = getSenderName(event);
|
||||
if (!event.getPrevContent()?.users || !event.getContent()?.users) {
|
||||
return null;
|
||||
|
@ -528,10 +532,10 @@ const onPinnedMessagesClick = (): void => {
|
|||
RightPanelStore.instance.setCard({ phase: RightPanelPhases.PinnedMessages }, false);
|
||||
};
|
||||
|
||||
function textForPinnedEvent(event: MatrixEvent, allowJSX: boolean): () => Renderable {
|
||||
function textForPinnedEvent(event: MatrixEvent, allowJSX: boolean): (() => Renderable) | null {
|
||||
if (!SettingsStore.getValue("feature_pinning")) return null;
|
||||
const senderName = getSenderName(event);
|
||||
const roomId = event.getRoomId();
|
||||
const roomId = event.getRoomId()!;
|
||||
|
||||
const pinned = event.getContent<{ pinned: string[] }>().pinned ?? [];
|
||||
const previouslyPinned: string[] = event.getPrevContent().pinned ?? [];
|
||||
|
@ -625,7 +629,7 @@ function textForPinnedEvent(event: MatrixEvent, allowJSX: boolean): () => Render
|
|||
return () => _t("%(senderName)s changed the pinned messages for the room.", { senderName });
|
||||
}
|
||||
|
||||
function textForWidgetEvent(event: MatrixEvent): () => string | null {
|
||||
function textForWidgetEvent(event: MatrixEvent): (() => string) | null {
|
||||
const senderName = getSenderName(event);
|
||||
const { name: prevName, type: prevType, url: prevUrl } = event.getPrevContent();
|
||||
const { name, type, url } = event.getContent() || {};
|
||||
|
@ -661,12 +665,12 @@ function textForWidgetEvent(event: MatrixEvent): () => string | null {
|
|||
}
|
||||
}
|
||||
|
||||
function textForWidgetLayoutEvent(event: MatrixEvent): () => string | null {
|
||||
function textForWidgetLayoutEvent(event: MatrixEvent): (() => string) | null {
|
||||
const senderName = getSenderName(event);
|
||||
return () => _t("%(senderName)s has updated the room layout", { senderName });
|
||||
}
|
||||
|
||||
function textForMjolnirEvent(event: MatrixEvent): () => string | null {
|
||||
function textForMjolnirEvent(event: MatrixEvent): (() => string) | null {
|
||||
const senderName = getSenderName(event);
|
||||
const { entity: prevEntity } = event.getPrevContent();
|
||||
const { entity, recommendation, reason } = event.getContent();
|
||||
|
@ -795,7 +799,7 @@ function textForMjolnirEvent(event: MatrixEvent): () => string | null {
|
|||
);
|
||||
}
|
||||
|
||||
export function textForLocationEvent(event: MatrixEvent): () => string | null {
|
||||
export function textForLocationEvent(event: MatrixEvent): () => string {
|
||||
return () =>
|
||||
_t("%(senderName)s has shared their location", {
|
||||
senderName: getSenderName(event),
|
||||
|
@ -817,7 +821,7 @@ function textForRedactedPollAndMessageEvent(ev: MatrixEvent): string {
|
|||
return message;
|
||||
}
|
||||
|
||||
function textForPollStartEvent(event: MatrixEvent): () => string | null {
|
||||
function textForPollStartEvent(event: MatrixEvent): (() => string) | null {
|
||||
return () => {
|
||||
let message = "";
|
||||
|
||||
|
@ -836,7 +840,7 @@ function textForPollStartEvent(event: MatrixEvent): () => string | null {
|
|||
};
|
||||
}
|
||||
|
||||
function textForPollEndEvent(event: MatrixEvent): () => string | null {
|
||||
function textForPollEndEvent(event: MatrixEvent): (() => string) | null {
|
||||
return () =>
|
||||
_t("%(senderName)s has ended a poll", {
|
||||
senderName: getSenderName(event),
|
||||
|
@ -846,7 +850,7 @@ function textForPollEndEvent(event: MatrixEvent): () => string | null {
|
|||
type Renderable = string | React.ReactNode | null;
|
||||
|
||||
interface IHandlers {
|
||||
[type: string]: (ev: MatrixEvent, allowJSX: boolean, showHiddenEvents?: boolean) => () => Renderable;
|
||||
[type: string]: (ev: MatrixEvent, allowJSX: boolean, showHiddenEvents?: boolean) => (() => Renderable) | null;
|
||||
}
|
||||
|
||||
const handlers: IHandlers = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue