2020-06-11 00:54:34 +02:00
|
|
|
import datetime
|
2020-07-08 00:35:59 +02:00
|
|
|
from typing import Dict, List
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2018-04-17 17:16:02 +02:00
|
|
|
|
2020-07-08 02:12:43 +02:00
|
|
|
from zerver.lib.actions import get_active_presence_idle_user_ids, get_client
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.models import (
|
|
|
|
Message,
|
|
|
|
UserPresence,
|
|
|
|
UserProfile,
|
|
|
|
bulk_get_huddle_user_ids,
|
|
|
|
get_huddle_user_ids,
|
|
|
|
)
|
2016-03-24 20:24:01 +01:00
|
|
|
|
2017-08-05 22:10:34 +02:00
|
|
|
|
2017-09-05 20:50:25 +02:00
|
|
|
class MissedMessageTest(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_presence_idle_user_ids(self) -> None:
|
2017-09-05 20:50:25 +02:00
|
|
|
UserPresence.objects.all().delete()
|
|
|
|
|
|
|
|
sender = self.example_user('cordelia')
|
|
|
|
realm = sender.realm
|
|
|
|
hamlet = self.example_user('hamlet')
|
|
|
|
othello = self.example_user('othello')
|
2017-09-09 04:14:28 +02:00
|
|
|
recipient_ids = {hamlet.id, othello.id}
|
2017-09-05 20:50:25 +02:00
|
|
|
message_type = 'stream'
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
user_flags: Dict[int, List[str]] = {}
|
2017-09-05 20:50:25 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def assert_missing(user_ids: List[int]) -> None:
|
2017-10-07 17:59:19 +02:00
|
|
|
presence_idle_user_ids = get_active_presence_idle_user_ids(
|
2017-09-05 20:50:25 +02:00
|
|
|
realm=realm,
|
|
|
|
sender_id=sender.id,
|
|
|
|
message_type=message_type,
|
2017-09-09 04:14:28 +02:00
|
|
|
active_user_ids=recipient_ids,
|
2017-09-05 20:50:25 +02:00
|
|
|
user_flags=user_flags,
|
|
|
|
)
|
2017-10-07 17:59:19 +02:00
|
|
|
self.assertEqual(sorted(user_ids), sorted(presence_idle_user_ids))
|
2017-09-05 20:50:25 +02:00
|
|
|
|
2020-02-08 18:46:27 +01:00
|
|
|
def set_presence(user: UserProfile, client_name: str, ago: int) -> None:
|
2017-09-05 20:50:25 +02:00
|
|
|
when = timezone_now() - datetime.timedelta(seconds=ago)
|
|
|
|
UserPresence.objects.create(
|
2020-02-08 18:46:27 +01:00
|
|
|
user_profile_id=user.id,
|
|
|
|
realm_id=user.realm_id,
|
2017-09-05 20:50:25 +02:00
|
|
|
client=get_client(client_name),
|
|
|
|
timestamp=when,
|
|
|
|
)
|
|
|
|
|
|
|
|
message_type = 'private'
|
|
|
|
assert_missing([hamlet.id, othello.id])
|
|
|
|
|
|
|
|
message_type = 'stream'
|
|
|
|
user_flags[hamlet.id] = ['mentioned']
|
|
|
|
assert_missing([hamlet.id])
|
|
|
|
|
2020-02-08 18:46:27 +01:00
|
|
|
set_presence(hamlet, 'iPhone', ago=5000)
|
2017-09-05 20:50:25 +02:00
|
|
|
assert_missing([hamlet.id])
|
|
|
|
|
2020-02-08 18:46:27 +01:00
|
|
|
set_presence(hamlet, 'webapp', ago=15)
|
2017-09-05 20:50:25 +02:00
|
|
|
assert_missing([])
|
|
|
|
|
|
|
|
message_type = 'private'
|
|
|
|
assert_missing([othello.id])
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-08-13 23:05:47 +02:00
|
|
|
class TestBulkGetHuddleUserIds(ZulipTestCase):
|
|
|
|
def test_bulk_get_huddle_user_ids(self) -> None:
|
|
|
|
hamlet = self.example_user('hamlet')
|
|
|
|
cordelia = self.example_user('cordelia')
|
|
|
|
othello = self.example_user('othello')
|
|
|
|
iago = self.example_user('iago')
|
|
|
|
message_ids = [
|
2020-03-07 11:43:05 +01:00
|
|
|
self.send_huddle_message(hamlet, [cordelia, othello], 'test'),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
self.send_huddle_message(cordelia, [hamlet, othello, iago], 'test'),
|
2019-08-13 23:05:47 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
messages = Message.objects.filter(id__in=message_ids).order_by("id")
|
|
|
|
first_huddle_recipient = messages[0].recipient
|
|
|
|
first_huddle_user_ids = list(get_huddle_user_ids(first_huddle_recipient))
|
|
|
|
second_huddle_recipient = messages[1].recipient
|
|
|
|
second_huddle_user_ids = list(get_huddle_user_ids(second_huddle_recipient))
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
huddle_user_ids = bulk_get_huddle_user_ids(
|
|
|
|
[first_huddle_recipient, second_huddle_recipient]
|
|
|
|
)
|
2019-08-13 23:05:47 +02:00
|
|
|
self.assertEqual(huddle_user_ids[first_huddle_recipient.id], first_huddle_user_ids)
|
|
|
|
self.assertEqual(huddle_user_ids[second_huddle_recipient.id], second_huddle_user_ids)
|
|
|
|
|
|
|
|
def test_bulk_get_huddle_user_ids_empty_list(self) -> None:
|
|
|
|
self.assertEqual(bulk_get_huddle_user_ids([]), {})
|