Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Clarify draft types & naming #924

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions zulipterminal/api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
EditPropagateMode = Literal['change_one', 'change_all', 'change_later']


class PrivateComposition(TypedDict):
type: Literal['private']
content: str
to: List[str] # emails # TODO: Migrate to using List[int] (user ids)


class StreamComposition(TypedDict):
type: Literal['stream']
content: str
to: str # stream name # TODO: Migrate to using int (stream id)
subject: str # TODO: Migrate to using topic
stream_id: int # FIXME: Not type of API; use until migrate to stream id


Composition = Union[PrivateComposition, StreamComposition]


class Message(TypedDict, total=False):
id: int
sender_id: int
Expand Down
6 changes: 3 additions & 3 deletions zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import zulip
from typing_extensions import Literal

from zulipterminal.api_types import Message
from zulipterminal.api_types import Composition, Message
from zulipterminal.config.themes import ThemeSpec
from zulipterminal.helper import asynch
from zulipterminal.model import Model
Expand Down Expand Up @@ -262,10 +262,10 @@ def search_messages(self, text: str) -> None:
if 0 <= focus_position < len(w_list):
self.view.message_view.set_focus(focus_position)

def save_draft_confirmation_popup(self, message: Message) -> None:
def save_draft_confirmation_popup(self, draft: Composition) -> None:
question = urwid.Text('Save this message as a draft?'
' (This will overwrite the existing draft.)')
save_draft = partial(self.model.save_draft, message)
save_draft = partial(self.model.save_draft, draft)
self.loop.widget = PopUpConfirmationView(self, question, save_draft)

def stream_muting_confirmation_popup(self, button: Any) -> None:
Expand Down
10 changes: 5 additions & 5 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from typing_extensions import Literal

from zulipterminal import unicode_emojis
from zulipterminal.api_types import EditPropagateMode, Event
from zulipterminal.api_types import Composition, EditPropagateMode, Event
from zulipterminal.config.keys import primary_key_for_command
from zulipterminal.helper import (
Message,
Expand Down Expand Up @@ -127,7 +127,7 @@ def __init__(self, controller: Any) -> None:

self.unread_counts = classify_unread_counts(self)

self._draft: Optional[Message] = None
self._draft: Optional[Composition] = None
unicode_emoji_data = unicode_emojis.EMOJI_DATA
for name, data in unicode_emoji_data.items():
data['type'] = 'unicode_emoji'
Expand Down Expand Up @@ -333,11 +333,11 @@ def react_to_message(self,
response = self.client.add_reaction(reaction_to_toggle_spec)
display_error_if_present(response, self.controller)

def session_draft_message(self) -> Optional[Message]:
def session_draft_message(self) -> Optional[Composition]:
return deepcopy(self._draft)

def save_draft(self, message: Message) -> None:
self._draft = deepcopy(message)
def save_draft(self, draft: Composition) -> None:
self._draft = deepcopy(draft)
self.controller.view.set_footer_text("Saved message as draft", 3)

@asynch
Expand Down
4 changes: 2 additions & 2 deletions zulipterminal/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ def keypress(self, size: Tuple[int, int], key: str) -> Optional[str]:
if saved_draft:
if saved_draft['type'] == 'stream':
self.write_box.stream_box_view(
caption=saved_draft['display_recipient'],
caption=saved_draft['to'],
title=saved_draft['subject'],
stream_id=saved_draft['stream_id'],
)
elif saved_draft['type'] == 'private':
email_list = saved_draft['display_recipient']
email_list = saved_draft['to']
recipient_user_ids = [
self.model.user_dict[email.strip()]['user_id']
for email in email_list
Expand Down
29 changes: 18 additions & 11 deletions zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
from tzlocal import get_localzone
from urwid_readline import ReadlineEdit

from zulipterminal.api_types import (
Composition,
PrivateComposition,
StreamComposition,
)
from zulipterminal.config.keys import (
is_command_key,
keys_for_command,
Expand Down Expand Up @@ -543,24 +548,26 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
if not self.msg_edit_id:
if self.to_write_box:
self.update_recipient_emails(self.to_write_box)
message = Message(
display_recipient=self.recipient_emails,
content=self.msg_write_box.edit_text,
type='private',
this_draft: Composition = PrivateComposition(
type="private",
to=self.recipient_emails,
content=self.msg_write_box.edit_text,
)
elif self.stream_id:
message = Message(
display_recipient=self.stream_write_box.edit_text,
this_draft = StreamComposition(
type="stream",
to=self.stream_write_box.edit_text,
content=self.msg_write_box.edit_text,
subject=self.title_write_box.edit_text,
stream_id=self.stream_id,
type='stream',
stream_id=self.stream_id, # FIXME Migrate to ids
)
saved_draft = self.model.session_draft_message()
if not saved_draft:
self.model.save_draft(message)
elif message != saved_draft:
self.view.controller.save_draft_confirmation_popup(message)
self.model.save_draft(this_draft)
elif this_draft != saved_draft:
self.view.controller.save_draft_confirmation_popup(
this_draft,
)
elif is_command_key('CYCLE_COMPOSE_FOCUS', key):
if len(self.contents) == 0:
return key
Expand Down