Esempio n. 1
0
def get_channel_by_id(**kwargs):
    log.info('Get channel by id kwargs: {}'.format(kwargs))
    channel = session.query(Channel).filter(Channel.id == kwargs['channel_id'], Channel.deleted_on.is_(None))\
        .first()
    if not channel:
        raise ValueError('INVALID-CHANNEL-ID')
    return channel
Esempio n. 2
0
def authenticate(username, password):
    """
    @api {post} /auth Authentication
    @apiName Authentication/Login
    @apiGroup User

    @apiHeader {String} Content-Type Should be application/json for /auth
    @apiParam {String} username Username of the user
    @apiParam {String} password Password of the user

    @apiSuccess {String} access_code JWT

    @apiSuccessExample Success Response
    HTTP/1.1 200 OK
    {
      "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6IjM5MDA4MGExLWY0ZjctMTFlNS04NTRkLTI4ZDI0NDQyZDNlNyIsImlhdCI6MTQ1OTE3ODE0NSwibmJmIjoxNDU5MTc4MTQ1LCJleHAiOjE0NTkxNzg0NDV9.nx_1a4RmvJ7Vlf1CvnMzqoTfzChcuJnDb1Tjy1_FnXw"
    }

    @apiErrorExample Invalid Credentials
    {
      "description": "Invalid credentials",
      "error": "Bad Request",
      "status_code": 401
    }
    """
    log.info('Authentication Username: {}, Password: {}'.format(username, password))
    user = session.query(User).filter(User.username == username, User.deleted_on.is_(None)).first()
    if user:
        if user.password == password:
            return user
    return False
Esempio n. 3
0
def get_pending_invitation(**kwargs):
    log.info('Get Pending Invitation kwargs: {}'.format(kwargs))
    user_id = kwargs['user_id']
    pending_invitation = session.query(ChannelJoinRequest).\
        filter(ChannelJoinRequest.requested_for == get_user_by_id(user_id=user_id).username,
               ChannelJoinRequest.rejected_on.is_(None), ChannelJoinRequest.deleted_on.is_(None)).\
        all()
    return pending_invitation
Esempio 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,
    )
Esempio n. 5
0
def is_channel_unsubscribed(**kwargs):
    log.info('Is channel unsubscribed kwargs: {}'.format(kwargs))
    user_id = kwargs['user_id']
    channel_id = kwargs['channel_id']
    channel_mapping = session.query(UserChannelMapping)\
        .filter(UserChannelMapping.user_id == user_id, UserChannelMapping.channel_id == channel_id)\
        .first()
    if not channel_mapping:
        log.info('No channel mapping found for user: {} and channel: {}, creating a mapping'.format(user_id, channel_id))
        channel_mapping = create_user_channel_mapping(user_id=user_id, channel_id=channel_id)
    if channel_mapping and channel_mapping.is_unsubscribed:
        return True
    return False
Esempio n. 6
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
Esempio n. 7
0
def fetch_message(**kwargs):
    log.info('Fetch Message kwargs: {}'.format(kwargs))
    message_list = []
    user_id = kwargs['user_id']
    channel_obj = get_channel_by_name(channel_name=kwargs['channel_name'])
    if channel_obj and not is_channel_unsubscribed(channel_id=channel_obj.id, user_id=user_id):
        page_size = kwargs['page_size']
        page = kwargs['page_num']
        message_list = session.query(ChatHistory) \
            .filter(ChatHistory.channel == channel_obj.id, ChatHistory.deleted_on.is_(None)) \
            .order_by(desc(ChatHistory.created_on))
        if page_size:
            message_list = message_list.limit(PER_PAGE_RESPONSE_LIMIT)
        if page:
            message_list = message_list.offset(page*page_size)
        message_marked_deleted = chat_marked_deleted(user_id=user_id, channel_id=channel_obj.id)
        if not message_marked_deleted[0]:
            return message_list.all()
        else:
            return [message for message in message_list.all()
                    if message.created_on > message_marked_deleted[0]]

    return message_list
Esempio n. 8
0
def get_user_by_id(**kwargs):
    user = session.query(User).filter(User.id == kwargs['user_id'], User.deleted_on.is_(None)).first()
    if not user:
        raise ValueError('BAD-USER-ID')
    return user
Esempio n. 9
0
def get_user_by_username(**kwargs):
    log.info('In get_user_by_username')
    return session.query(User) \
        .filter(User.username == kwargs['username'], User.deleted_on.is_(None)) \
        .first()
Esempio n. 10
0
def chat_marked_deleted(**kwargs):
    return session.query(UserChannelMapping.marked_deleted_on).\
        filter(UserChannelMapping.user_id==kwargs['user_id']).\
        filter(UserChannelMapping.channel_id==kwargs['channel_id']).first()
Esempio n. 11
0
def get_user_channel_mapping(**kwargs):
    return session.query(UserChannelMapping).\
        filter(UserChannelMapping.user_id == kwargs['user_id'], UserChannelMapping.is_unsubscribed == False).\
        all()
Esempio n. 12
0
def get_channel_by_name(**kwargs):
    log.info('Get channel by name kwargs: {}'.format(kwargs))
    return session.query(Channel).filter(Channel.name == kwargs['channel_name'], Channel.deleted_on.is_(None))\
        .first()