Exemplo n.º 1
0
def create_channel(**kwargs):
    log.info('Create Channel kwargs: {}'.format(kwargs))
    channel_name = kwargs['channel_name']
    user_id = kwargs['user_id']
    channel_obj = get_channel_by_name(channel_name=channel_name)
    channel_type = kwargs.get('channel_type', 'public').lower()
    if not channel_obj:
        channel_create_params = dict(
            id=str(uuid4()),
            name=channel_name,
            created_by=user_id,
            type=channel_type,
        )
        log.info('Create channel payload: {}'.format(channel_create_params))
        channel = Channel(**channel_create_params)
        session.add(channel)

        create_user_channel_mapping(user_id=user_id,
                                    channel_id=get_channel_by_name(channel_name=channel_name).id)
    else:
        create_user_channel_mapping(user_id=user_id,
                                    channel_id=get_channel_by_name(channel_name=channel_name).id)
    return dict(
        channel_name=channel_name,
        type=channel_type,
        created=True
    )
Exemplo n.º 2
0
def create_user_channel_mapping(**kwargs):
    log.info('Create User Channel Mapping kwargs: {}'.format(kwargs))
    params = dict(
        id=str(uuid4()),
        user_id=kwargs['user_id'],
        channel_id=kwargs['channel_id'],
    )
    user_channel_mapping = UserChannelMapping(**params)
    session.add(user_channel_mapping)
    return user_channel_mapping
Exemplo n.º 3
0
            def callback_function(ch, method, properties, body):
                log.info('Pika Client Message payload: {}'.format(body))
                payload = json.loads(body)
                payload.update(dict(
                    id=str(uuid4()),
                ))

                chat_obj = ChatHistory(**payload)
                session.add(chat_obj)
                session.commit()
Exemplo n.º 4
0
def delete_chat(**kwargs):
    log.info('Delete chat kwargs: {}'.format(kwargs))
    channel = get_channel_by_name(channel_name=kwargs['channel_name'])
    chat_obj = session.query(UserChannelMapping) \
        .filter(UserChannelMapping.user_id == kwargs['user_id'], UserChannelMapping.channel_id == channel.id)\
        .first()
    setattr(chat_obj, 'marked_deleted_on', datetime.now())
    session.add(chat_obj)
    return dict(
        chat_deleted=True,
    )
Exemplo n.º 5
0
    def setUpClass(cls):
        super(TestUser, cls).setUpClass()
        cls.user = User(
            id=cls.user_id,
            username=cls.username,
            password=cls.password,
            first_name=cls.first_name,
            last_name=cls.last_name,
        )

        session.add(cls.user)
        session.flush()
Exemplo n.º 6
0
def join_channel_request(**kwargs):
    log.info('Send Invitation kwargs: {}'.format(kwargs))
    channel = get_channel_by_name(channel_name=kwargs['channel_name'])
    if not channel:
        raise ValueError('INVALID-CHANNEL-NAME')
    send_invite_payload = dict(
        id=str(uuid4()),
        requested_by=kwargs['user_id'],
        requested_for=kwargs['invited_user_name'],
        channel_id=channel.id,
    )
    invite_user = ChannelJoinRequest(**send_invite_payload)
    session.add(invite_user)
    return dict(
        invitation_sent=True
    )
Exemplo n.º 7
0
def signup(**kwargs):
    log.info('Create user kwargs: {}'.format(kwargs))
    if not get_user_by_username(username=kwargs['username']):
        kwargs.update(dict(
            id=str(uuid4()),
        ))
        user = User(**kwargs)
        session.add(user)
        session.flush()
        log.info("User added. id=%s", user.id)
    else:
        raise ValueError('SIGNUP-EXISTS-USERNAME')
    return dict(
        user_id=user.id,
        username=user.username,
        created=True
    )
Exemplo n.º 8
0
def unsubscribe_channel(**kwargs):
    log.info('Unsubscribe Channel kwargs: {}'.format(kwargs))
    channel = get_channel_by_name(channel_name=kwargs['channel_name'])
    if not channel:
        raise ValueError('INVALID-CHANNEL-NAME')

    user_id = kwargs['user_id']
    user_channel_obj = session.query(UserChannelMapping)\
        .filter(UserChannelMapping.user_id == user_id,
                UserChannelMapping.channel_id == channel.id, UserChannelMapping.deleted_on.is_(None))\
        .first()
    if not user_channel_obj:
        raise ValueError('INVALID-CHANNEL-MAPPING')
    else:
        log.info('Unsubscribing User: {} from channel:{}'.format(user_id, kwargs['channel_name']))
        setattr(user_channel_obj, 'is_unsubscribed', True)
        session.add(user_channel_obj)
        return user_channel_obj
Exemplo n.º 9
0
    def setUpClass(cls):
        super(TestChat, cls).setUpClass()
        cls.user = User(
            id=cls.user_id,
            username=cls.username,
            password=cls.password,
            first_name=cls.first_name,
            last_name=cls.last_name,
        )

        session.add(cls.user)
        session.flush()

        cls.channel = Channel(
            id=cls.channel_id,
            name=cls.channel_name,
            type=cls.type,
            created_by=cls.user.id,
        )

        session.add(cls.channel)
        session.flush()

        cls.chat = ChatHistory(
            id=cls.message_id,
            published_by=cls.user_id,
            message=cls.message,
            channel=cls.channel_id,
        )
        session.add(cls.chat)
        session.flush()
Exemplo n.º 10
0
    def setUpClass(cls):
        super(TestChannel, cls).setUpClass()
        cls.user = User(
            id=cls.user_id,
            username=cls.username,
            password=cls.password,
            first_name=cls.first_name,
            last_name=cls.last_name,
        )

        session.add(cls.user)
        session.flush()

        cls.channel = Channel(
            id=cls.channel_id,
            name=cls.channel_name,
            type=cls.type,
            created_by=cls.user.id,
        )

        session.add(cls.channel)
        session.flush()