Exemplo n.º 1
0
async def patchChat(request, id, chat_id):
    user = db.findUserById(id)
    if user == None:
        return json_response({ 'error': Response.NotFoundError })

    if not Auth.ValidateUser(user, request):
        return json_response({ 'error':  Response.InvalidUser }, status=400)

    chat = db.findChatById(chat_id)
    if chat == None:
        return json_response({ 'error': Response.NotFoundError })

    body = request.json

    if 'user_ids' not in body:
        return json_response({ 'error': Response.BadRequest }, status=400)

    notify = Notify()
    users = db.findUsersByIds(chat['user_ids'])
    apnTokens = [otherUser['apn_token'] for otherUser in users if otherUser['_id'] != user['_id']]
    custom = { 'chat_id' : chat['_id'], 'type' : 'users_added' }
    notify.sendMessages(apnTokens, user['first_name'] + ' ' + user['last_name'] + ' added more people to one of your chats.', custom)

    newUserIdsToAdd = [userId for userId in body['user_ids'] if userId not in chat['user_ids']]

    db.addUsersToChat(chat_id, newUserIdsToAdd)
    chat = db.findChatById(chat_id)
    chat['users'] = db.findUsersByIds(chat['user_ids'])

    return json_response({ 'chat' : chat })
Exemplo n.º 2
0
async def getUserChats(request, id):
    user = db.findUserById(id)
    if user == None:
        return json_response({ 'error': Response.NotFoundError })

    if not Auth.ValidateUser(user, request):
        return json_response({ 'error':  Response.InvalidUser }, status=400)

    chats = db.findChatsByUserId(id)
    if 'removed_chat_ids' in user:
        chats = [chat for chat in chats if chat['_id'] not in user['removed_chat_ids']]

    for chat in chats:
        chat['users'] = db.findUsersByIds(chat['user_ids'])

    return json_response({ 'chats' : chats })
Exemplo n.º 3
0
async def getChatMessages(request, user_id, chat_id):
    user = db.findUserById(user_id)
    chat = db.findChatById(chat_id)

    if user == None or chat == None:
        return json_response({'error': Response.NotFoundError})

    if not Auth.ValidateUser(user, request):
        return json_response({'error': Response.InvalidUser}, status=400)

    messages = db.findMessagesByChatId(chat_id)
    users = db.findUsersByIds(chat['user_ids'])

    return json_response({
        'messages': messages,
        'chat': chat,
        'users': users
    },
                         status=201)
Exemplo n.º 4
0
async def getPageByUrl(request):
    if 'url' not in request.args:
        return json_response({ 'error': 'no url' }, status=400)

    page_parser = PageParser(request.args['url'][0])
    website = page_parser.website
    path = page_parser.path

    page = db.findPageByWebsiteAndPath(website, path)

    # Create page.
    if page == None:
        parsed = page_parser.getPage()
        if not parsed:
            return json_response({ 'error': 'Parsing did not work...' }, status=400)

        page_id = db.insertPage(parsed)
        page = db.findPageById(page_id)

    # Add src_img if it exists
    page['src_img'] = NewsSources.getSrcImg(page['website'])

    # Get comments.
    comments = db.findCommentsByPageId(page['_id'])

    # Get users.
    users = db.findUsersByIds([comment['user_id'] for comment in comments])

    jsonComments = []
    for comment in comments:
        comment_user = None
        for user in users:
            if user['_id'] == comment['user_id']:
                comment_user = user
                break

        comment['user'] = comment_user
        jsonComments.append(comment)

    return json_response({ 'page': page, 'comments': jsonComments }, status=200)
Exemplo n.º 5
0
async def postChat(request, user_id, chat_id):
    user = db.findUserById(user_id)
    chat = db.findChatById(chat_id)

    if user == None or chat == None:
        return json_response({'error': Response.NotFoundError})

    if not Auth.ValidateUser(user, request):
        return json_response({'error': Response.InvalidUser}, status=400)

    body = request.json

    if 'message' not in body:
        return json_response({'error': Response.BadRequest}, status=400)

    db.insertMessage(user_id, chat_id, body['message'])
    messages = db.findMessagesByChatId(chat_id)
    chat = db.findChatById(chat_id)
    users = db.findUsersByIds(chat['user_ids'])
    apnTokens = [
        otherUser['apn_token'] for otherUser in users
        if otherUser['_id'] != user['_id'] and (
            'removed_chat_ids' not in otherUser
            or chat['_id'] not in otherUser['removed_chat_ids'])
    ]

    # Send push notification to users.
    notify = Notify()
    custom = {'chat_id': chat['_id'], 'type': 'new_message'}
    notify.sendMessages(apnTokens,
                        'Encrypted message from ' + user['first_name'] + '.',
                        custom)

    return json_response({
        'messages': messages,
        'chat': chat,
        'users': users
    },
                         status=201)