hipchat: Rework stream/subscriber logic.

We now account for streams having users that
may be deleted.  We do a couple things:

    - use a loop instead of map
    - only pass in users to hipchat_subscriber
    - early-exit if there are not users
    - skip owner/members logic for public streams
This commit is contained in:
Steve Howell
2018-11-17 15:44:58 +00:00
committed by Tim Abbott
parent 1335dfd295
commit bd1e96cf63
2 changed files with 31 additions and 47 deletions

View File

@@ -207,7 +207,7 @@ def convert_room_data(raw_data: List[ZerverFieldsT],
for d in raw_data
]
def invite_only(v: str) -> bool:
def get_invite_only(v: str) -> bool:
if v == 'public':
return False
elif v == 'private':
@@ -215,45 +215,51 @@ def convert_room_data(raw_data: List[ZerverFieldsT],
else:
raise Exception('unexpected value')
def process(in_dict: ZerverFieldsT) -> ZerverFieldsT:
streams = []
for in_dict in flat_data:
now = int(timezone_now().timestamp())
stream_id = stream_id_mapper.get(in_dict['id'])
out_dict = build_stream(
invite_only = get_invite_only(in_dict['privacy'])
stream = build_stream(
date_created=now,
realm_id=realm_id,
name=in_dict['name'],
description=in_dict['topic'],
stream_id=stream_id,
deactivated=in_dict['is_archived'],
invite_only=invite_only(in_dict['privacy']),
invite_only=invite_only,
)
if not user_id_mapper.has(in_dict['owner']):
raise Exception('bad owner')
if invite_only:
users = {
user_id_mapper.get(key)
for key in in_dict['members']
if user_id_mapper.has(key)
} # type: Set[int]
owner = user_id_mapper.get(in_dict['owner'])
members = {
user_id_mapper.get(key)
for key in in_dict['members']
if user_id_mapper.has(key)
}
if user_id_mapper.has(in_dict['owner']):
owner = user_id_mapper.get(in_dict['owner'])
users.add(owner)
subscriber_handler.set_info(
stream_id=stream_id,
owner=owner,
members=members,
)
if not users:
continue
subscriber_handler.set_info(
stream_id=stream_id,
users=users,
)
# unmapped fields:
# guest_access_url: no Zulip equivalent
# created: we just use "now"
# members: no good sample data
# owners: no good sample data
# participants: no good sample data
return out_dict
return list(map(process, flat_data))
streams.append(stream)
return streams
def make_realm(realm_id: int) -> ZerverFieldsT:
NOW = float(timezone_now().timestamp())

View File

@@ -1,37 +1,15 @@
from typing import Any, Dict, Set
class SubscriberHandler:
'''
A note on ids here: we borrow Hipchat ids as Zulip
ids during the conversion phase. (They get re-mapped
during import, but that doesn't concern use here.)
So these are all synonymous:
HipChat room_id == Zulip stream_id
member ids = hipchat user ids = Zulip user_id
owner id = hipchat user id = Zulip user_id
In this class, when it's somewhat arbitrary whether
to call something a "room" or a "stream", we use
the Zulip naming.
'''
def __init__(self) -> None:
self.stream_info = dict() # type: Dict[int, Dict[str, Any]]
self.stream_info = dict() # type: Dict[int, Set[int]]
def set_info(self,
stream_id: int,
owner: int,
members: Set[int]) -> None:
# Our callers are basically giving us
# data straight out of rooms.json.
self.stream_info[stream_id] = dict(
owner=owner,
members=members,
)
users: Set[int]) -> None:
self.stream_info[stream_id] = users
def get_users(self,
stream_id: int) -> Set[int]:
info = self.stream_info[stream_id]
users = info['members'] | {info['owner']}
users = self.stream_info[stream_id]
return users