Skip to content

Commit 11f5a64

Browse files
committed
Support using 'direct' as the message type for private messages
1 parent b7fbf1b commit 11f5a64

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

zulip/zulip/cli.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ def log_exit(response: Dict[str, Any]) -> None:
5151
help="Allows the user to specify a subject for the message.",
5252
)
5353
@click.option("--message", "-m", required=True)
54-
def send_message(recipients: List[str], stream: str, subject: str, message: str) -> None:
54+
@click.option(
55+
"--type",
56+
"-t",
57+
"message_type",
58+
default="private",
59+
help="Message type: 'stream', 'private', or 'direct'.",
60+
)
61+
def send_message(
62+
recipients: List[str], stream: str, subject: str, message: str, message_type: str
63+
) -> None:
5564
"""Sends a message and optionally prints status about the same."""
5665

5766
# Sanity check user data
@@ -61,35 +70,54 @@ def send_message(recipients: List[str], stream: str, subject: str, message: str)
6170
click.echo("You cannot specify both a username and a stream/subject.")
6271
raise SystemExit(1)
6372
if len(recipients) == 0 and has_stream != has_subject:
64-
click.echo("Stream messages must have a subject")
73+
click.echo("Stream messages must have a subject.")
6574
raise SystemExit(1)
6675
if len(recipients) == 0 and not has_stream:
6776
click.echo("You must specify a stream/subject or at least one recipient.")
6877
raise SystemExit(1)
6978

70-
message_data: Dict[str, Any]
79+
# Normalize message type
80+
if message_type not in ("stream", "private", "direct"):
81+
click.echo("Invalid message type. Use 'stream', 'private', or 'direct'.")
82+
raise SystemExit(1)
83+
84+
# Determine type
7185
if has_stream:
72-
message_data = {
86+
message_data: Dict[str, Any] = {
7387
"type": "stream",
7488
"content": message,
7589
"subject": subject,
7690
"to": stream,
7791
}
7892
else:
93+
# Default to "direct" if explicitly given, else private
7994
message_data = {
80-
"type": "private",
95+
"type": "direct" if message_type == "direct" else "private",
8196
"content": message,
8297
"to": recipients,
8398
}
8499

100+
# Backward compatibility: convert "direct" → "private" if server doesn’t support feature level 174+
101+
if message_data["type"] == "direct":
102+
try:
103+
if client.server_feature_level() < 174:
104+
log.info(
105+
"Server does not support 'direct' message type; falling back to 'private'."
106+
)
107+
message_data["type"] = "private"
108+
except Exception:
109+
# Fallback: assume older server
110+
message_data["type"] = "private"
111+
85112
if message_data["type"] == "stream":
86113
log.info(
87114
"Sending message to stream %r, subject %r... ",
88115
message_data["to"],
89116
message_data["subject"],
90117
)
91118
else:
92-
log.info("Sending message to %s... ", message_data["to"])
119+
log.info("Sending %r message to %s... ", message_data["type"], message_data["to"])
120+
93121
response = client.send_message(message_data)
94122
log_exit(response)
95123

@@ -121,11 +149,6 @@ def delete_message(message_id: int) -> None:
121149
log_exit(response)
122150

123151

124-
# TODO
125-
# https://zulip.com/api/get-messages
126-
# https://zulip.com/api/construct-narrow
127-
128-
129152
@cli.command()
130153
@click.argument("message_id", type=int)
131154
@click.argument("emoji_name")
@@ -154,36 +177,21 @@ def remove_reaction(message_id: int, emoji_name: str) -> None:
154177
log_exit(response)
155178

156179

157-
# TODO
158-
# https://zulip.com/api/render-message
159-
# https://zulip.com/api/get-raw-message
160-
# https://zulip.com/api/check-narrow-matches
161-
162-
163180
@cli.command()
164181
@click.argument("message_id", type=int)
165182
def get_message_history(message_id: int) -> None:
166-
"""Fetch the message edit history of a previously edited message.
167-
Note that edit history may be disabled in some organizations; see https://zulip.com/help/view-a-messages-edit-history.
168-
"""
183+
"""Fetch the message edit history of a previously edited message."""
169184
response = client.get_message_history(message_id)
170185
log_exit(response)
171186

172187

173-
# TODO
174-
# https://zulip.com/api/update-message-flags
175-
176-
177188
@cli.command()
178189
def mark_all_as_read() -> None:
179190
"""Marks all of the current user's unread messages as read."""
180191
response = client.mark_all_as_read()
181192
log_exit(response)
182193

183194

184-
# Streams API
185-
186-
187195
@cli.command()
188196
def get_subscriptions() -> None:
189197
"""Get all streams that the user is subscribed to."""
@@ -192,4 +200,4 @@ def get_subscriptions() -> None:
192200

193201

194202
if __name__ == "__main__":
195-
cli()
203+
cli()

0 commit comments

Comments
 (0)