Example #1
0
 def test_build_defaultstream(self) -> None:
     realm_id = 1
     stream_id = 1
     default_channel_general = build_defaultstream(realm_id, stream_id, 1)
     test_default_channel = {"stream": 1, "realm": 1, "id": 1}
     self.assertDictEqual(test_default_channel, default_channel_general)
     default_channel_general = build_defaultstream(realm_id, stream_id, 1)
     test_default_channel = {"stream": 1, "realm": 1, "id": 1}
     self.assertDictEqual(test_default_channel, default_channel_general)
 def test_build_defaultstream(self) -> None:
     realm_id = 1
     stream_id = 1
     default_channel_general = build_defaultstream(realm_id, stream_id, 1)
     test_default_channel = {'stream': 1, 'realm': 1, 'id': 1}
     self.assertDictEqual(test_default_channel, default_channel_general)
     default_channel_general = build_defaultstream(realm_id, stream_id, 1)
     test_default_channel = {'stream': 1, 'realm': 1, 'id': 1}
     self.assertDictEqual(test_default_channel, default_channel_general)
Example #3
0
 def test_build_defaultstream(self) -> None:
     realm_id = 1
     stream_id = 1
     default_channel_general = build_defaultstream(realm_id, stream_id, 1)
     test_default_channel = {'stream': 1, 'realm': 1, 'id': 1}
     self.assertDictEqual(test_default_channel, default_channel_general)
     default_channel_general = build_defaultstream(realm_id, stream_id, 1)
     test_default_channel = {'stream': 1, 'realm': 1, 'id': 1}
     self.assertDictEqual(test_default_channel, default_channel_general)
Example #4
0
def build_stream_map(
    timestamp: Any, gitter_data: GitterDataT
) -> Tuple[List[ZerverFieldsT], List[ZerverFieldsT], Dict[str, int]]:
    """
    Returns:
    1. stream, which is the list of streams
    2. defaultstreams, which is the list of default streams
    3. stream_map, which is a dictionary to map from Gitter rooms to Zulip stream id
    """
    logging.info('######### IMPORTING STREAM STARTED #########\n')
    stream_id = 0
    stream: List[ZerverFieldsT] = []

    # Default stream when no "room" field is present
    stream.append(
        build_stream(timestamp, realm_id, 'from gitter',
                     "Imported from Gitter", stream_id))
    defaultstream = build_defaultstream(realm_id=realm_id,
                                        stream_id=stream_id,
                                        defaultstream_id=0)
    stream_id += 1

    # Gathering streams from gitter_data
    stream_map: Dict[str, int] = {}
    for data in gitter_data:
        if 'room' in data and data['room'] not in stream_map:
            stream.append(
                build_stream(timestamp, realm_id, data['room'],
                             f'Gitter room {data["room"]}', stream_id))
            stream_map[data['room']] = stream_id
            stream_id += 1
    logging.info('######### IMPORTING STREAMS FINISHED #########\n')

    return stream, [defaultstream], stream_map
Example #5
0
    def process_channels(channels: List[Dict[str, Any]],
                         invite_only: bool = False) -> None:
        nonlocal stream_id_count
        nonlocal recipient_id_count
        nonlocal defaultstream_id
        nonlocal subscription_id_count

        for channel in channels:
            # slack_channel_id = channel['id']

            # map Slack's topic and purpose content into Zulip's stream description.
            # WARN This mapping is lossy since the topic.creator, topic.last_set,
            # purpose.creator, purpose.last_set fields are not preserved.
            description = channel["purpose"]["value"]
            stream_id = stream_id_count
            recipient_id = recipient_id_count

            # construct the stream object and append it to zerver_stream
            stream = build_stream(float(channel["created"]), realm_id,
                                  channel["name"], description, stream_id,
                                  channel["is_archived"], invite_only)
            zerver_stream.append(stream)

            # construct defaultstream object
            # slack has the default channel 'general' and 'random'
            # where every user is subscribed
            default_channels = ['general', 'random']  # Slack specific
            if channel['name'] in default_channels:
                defaultstream = build_defaultstream(realm_id, stream_id,
                                                    defaultstream_id)
                zerver_defaultstream.append(defaultstream)
                defaultstream_id += 1

            added_channels[stream['name']] = (channel['id'], stream_id)

            recipient = build_recipient(stream_id, recipient_id,
                                        Recipient.STREAM)
            zerver_recipient.append(recipient)
            added_recipient[stream['name']] = recipient_id
            # TODO add recipients for private message and huddles

            # construct the subscription object and append it to zerver_subscription
            subscription_id_count = get_subscription(channel['members'],
                                                     zerver_subscription,
                                                     recipient_id, added_users,
                                                     subscription_id_count)
            # TODO add zerver_subscription which correspond to
            # huddles type recipient
            # For huddles:
            # sub['recipient']=recipient['id'] where recipient['type_id']=added_users[member]

            stream_id_count += 1
            recipient_id_count += 1
            logging.info(u"{} -> created".format(channel['name']))
Example #6
0
def build_stream_and_defaultstream(timestamp: Any) -> Tuple[List[ZerverFieldsT],
                                                            List[ZerverFieldsT]]:
    logging.info('######### IMPORTING STREAM STARTED #########\n')
    # We have only one stream for gitter export
    stream_name = 'from gitter'
    stream_description = "Imported from gitter"
    stream_id = 0
    stream = build_stream(timestamp, realm_id, stream_name, stream_description,
                          stream_id)

    defaultstream = build_defaultstream(realm_id=realm_id, stream_id=stream_id,
                                        defaultstream_id=0)
    logging.info('######### IMPORTING STREAMS FINISHED #########\n')
    return [stream], [defaultstream]
Example #7
0
    def process_channels(channels: List[Dict[str, Any]], invite_only: bool = False) -> None:
        nonlocal stream_id_count
        nonlocal recipient_id_count
        nonlocal defaultstream_id
        nonlocal subscription_id_count

        for channel in channels:
            # map Slack's topic and purpose content into Zulip's stream description.
            # WARN This mapping is lossy since the topic.creator, topic.last_set,
            # purpose.creator, purpose.last_set fields are not preserved.
            description = channel["purpose"]["value"]
            stream_id = stream_id_count
            recipient_id = recipient_id_count

            stream = build_stream(
                float(channel["created"]),
                realm_id,
                channel["name"],
                description,
                stream_id,
                channel["is_archived"],
                invite_only,
            )
            realm["zerver_stream"].append(stream)

            slack_default_channels = ["general", "random"]
            if channel["name"] in slack_default_channels and not stream["deactivated"]:
                defaultstream = build_defaultstream(realm_id, stream_id, defaultstream_id)
                realm["zerver_defaultstream"].append(defaultstream)
                defaultstream_id += 1

            added_channels[stream["name"]] = (channel["id"], stream_id)

            recipient = build_recipient(stream_id, recipient_id, Recipient.STREAM)
            realm["zerver_recipient"].append(recipient)
            slack_recipient_name_to_zulip_recipient_id[stream["name"]] = recipient_id

            subscription_id_count = get_subscription(
                channel["members"],
                realm["zerver_subscription"],
                recipient_id,
                slack_user_id_to_zulip_user_id,
                subscription_id_count,
            )

            stream_id_count += 1
            recipient_id_count += 1
            logging.info("%s -> created", channel["name"])
Example #8
0
def build_stream(
        timestamp: Any) -> Tuple[List[ZerverFieldsT], List[ZerverFieldsT]]:
    logging.info('######### IMPORTING STREAM STARTED #########\n')
    # We have only one stream for gitter export
    stream = dict(realm=realm_id,
                  name="from gitter",
                  deactivated=False,
                  description="Imported from gitter",
                  invite_only=False,
                  date_created=timestamp,
                  id=0)

    defaultstream = build_defaultstream(realm_id=realm_id,
                                        stream_id=0,
                                        defaultstream_id=0)
    logging.info('######### IMPORTING STREAMS FINISHED #########\n')
    return [stream], [defaultstream]
Example #9
0
File: slack.py Project: kyoki/zulip
def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: AddedUsersT,
                              zerver_userprofile: List[ZerverFieldsT]) -> Tuple[List[ZerverFieldsT],
                                                                                List[ZerverFieldsT],
                                                                                AddedChannelsT,
                                                                                List[ZerverFieldsT],
                                                                                List[ZerverFieldsT],
                                                                                AddedRecipientsT]:
    """
    Returns:
    1. zerver_defaultstream, which is a list of the default streams
    2. zerver_stream, while is a list of all streams
    3. added_channels, which is a dictionary to map from channel name to channel id, zulip stream_id
    4. zerver_subscription, which is a list of the subscriptions
    5. zerver_recipient, which is a list of the recipients
    6. added_recipient, which is a dictionary to map from channel name to zulip recipient_id
    """
    logging.info('######### IMPORTING CHANNELS STARTED #########\n')
    channels = get_data_file(slack_data_dir + '/channels.json')

    added_channels = {}
    added_recipient = {}

    zerver_stream = []
    zerver_subscription = []  # type: List[ZerverFieldsT]
    zerver_recipient = []
    zerver_defaultstream = []

    stream_id_count = subscription_id_count = recipient_id_count = defaultstream_id = 0

    for channel in channels:
        # slack_channel_id = channel['id']

        # map Slack's topic and purpose content into Zulip's stream description.
        # WARN This mapping is lossy since the topic.creator, topic.last_set,
        # purpose.creator, purpose.last_set fields are not preserved.
        description = channel["purpose"]["value"]
        stream_id = stream_id_count
        recipient_id = recipient_id_count

        # construct the stream object and append it to zerver_stream
        stream = build_stream(float(channel["created"]), realm_id, channel["name"],
                              description, stream_id, channel["is_archived"])
        zerver_stream.append(stream)

        # construct defaultstream object
        # slack has the default channel 'general' and 'random'
        # where every user is subscribed
        default_channels = ['general', 'random']  # Slack specific
        if channel['name'] in default_channels:
            defaultstream = build_defaultstream(realm_id, stream_id,
                                                defaultstream_id)
            zerver_defaultstream.append(defaultstream)
            defaultstream_id += 1

        added_channels[stream['name']] = (channel['id'], stream_id)

        recipient = build_recipient(stream_id, recipient_id, Recipient.STREAM)
        zerver_recipient.append(recipient)
        added_recipient[stream['name']] = recipient_id
        # TODO add recipients for private message and huddles

        # construct the subscription object and append it to zerver_subscription
        subscription_id_count = get_subscription(channel['members'], zerver_subscription,
                                                 recipient_id, added_users,
                                                 subscription_id_count)
        # TODO add zerver_subscription which correspond to
        # huddles type recipient
        # For huddles:
        # sub['recipient']=recipient['id'] where recipient['type_id']=added_users[member]

        stream_id_count += 1
        recipient_id_count += 1
        logging.info(u"{} -> created".format(channel['name']))

        # TODO map Slack's pins to Zulip's stars
        # There is the security model that Slack's pins are known to the team owner
        # as evident from where it is stored at (channels)
        # "pins": [
        #         {
        #             "id": "1444755381.000003",
        #             "type": "C",
        #             "user": "******",
        #             "owner": "U061A5N1G",
        #             "created": "1444755463"
        #         }
        #         ],

    for user in zerver_userprofile:
        # this maps the recipients and subscriptions
        # related to private messages
        recipient = build_recipient(user['id'], recipient_id_count, Recipient.PERSONAL)
        sub = build_subscription(recipient_id_count, user['id'], subscription_id_count)

        zerver_recipient.append(recipient)
        zerver_subscription.append(sub)

        subscription_id_count += 1
        recipient_id_count += 1

    logging.info('######### IMPORTING STREAMS FINISHED #########\n')
    return zerver_defaultstream, zerver_stream, added_channels, zerver_subscription, \
        zerver_recipient, added_recipient
Example #10
0
def channels_to_zerver_stream(slack_data_dir: str, realm_id: int, added_users: AddedUsersT,
                              zerver_userprofile: List[ZerverFieldsT]) -> Tuple[List[ZerverFieldsT],
                                                                                List[ZerverFieldsT],
                                                                                AddedChannelsT,
                                                                                List[ZerverFieldsT],
                                                                                List[ZerverFieldsT],
                                                                                AddedRecipientsT]:
    """
    Returns:
    1. zerver_defaultstream, which is a list of the default streams
    2. zerver_stream, while is a list of all streams
    3. added_channels, which is a dictionary to map from channel name to channel id, zulip stream_id
    4. zerver_subscription, which is a list of the subscriptions
    5. zerver_recipient, which is a list of the recipients
    6. added_recipient, which is a dictionary to map from channel name to zulip recipient_id
    """
    logging.info('######### IMPORTING CHANNELS STARTED #########\n')
    channels = get_data_file(slack_data_dir + '/channels.json')

    added_channels = {}
    added_recipient = {}

    zerver_stream = []
    zerver_subscription = []  # type: List[ZerverFieldsT]
    zerver_recipient = []
    zerver_defaultstream = []

    stream_id_count = subscription_id_count = recipient_id_count = defaultstream_id = 0

    for channel in channels:
        # slack_channel_id = channel['id']

        # map Slack's topic and purpose content into Zulip's stream description.
        # WARN This mapping is lossy since the topic.creator, topic.last_set,
        # purpose.creator, purpose.last_set fields are not preserved.
        description = channel["purpose"]["value"]
        stream_id = stream_id_count
        recipient_id = recipient_id_count

        # construct the stream object and append it to zerver_stream
        stream = build_stream(float(channel["created"]), realm_id, channel["name"],
                              description, stream_id, channel["is_archived"])
        zerver_stream.append(stream)

        # construct defaultstream object
        # slack has the default channel 'general' and 'random'
        # where every user is subscribed
        default_channels = ['general', 'random']  # Slack specific
        if channel['name'] in default_channels:
            defaultstream = build_defaultstream(realm_id, stream_id,
                                                defaultstream_id)
            zerver_defaultstream.append(defaultstream)
            defaultstream_id += 1

        added_channels[stream['name']] = (channel['id'], stream_id)

        recipient = build_recipient(stream_id, recipient_id, Recipient.STREAM)
        zerver_recipient.append(recipient)
        added_recipient[stream['name']] = recipient_id
        # TODO add recipients for private message and huddles

        # construct the subscription object and append it to zerver_subscription
        subscription_id_count = get_subscription(channel['members'], zerver_subscription,
                                                 recipient_id, added_users,
                                                 subscription_id_count)
        # TODO add zerver_subscription which correspond to
        # huddles type recipient
        # For huddles:
        # sub['recipient']=recipient['id'] where recipient['type_id']=added_users[member]

        stream_id_count += 1
        recipient_id_count += 1
        logging.info(u"{} -> created".format(channel['name']))

        # TODO map Slack's pins to Zulip's stars
        # There is the security model that Slack's pins are known to the team owner
        # as evident from where it is stored at (channels)
        # "pins": [
        #         {
        #             "id": "1444755381.000003",
        #             "type": "C",
        #             "user": "******",
        #             "owner": "U061A5N1G",
        #             "created": "1444755463"
        #         }
        #         ],

    for user in zerver_userprofile:
        # this maps the recipients and subscriptions
        # related to private messages
        recipient = build_recipient(user['id'], recipient_id_count, Recipient.PERSONAL)
        sub = build_subscription(recipient_id_count, user['id'], subscription_id_count)

        zerver_recipient.append(recipient)
        zerver_subscription.append(sub)

        subscription_id_count += 1
        recipient_id_count += 1

    logging.info('######### IMPORTING STREAMS FINISHED #########\n')
    return zerver_defaultstream, zerver_stream, added_channels, zerver_subscription, \
        zerver_recipient, added_recipient