hipchat import: Fix PM messages.

Before this fix, we were creating two copies of every
PM Message in zerver_message with only corresponding
UserMessage row.

Now we only create one PM Message per message, which
we accomplish by making sure we only use imported
messages from the sender's history.json file.  And
then we write UserMessage rows for both participants
by making sure to include sender_id in the set of
user_ids that feeds into making UserMessage.  For
the case where you PM yourself, there's just one
UserMessage row.

It does not appear that we need to support huddles
yet.
This commit is contained in:
Steve Howell
2018-10-22 18:56:41 +00:00
committed by showell
parent 53436b4b41
commit 737e02a2e6

View File

@@ -444,7 +444,7 @@ def process_message_file(realm_id: int,
if message_key in d
]
def get_raw_message(d: Dict[str, Any]) -> ZerverFieldsT:
def get_raw_message(d: Dict[str, Any]) -> Optional[ZerverFieldsT]:
if isinstance(d['sender'], str):
# Some Hipchat instances just give us a person's
# name in the sender field for NotificationMessage.
@@ -457,6 +457,12 @@ def process_message_file(realm_id: int,
else:
sender_id = d['sender']['id']
if message_key == 'PrivateUserMessage':
if sender_id != fn_id:
# PMs are in multiple places in the Hipchat export,
# and we only use the copy from the sender
return None
return dict(
fn_id=fn_id,
sender_id=sender_id,
@@ -472,7 +478,8 @@ def process_message_file(realm_id: int,
for d in flat_data:
raw_message = get_raw_message(d)
raw_messages.append(raw_message)
if raw_message is not None:
raw_messages.append(raw_message)
return raw_messages
@@ -598,8 +605,11 @@ def make_user_messages(zerver_message: List[ZerverFieldsT],
for message in zerver_message:
message_id = message['id']
recipient_id = message['recipient']
sender_id = message['sender']
mention_user_ids = mention_map[message_id]
user_ids = subscriber_map.get(recipient_id, set())
user_ids.add(sender_id)
for user_id in user_ids:
is_mentioned = user_id in mention_user_ids
user_message = build_user_message(