2023-02-04 09:16:07 +01:00
|
|
|
from datetime import datetime, timezone
|
2021-12-17 08:14:22 +01:00
|
|
|
from typing import Any, Dict, List
|
2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2017-03-08 12:46:05 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-10-23 19:39:12 +02:00
|
|
|
|
2023-02-03 13:21:25 +01:00
|
|
|
from zerver.actions.user_topics import do_set_user_topic_visibility_policy
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.stream_topic import StreamTopicTarget
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2022-02-22 21:07:07 +01:00
|
|
|
from zerver.lib.user_topics import (
|
2017-08-30 02:19:34 +02:00
|
|
|
get_topic_mutes,
|
|
|
|
topic_is_muted,
|
|
|
|
)
|
2021-07-23 15:26:02 +02:00
|
|
|
from zerver.models import UserProfile, UserTopic, get_stream
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-03-08 12:46:05 +01:00
|
|
|
|
|
|
|
class MutedTopicsTests(ZulipTestCase):
|
2021-02-16 02:26:56 +01:00
|
|
|
def test_get_deactivated_muted_topic(self) -> None:
|
|
|
|
user = self.example_user("hamlet")
|
|
|
|
self.login_user(user)
|
|
|
|
|
|
|
|
stream = get_stream("Verona", user.realm)
|
|
|
|
|
|
|
|
mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()
|
|
|
|
|
2023-02-03 12:57:43 +01:00
|
|
|
do_set_user_topic_visibility_policy(
|
2023-03-03 18:00:27 +01:00
|
|
|
user,
|
|
|
|
stream,
|
|
|
|
"Verona3",
|
2023-02-03 12:57:43 +01:00
|
|
|
visibility_policy=UserTopic.MUTED,
|
|
|
|
last_updated=datetime(2020, 1, 1, tzinfo=timezone.utc),
|
2021-02-16 02:26:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
stream.deactivated = True
|
|
|
|
stream.save()
|
|
|
|
|
|
|
|
self.assertNotIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user))
|
|
|
|
self.assertIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user, True))
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_user_ids_muting_topic(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
cordelia = self.example_user("cordelia")
|
2017-10-23 19:39:12 +02:00
|
|
|
realm = hamlet.realm
|
2021-02-12 08:20:45 +01:00
|
|
|
stream = get_stream("Verona", realm)
|
|
|
|
topic_name = "teST topic"
|
2023-02-04 09:16:07 +01:00
|
|
|
date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc)
|
2017-10-23 19:39:12 +02:00
|
|
|
|
|
|
|
stream_topic_target = StreamTopicTarget(
|
|
|
|
stream_id=stream.id,
|
|
|
|
topic_name=topic_name,
|
|
|
|
)
|
|
|
|
|
2022-09-12 14:14:24 +02:00
|
|
|
user_ids = stream_topic_target.user_ids_with_visibility_policy(UserTopic.MUTED)
|
2017-10-23 19:39:12 +02:00
|
|
|
self.assertEqual(user_ids, set())
|
|
|
|
|
2021-03-27 11:58:03 +01:00
|
|
|
def mute_topic_for_user(user: UserProfile) -> None:
|
2023-02-03 12:57:43 +01:00
|
|
|
do_set_user_topic_visibility_policy(
|
2023-03-03 18:00:27 +01:00
|
|
|
user,
|
|
|
|
stream,
|
|
|
|
"test TOPIC",
|
2023-02-03 12:57:43 +01:00
|
|
|
visibility_policy=UserTopic.MUTED,
|
2023-02-04 09:16:07 +01:00
|
|
|
last_updated=date_muted,
|
2017-10-23 19:39:12 +02:00
|
|
|
)
|
|
|
|
|
2021-03-27 11:58:03 +01:00
|
|
|
mute_topic_for_user(hamlet)
|
2022-09-12 14:14:24 +02:00
|
|
|
user_ids = stream_topic_target.user_ids_with_visibility_policy(UserTopic.MUTED)
|
2017-10-23 19:39:12 +02:00
|
|
|
self.assertEqual(user_ids, {hamlet.id})
|
2021-08-02 09:49:56 +02:00
|
|
|
hamlet_date_muted = UserTopic.objects.filter(
|
|
|
|
user_profile=hamlet, visibility_policy=UserTopic.MUTED
|
|
|
|
)[0].last_updated
|
2023-02-04 09:16:07 +01:00
|
|
|
self.assertEqual(hamlet_date_muted, date_muted)
|
2017-10-23 19:39:12 +02:00
|
|
|
|
2021-03-27 11:58:03 +01:00
|
|
|
mute_topic_for_user(cordelia)
|
2022-09-12 14:14:24 +02:00
|
|
|
user_ids = stream_topic_target.user_ids_with_visibility_policy(UserTopic.MUTED)
|
2017-10-23 19:39:12 +02:00
|
|
|
self.assertEqual(user_ids, {hamlet.id, cordelia.id})
|
2021-08-02 09:49:56 +02:00
|
|
|
cordelia_date_muted = UserTopic.objects.filter(
|
|
|
|
user_profile=cordelia, visibility_policy=UserTopic.MUTED
|
|
|
|
)[0].last_updated
|
2023-02-04 09:16:07 +01:00
|
|
|
self.assertEqual(cordelia_date_muted, date_muted)
|
2017-10-23 19:39:12 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_add_muted_topic(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2018-12-24 17:04:27 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
stream = get_stream("Verona", user.realm)
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
url = "/api/v1/users/me/subscriptions/muted_topics"
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2021-12-17 08:14:22 +01:00
|
|
|
payloads: List[Dict[str, object]] = [
|
2021-02-12 08:20:45 +01:00
|
|
|
{"stream": stream.name, "topic": "Verona3", "op": "add"},
|
|
|
|
{"stream_id": stream.id, "topic": "Verona3", "op": "add"},
|
2018-12-24 17:04:27 +01:00
|
|
|
]
|
|
|
|
|
2020-06-04 03:32:59 +02:00
|
|
|
mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()
|
2018-12-24 17:04:27 +01:00
|
|
|
for data in payloads:
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2023-02-03 11:07:35 +01:00
|
|
|
"zerver.views.user_topics.timezone_now",
|
2021-02-12 08:19:30 +01:00
|
|
|
return_value=datetime(2020, 1, 1, tzinfo=timezone.utc),
|
|
|
|
):
|
2020-02-05 09:03:11 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
|
|
|
self.assert_json_success(result)
|
2018-12-24 17:04:27 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user))
|
|
|
|
self.assertTrue(topic_is_muted(user, stream.id, "verona3"))
|
2018-12-24 17:04:27 +01:00
|
|
|
|
2023-02-03 13:21:25 +01:00
|
|
|
do_set_user_topic_visibility_policy(
|
2023-03-03 18:00:27 +01:00
|
|
|
user,
|
|
|
|
stream,
|
|
|
|
"Verona3",
|
2023-02-03 13:21:25 +01:00
|
|
|
visibility_policy=UserTopic.VISIBILITY_POLICY_INHERIT,
|
2018-12-24 17:04:27 +01:00
|
|
|
)
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2022-05-31 01:27:38 +02:00
|
|
|
assert stream.recipient is not None
|
2023-02-10 20:06:39 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2022-02-09 16:49:46 +01:00
|
|
|
|
2023-02-10 20:06:39 +01:00
|
|
|
# Now check that error is raised when attempted to mute an already
|
|
|
|
# muted topic. This should be case-insensitive.
|
|
|
|
data["topic"] = "VERONA3"
|
|
|
|
result = self.api_patch(user, url, data)
|
|
|
|
self.assert_json_error(result, "Topic already muted")
|
2022-02-09 16:49:46 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_remove_muted_topic(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2018-12-24 17:04:27 +01:00
|
|
|
realm = user.realm
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
stream = get_stream("Verona", realm)
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
url = "/api/v1/users/me/subscriptions/muted_topics"
|
2021-12-17 08:14:22 +01:00
|
|
|
payloads: List[Dict[str, object]] = [
|
2021-02-12 08:20:45 +01:00
|
|
|
{"stream": stream.name, "topic": "vERONA3", "op": "remove"},
|
|
|
|
{"stream_id": stream.id, "topic": "vEroNA3", "op": "remove"},
|
2018-12-24 17:04:27 +01:00
|
|
|
]
|
2020-06-04 03:32:59 +02:00
|
|
|
mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
for data in payloads:
|
2023-02-03 12:57:43 +01:00
|
|
|
do_set_user_topic_visibility_policy(
|
2023-03-03 18:00:27 +01:00
|
|
|
user,
|
|
|
|
stream,
|
|
|
|
"Verona3",
|
2023-02-03 12:57:43 +01:00
|
|
|
visibility_policy=UserTopic.MUTED,
|
|
|
|
last_updated=datetime(2020, 1, 1, tzinfo=timezone.utc),
|
2018-12-24 17:04:27 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user))
|
2018-12-24 17:04:27 +01:00
|
|
|
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertNotIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user))
|
|
|
|
self.assertFalse(topic_is_muted(user, stream.id, "verona3"))
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_muted_topic_add_invalid(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2018-12-24 17:04:27 +01:00
|
|
|
realm = user.realm
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
stream = get_stream("Verona", realm)
|
2023-02-03 12:57:43 +01:00
|
|
|
do_set_user_topic_visibility_policy(
|
|
|
|
user, stream, "Verona3", visibility_policy=UserTopic.MUTED, last_updated=timezone_now()
|
|
|
|
)
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
url = "/api/v1/users/me/subscriptions/muted_topics"
|
2018-12-24 17:04:27 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"stream_id": 999999999, "topic": "Verona3", "op": "add"}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2022-05-27 14:03:08 +02:00
|
|
|
self.assert_json_error(result, "Invalid stream ID")
|
2018-12-24 17:04:27 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"topic": "Verona3", "op": "add"}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Please supply 'stream'.")
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"stream": stream.name, "stream_id": stream.id, "topic": "Verona3", "op": "add"}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Please choose one: 'stream' or 'stream_id'.")
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_muted_topic_remove_invalid(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2018-12-24 17:04:27 +01:00
|
|
|
realm = user.realm
|
2020-03-06 18:40:46 +01:00
|
|
|
self.login_user(user)
|
2021-02-12 08:20:45 +01:00
|
|
|
stream = get_stream("Verona", realm)
|
2017-03-13 22:07:00 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
url = "/api/v1/users/me/subscriptions/muted_topics"
|
|
|
|
data: Dict[str, Any] = {"stream": "BOGUS", "topic": "Verona3", "op": "remove"}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Topic is not muted")
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"stream": stream.name, "topic": "BOGUS", "op": "remove"}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-08-27 22:13:56 +02:00
|
|
|
self.assert_json_error(result, "Topic is not muted")
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"stream_id": 999999999, "topic": "BOGUS", "op": "remove"}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-08-27 22:13:56 +02:00
|
|
|
self.assert_json_error(result, "Topic is not muted")
|
2018-12-24 17:04:27 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"topic": "Verona3", "op": "remove"}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Please supply 'stream'.")
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data = {"stream": stream.name, "stream_id": stream.id, "topic": "Verona3", "op": "remove"}
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.api_patch(user, url, data)
|
2018-12-24 17:04:27 +01:00
|
|
|
self.assert_json_error(result, "Please choose one: 'stream' or 'stream_id'.")
|