Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

gitter: Soft deactivate appropriate imported users.

Mateusz Mandera4c7a9816ff0c
diff --git a/zerver/data_import/gitter.py b/zerver/data_import/gitter.py
index 70e910778a5..5f93939e433 100644
--- a/zerver/data_import/gitter.py
+++ b/zerver/data_import/gitter.py
@@ -21,6 +21,7 @@ from zerver.data_import.import_util import (
build_usermessages,
build_zerver_realm,
create_converted_data_files,
+ long_term_idle_helper,
make_subscriber_map,
process_avatars,
)
@@ -216,12 +217,23 @@ def convert_gitter_workspace_messages(
user_map: Dict[str, int],
stream_map: Dict[str, int],
user_short_name_to_full_name: Dict[str, str],
+ zerver_userprofile: List[ZerverFieldsT],
chunk_size: int = MESSAGE_BATCH_CHUNK_SIZE,
) -> None:
"""
Messages are stored in batches
"""
logging.info("######### IMPORTING MESSAGES STARTED #########\n")
+
+ long_term_idle = long_term_idle_helper(
+ iter(gitter_data),
+ get_user_from_message,
+ get_timestamp_from_message,
+ lambda id: user_map[id],
+ list(user_map.keys()),
+ zerver_userprofile,
+ )
+
message_id = 0
low_index = 0
@@ -262,6 +274,7 @@ def convert_gitter_workspace_messages(
mentioned_user_ids=mentioned_user_ids,
message_id=message_id,
is_private=False,
+ long_term_idle=long_term_idle,
)
message_id += 1
@@ -330,7 +343,13 @@ def do_convert_data(gitter_data_file: str, output_dir: str, threads: int = 6) ->
user_short_name_to_full_name[userprofile["short_name"]] = userprofile["full_name"]
convert_gitter_workspace_messages(
- gitter_data, output_dir, subscriber_map, user_map, stream_map, user_short_name_to_full_name
+ gitter_data,
+ output_dir,
+ subscriber_map,
+ user_map,
+ stream_map,
+ user_short_name_to_full_name,
+ realm["zerver_userprofile"],
)
avatar_folder = os.path.join(output_dir, "avatars")
diff --git a/zerver/tests/fixtures/gitter_data.json b/zerver/tests/fixtures/gitter_data.json
index fe4d7a66f75..d7c5a10d33a 100644
--- a/zerver/tests/fixtures/gitter_data.json
+++ b/zerver/tests/fixtures/gitter_data.json
@@ -14,7 +14,7 @@
"issues": [],
"meta": [],
"readBy": 8,
- "sent": "2016-06-02T20:54:38.747Z",
+ "sent": "2015-06-02T20:54:38.747Z",
"text": "test message",
"unread": false,
"urls": [],
diff --git a/zerver/tests/test_gitter_importer.py b/zerver/tests/test_gitter_importer.py
index 9cf9243eb14..4f51b8ebec6 100644
--- a/zerver/tests/test_gitter_importer.py
+++ b/zerver/tests/test_gitter_importer.py
@@ -2,6 +2,7 @@ import os
from typing import Any
from unittest import mock
+import dateutil.parser
import orjson
from zerver.data_import.gitter import do_convert_data, get_usermentions
@@ -15,7 +16,19 @@ class GitterImporter(ZulipTestCase):
def test_gitter_import_data_conversion(self, mock_process_avatars: mock.Mock) -> None:
output_dir = self.make_import_output_dir("gitter")
gitter_file = os.path.join(os.path.dirname(__file__), "fixtures/gitter_data.json")
- with self.assertLogs(level="INFO"):
+
+ # We need some time-mocking to set up user soft-deactivation logic.
+ # One of the messages in the import data
+ # is significantly older than the other one. We mock the current time in the relevant module
+ # to match the sent time of the more recent message - to make it look like one of the messages
+ # is very recent, while the other one is old. This should cause that the sender of the recent
+ # message to NOT be soft-deactivated, while the sender of the other one is.
+ with open(gitter_file) as f:
+ gitter_data = orjson.loads(f.read())
+ sent_datetime = dateutil.parser.parse(gitter_data[1]["sent"])
+ with self.assertLogs(level="INFO"), mock.patch(
+ "zerver.data_import.import_util.timezone_now", return_value=sent_datetime
+ ):
do_convert_data(gitter_file, output_dir)
def read_file(output_file: str) -> Any:
@@ -73,11 +86,26 @@ class GitterImporter(ZulipTestCase):
self.assertIn(messages["zerver_message"][1]["recipient"], exported_recipient_id)
self.assertIn(messages["zerver_message"][0]["content"], "test message")
- # test usermessages
+ # test usermessages and soft-deactivation of users
+ user_should_be_long_term_idle = [
+ user
+ for user in realm["zerver_userprofile"]
+ if user["delivery_email"] == "[email protected]"
+ ][0]
+ user_should_not_be_long_term_idle = [
+ user
+ for user in realm["zerver_userprofile"]
+ if user["delivery_email"] == "[email protected]"
+ ][0]
+ self.assertEqual(user_should_be_long_term_idle["long_term_idle"], True)
+
+ # Only the user who's not soft-deactivated gets UserMessages.
exported_usermessage_userprofile = self.get_set(
messages["zerver_usermessage"], "user_profile"
)
- self.assertEqual(exported_user_ids, exported_usermessage_userprofile)
+ self.assertEqual(
+ set([user_should_not_be_long_term_idle["id"]]), exported_usermessage_userprofile
+ )
exported_usermessage_message = self.get_set(messages["zerver_usermessage"], "message")
self.assertEqual(exported_usermessage_message, exported_messages_id)