Skip to content

Commit

Permalink
refactor: api_types/model: Improve types for typing notifications.
Browse files Browse the repository at this point in the history
With the addition of stream typing notifications, and soon the migration to
"direct" in place of "private", message type can now be useful to include.

However, note that we don't yet explicitly specify the type for the
direct/private message form, since this was added only in ZFL 58, Zulip 4.0.
  • Loading branch information
neiljp committed Apr 22, 2023
1 parent 99157c2 commit 10e02d0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
37 changes: 31 additions & 6 deletions zulipterminal/api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

RESOLVED_TOPIC_PREFIX = "✔ "

# Refer to https://zulip.com/api/set-typing-status for the protocol
# on typing notifications sent by clients.
TYPING_STARTED_WAIT_PERIOD = 10
TYPING_STOPPED_WAIT_PERIOD = 5


###############################################################################
# These values are in the register response from ZFL 53
# Before this feature level, they had the listed default (fixed) values
Expand All @@ -42,6 +36,37 @@
MessageType = Union[DirectMessageString, StreamMessageString]


###############################################################################
# Parameters to pass in request to:
# https://zulip.com/api/set-typing-status
# Refer to the top of that page for the expected protocol clients should observe
#
# NOTE: `to` field could be email until ZFL 11/3.0; ids were possible from 2.0+

# Timing parameters for when notifications should occur
TYPING_STARTED_WAIT_PERIOD: Final = 10
TYPING_STOPPED_WAIT_PERIOD: Final = 5
TYPING_STARTED_EXPIRY_PERIOD: Final = 15 # TODO: Needs implementation in ZT

TypingStatusChange = Literal["start", "stop"]


class DirectTypingNotification(TypedDict):
# The type field was added in ZFL 58, Zulip 4.0, so don't require it yet
## type: DirectMessageString
op: TypingStatusChange
to: List[int]


# NOTE: Not yet implemented in ZT
# New in ZFL 58, Zulip 4.0
class StreamTypingNotification(TypedDict):
type: StreamMessageString
op: TypingStatusChange
to: List[int] # NOTE: Length 1, stream id
topic: str


###############################################################################
# Parameter to pass in request to:
# https://zulip.com/api/send-message
Expand Down
8 changes: 5 additions & 3 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@

import zulip
from bs4 import BeautifulSoup
from typing_extensions import Literal, TypedDict
from typing_extensions import TypedDict

from zulipterminal import unicode_emojis
from zulipterminal.api_types import (
MAX_MESSAGE_LENGTH,
MAX_STREAM_NAME_LENGTH,
MAX_TOPIC_NAME_LENGTH,
Composition,
DirectTypingNotification,
EditPropagateMode,
Event,
PrivateComposition,
Expand All @@ -44,6 +45,7 @@
StreamComposition,
StreamMessageUpdateRequest,
Subscription,
TypingStatusChange,
)
from zulipterminal.config.keys import primary_key_for_command
from zulipterminal.config.symbols import STREAM_TOPIC_SEPARATOR
Expand Down Expand Up @@ -513,12 +515,12 @@ def mark_message_ids_as_read(self, id_list: List[int]) -> None:

@asynch
def send_typing_status_by_user_ids(
self, recipient_user_ids: List[int], *, status: Literal["start", "stop"]
self, recipient_user_ids: List[int], *, status: TypingStatusChange
) -> None:
if not self.user_settings()["send_private_typing_notifications"]:
return
if recipient_user_ids:
request = {"to": recipient_user_ids, "op": status}
request: DirectTypingNotification = {"to": recipient_user_ids, "op": status}
response = self.client.set_typing_status(request)
display_error_if_present(response, self.controller)
else:
Expand Down

0 comments on commit 10e02d0

Please sign in to comment.