models: Move Message.sent_by_human to Client.default_read_by_sender.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-12-13 22:08:43 -08:00 committed by Tim Abbott
parent d893ff5ba8
commit d7d5b6c73e
4 changed files with 34 additions and 36 deletions

View File

@ -775,7 +775,7 @@ def create_user_messages(
# automatically marked as read for yourself; scheduled
# messages to yourself only are not.
user_profile_id == sender_id
and message.sent_by_human()
and message.sending_client.default_read_by_sender()
and not scheduled_message_to_self
)
or user_profile_id in mark_as_read_user_ids

View File

@ -2968,6 +2968,37 @@ class Client(models.Model):
def __str__(self) -> str:
return self.name
def default_read_by_sender(self) -> bool:
"""Used to determine whether a message was sent by a full Zulip UI
style client (and thus whether the message should be treated
as sent by a human and automatically marked as read for the
sender). The purpose of this distinction is to ensure that
message sent to the user by e.g. a Google Calendar integration
using the user's own API key don't get marked as read
automatically.
"""
sending_client = self.name.lower()
return (
sending_client
in (
"zulipandroid",
"zulipios",
"zulipdesktop",
"zulipmobile",
"zulipelectron",
"zulipterminal",
"snipe",
"website",
"ios",
"android",
)
or "desktop app" in sending_client
# Since the vast majority of messages are sent by humans
# in Zulip, treat test suite messages as such.
or (sending_client == "test suite" and settings.TEST_SUITE)
)
get_client_cache: Dict[str, Client] = {}
@ -3235,39 +3266,6 @@ class Message(AbstractMessage):
or rendered_content_version < markdown_version
)
def sent_by_human(self) -> bool:
"""Used to determine whether a message was sent by a full Zulip UI
style client (and thus whether the message should be treated
as sent by a human and automatically marked as read for the
sender). The purpose of this distinction is to ensure that
message sent to the user by e.g. a Google Calendar integration
using the user's own API key don't get marked as read
automatically.
"""
sending_client = self.sending_client.name.lower()
return (
(
sending_client
in (
"zulipandroid",
"zulipios",
"zulipdesktop",
"zulipmobile",
"zulipelectron",
"zulipterminal",
"snipe",
"website",
"ios",
"android",
)
)
or ("desktop app" in sending_client)
# Since the vast majority of messages are sent by humans
# in Zulip, treat test suite messages as such.
or (sending_client == "test suite" and settings.TEST_SUITE)
)
@staticmethod
def is_status_message(content: str, rendered_content: str) -> bool:
"""

View File

@ -547,7 +547,7 @@ class TestDigestEmailMessages(ZulipTestCase):
self.assertEqual(stream_info["html"], [])
def simulate_stream_conversation(self, stream: str, senders: List[str]) -> List[int]:
client = "website" # this makes `sent_by_human` return True
client = "website" # this makes `default_read_by_sender` return True
sending_client = get_client(client)
message_ids = [] # List[int]
for sender_name in senders:

View File

@ -880,7 +880,7 @@ class GetUnreadMsgsTest(ZulipTestCase):
# Check our test setup is correct--the message should
# not have looked like it was sent by a human.
message = Message.objects.get(id=message_id)
self.assertFalse(message.sent_by_human())
self.assertFalse(message.sending_client.default_read_by_sender())
# And since it was not sent by a human, it should not
# be read, not even by the sender (Hamlet).