Exemple #1
0
def handle(connection, data):
    user = connection.user
    room_id = data.get('roomId')
    del_msg_id = data.get('messageId')
    msgs = get_room_messages(room_id)
    # user can delete message if:
    # 1. user is mod
    # 2. user is room owner
    # 3. own message

    existing_msg = [m for m in msgs if m['id'] == del_msg_id]
    if len(existing_msg) > 0:
        existing_msg = existing_msg[0]
    else:
        return {
            'error': 404,
            "roomId": room_id,
            'message': 'message not found'
        }

    if not existing_msg['user']['id'] == user['id']:
        check_permission(ACTION_NAME, connection.token, room_id)

    msgs = [m for m in msgs if m['id'] != del_msg_id]
    save_room_messages(room_id, msgs)

    payload = {
        "name": ACTION_NAME,
        "roomId": room_id,
        "data": del_msg_id,
        "connectionId": connection.id
    }
    redis_client.publish('sp', json.dumps(payload))
    return payload
Exemple #2
0
def handle(connection, data):

    room = data['room']
    token = data.get('token')
    user = get_user(token)
    room_id = room['id']
    if user:
        connection.user = user
        room_info = join_room(connection, user, room_id)
        connection.join_room(room_id)

        room_info['chatHistory'] = get_room_messages(room_id)
        room_info['roomId'] = room_id

        # save connection - {'user':{}, 'rooms':[]}
        # need this mapping to remove user from all rooms they joined
        # save_connection(connection_id, user,
        #                 list(set(previous_joined_room_ids+[room['id']])))

        # do we need this mapping? useful when user login/logout
        # save user - {'connections':[]}
        # save_user(connection_id, user['id'])

        # TODO: client shouldn't see other user's connections
        # time.sleep(1)
        res = {"name": "room info", "data": room_info}

        return res

    else:
        return {"error": "don't support visitor yet"}
Exemple #3
0
def lambda_handler(event, context):

    connection_id = event["requestContext"].get("connectionId")
    data = json.loads(event['body'])['data']

    # This is only called by chatbox not injection script
    # And chatbox always getChatHistory
    get_chat_history = data.get('getChatHistory')
    rooms = data.get('rooms', [])
    res = {}
    if len(rooms) == 0:
        # If client doesn't specify which room
        # return room info of all rooms this connection is in
        connection = get_connection(connection_id)
        if connection:
            rooms = connection['rooms']

    for room_id in rooms:

        room = get_room(room_id) or {}
        if get_chat_history:
            room['chatHistory'] = get_room_messages(room_id)
            if room.get('users'):
                # client shouldn't see other user's connections
                for u in room["users"]:
                    del u['connections']
        res[room_id] = room

    payload = {"name": "room info", "data": res, "query": data}
    return {'statusCode': 200, 'body': json.dumps(payload)}
Exemple #4
0
def handle(connection, data):

    room_id = data['roomId']
    user = connection.user
    token = connection.token

    check_permission(ACTION_NAME, token, room_id)

    room_info = join_room(connection, user, room_id)
    connection.join_room(room_id)
    room_info['chatHistory'] = get_room_messages(room_id)
    res = {"name": "room_info", "roomId": room_id, "data": room_info}
    return res
Exemple #5
0
def save_msg(data, room_id):
    data = copy.deepcopy(data)
    """
    Input
        chat_message = {
            "id": message_id,
            "user": sender,
            "content": content
        }
    add created?
    """
    chat_history = get_room_messages(room_id)
    chat_history.append(data)
    save_room_messages(room_id, chat_history)
def lambda_handler(event, context):
    room_id = event.get("queryStringParameters", {}).get('roomId')
    timestamp = event.get("queryStringParameters", {}).get('timestamp')
    res = get_room_messages(room_id)
    if timestamp:
        timestamp = int(timestamp)
        res = [m for m in res if m['timestamp'] > timestamp]
    return {
        'statusCode': 200,
        'body': json.dumps(res),
        'headers': {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Headers': 'token'
        },
    }
Exemple #7
0
def lambda_handler(event, context):
    connection_id = event["requestContext"].get("connectionId")
    data = json.loads(event['body'])['data']
    token = data.get('token')
    user = get_user(token)
    if user:
        room_id = data.get('roomId')
        del_msg_id = data.get('messageId')
        msgs = get_room_messages(room_id)
        # user can delete message if:
        # 1. user is mod
        # 2. user is room owner
        # 3. own message

        if not user['isMod']:
            if not str(room_id) in user['rooms']:

                for m in msgs:
                    if m['id'] == del_msg_id:
                        msg_sender = m['user']
                        if user['id'] != msg_sender['id']:
                            return {
                                'statusCode': 403,
                                'body': json.dumps('forbidden!')
                            }

        msgs = [m for m in msgs if m['id'] != del_msg_id]
        save_room_messages(room_id, msgs)

        payload = {
            "name": "delete message",
            "data": {
                "roomId": room_id,
                "messageId": del_msg_id
            }
        }

        endpoint_url = 'https://' + event["requestContext"]["domainName"] + \
            '/'+event["requestContext"]["stage"]

        send_msg_to_room(endpoint_url,
                         payload,
                         room_id,
                         exclude_connection=connection_id)

        return {'statusCode': 200, 'body': json.dumps(payload)}
    else:
        return {'statusCode': 401, 'body': json.dumps('not logged in!')}
Exemple #8
0
def save_msg(data, room_id):
    data = copy.deepcopy(data)
    """
    Input
        chat_message = {
            "id": message_id,
            "roomId": room_id,
            "user": sender,
            "content": content
        }
    Remove roomId, add created
    """
    del data['roomId']
    chat_history = get_room_messages(room_id)
    chat_history.append(data)
    save_room_messages(room_id, chat_history)
Exemple #9
0
def save_msg(data, room_id):
    """
    Input
        chat_message = {
            "id": message_id,
            "roomId": room_id,
            "roomType": room_type,
            "user": sender,
            "content": content
        }
    Remove roomId and roomType, add timestamp
    """
    del data['roomId']
    del data['roomType']
    data['timestamp'] = int(time.time() * 1000)
    chat_history = get_room_messages(room_id)
    chat_history.append(data)
    save_room_messages(room_id, chat_history)
Exemple #10
0
def save_msg(data, room_id):
    """
    Input
        chat_message = {
            "id": message_id,
            "roomId": room_id,
            "roomType": room_type,
            "user": sender,
            "content": content
        }
    Remove roomId and roomType, add created_at
    """
    del data['roomId']
    del data['roomType']
    data['created_at'] = datetime.datetime.utcnow().isoformat()
    chat_history = get_room_messages(room_id)
    chat_history.append(data)
    save_room_messages(room_id, chat_history)
Exemple #11
0
def lambda_handler(event, context):

    connection_id = event["requestContext"].get("connectionId")
    data = json.loads(event['body'])['data']

    rooms = data['rooms']
    room_ids = [r['id'] for r in rooms]
    token = data.get('token')
    get_chat_history = data.get('getChatHistory')

    user = get_user(token)

    if user:
        previous_joined_room_ids = []
        connection = get_connection(connection_id)
        if connection:
            previous_joined_room_ids = connection['rooms']

        # join room if not already in
        joined_rooms = {}
        for room in rooms:
            room_info = join_room(connection_id, user, room['id'],
                                  room['type'], event)
            if get_chat_history:
                room_info['chatHistory'] = get_room_messages(room['id'])
            joined_rooms[room['id']] = room_info

        # leave room not in payload
        for room_id in previous_joined_room_ids:
            if not (room_id in room_ids):
                delete_connection_from_rooms(event, connection_id, user,
                                             [room_id])

        # save connection - {'user':{}, 'rooms':[]}
        save_connection(connection_id, user, room_ids)
        # save user - {'connections':[]}
        # save_user(connection_id, user['id'])

        # TODO: client shouldn't see other user's connections
        res = {"name": "room info", "data": joined_rooms}

        return {'statusCode': 200, 'body': json.dumps(res)}
    else:
        return {'statusCode': 400, 'body': json.dumps('not logged in!')}
def lambda_handler(event, context):
    # called by chatbox not injection script
    # so it's ok to include chat history
    connection_id = event["requestContext"].get("connectionId")
    data = json.loads(event['body'])['data']

    room = data['room']
    token = data.get('token')
    user = get_user(token)

    if user:
        previous_joined_room_ids = []
        newly_joined_rooms = {}
        connection = get_connection(connection_id)
        if connection:
            previous_joined_room_ids = connection['rooms']

        room_info = join_room(connection_id, user, room['id'], room['type'],
                              event)
        room_info['chatHistory'] = get_room_messages(room['id'])

        newly_joined_rooms[room['id']] = room_info

        # save connection - {'user':{}, 'rooms':[]}
        save_connection(connection_id, user,
                        list(set(previous_joined_room_ids + [room['id']])))

        # save user - {'connections':[]}
        # save_user(connection_id, user['id'])

        # TODO: client shouldn't see other user's connections
        res = {"name": "room info", "data": newly_joined_rooms}

        return {'statusCode': 200, 'body': json.dumps(res)}
    else:
        return {'statusCode': 400, 'body': json.dumps('not logged in!')}