Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

types: Use TypeScript unions for message types.

Anders Kaseorg5cc65dd3c2e9
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index c54aa1f79ce..822f1039d30 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -160,7 +160,7 @@ export function by_conversation_and_time_url(message: Message): string {
const suffix = "/near/" + internal_url.encodeHashComponent(message.id.toString());
if (message.type === "stream") {
- return absolute_url + by_stream_topic_url(message.stream_id!, message.topic) + suffix;
+ return absolute_url + by_stream_topic_url(message.stream_id, message.topic) + suffix;
}
return absolute_url + people.pm_perma_link(message) + suffix;
diff --git a/web/src/recent_view_util.ts b/web/src/recent_view_util.ts
index f8658bba7e1..516ca693095 100644
--- a/web/src/recent_view_util.ts
+++ b/web/src/recent_view_util.ts
@@ -23,5 +23,5 @@ export function get_key_from_message(msg: Message): string | undefined {
}
// For messages with type = "stream".
- return get_topic_key(msg.stream_id!, msg.topic);
+ return get_topic_key(msg.stream_id, msg.topic);
}
diff --git a/web/src/types.ts b/web/src/types.ts
index 53297ded9c4..5fec49d58c6 100644
--- a/web/src/types.ts
+++ b/web/src/types.ts
@@ -5,7 +5,6 @@ export type MatchedMessage = {
};
// TODO/typescript: Move this to message_store
-export type MessageType = "private" | "stream";
export type MessageReactionType = "unicode_emoji" | "realm_emoji" | "zulip_extra_emoji";
// TODO/typescript: Move these types to message_store
@@ -71,17 +70,27 @@ export type RawMessage = {
sender_full_name: string;
sender_id: number;
sender_realm_str: string;
- stream_id?: number;
- subject: string;
submessages: Submessage[];
timestamp: number;
- topic_links: TopicLink[];
- type: MessageType;
flags: string[];
-} & MatchedMessage;
+} & (
+ | {
+ type: "private";
+ }
+ | {
+ type: "stream";
+ stream_id: number;
+ subject: string;
+ topic_links: TopicLink[];
+ }
+) &
+ MatchedMessage;
// We add these boolean properties to Raw message in `message_store.set_message_booleans` method.
-export type MessageWithBooleans = Omit<RawMessage, "flags"> & {
+export type MessageWithBooleans = (
+ | Omit<RawMessage & {type: "private"}, "flags">
+ | Omit<RawMessage & {type: "stream"}, "flags">
+) & {
unread: boolean;
historical: boolean;
starred: boolean;
@@ -108,26 +117,38 @@ export type MessageCleanReaction = {
};
// TODO/typescript: Move this to message_store
-export type Message = Omit<MessageWithBooleans, "reactions"> & {
+export type Message = (
+ | Omit<MessageWithBooleans & {type: "private"}, "reactions">
+ | Omit<MessageWithBooleans & {type: "stream"}, "reactions">
+) & {
// Added in `reactions.set_clean_reactions`.
clean_reactions: Map<string, MessageCleanReaction>;
// Added in `message_helper.process_new_message`.
sent_by_me: boolean;
- is_private?: boolean;
- is_stream?: boolean;
- stream?: string;
reply_to: string;
display_reply_to?: string;
- pm_with_url?: string;
- to_user_ids?: string;
- topic: string;
// These properties are used in `message_list_view.js`.
starred_status: string;
message_reactions: MessageCleanReaction[];
url: string;
-};
+} & (
+ | {
+ type: "private";
+ is_private: true;
+ is_stream: false;
+ pm_with_url: string;
+ to_user_ids: string;
+ }
+ | {
+ type: "stream";
+ is_private: false;
+ is_stream: true;
+ stream: string;
+ topic: string;
+ }
+ );
// TODO/typescript: Move this to server_events_dispatch
export type UserGroupUpdateEvent = {
diff --git a/web/src/util.ts b/web/src/util.ts
index bf9a49a4995..1c7529a2091 100644
--- a/web/src/util.ts
+++ b/web/src/util.ts
@@ -253,7 +253,7 @@ export function is_topic_synonym(operator: string): boolean {
}
export function convert_message_topic(message: Message): void {
- if (message.topic === undefined) {
+ if (message.type === "stream" && message.topic === undefined) {
message.topic = message.subject;
}
}