Esempio n. 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
Esempio n. 2
0
def get_news_and_save_to_room():
    resp = requests.get(URL)
    news = resp.json().get('articles', [])

    chat_history = []
    for n in news:
        url = n.get('url')
        if url:
            url = url.replace('http://', 'https://')
            item = {
                "id": str(uuid.uuid4()),
                "user": {
                    'id': 3,
                    'name': "news",
                    'about': "新闻推送",
                    'avatarSrc': "https://dnsofx4sf31ab.cloudfront.net/3.jpg"
                },
                "content": {
                    "type": "url",
                    "iframe_url": url,
                    "url": url,
                    "title": n.get('title')
                },
                "created_at": datetime.datetime.utcnow().isoformat()
            }
            chat_history.append(item)
    save_room_messages(ROOM_ID, chat_history)
    return chat_history
Esempio n. 3
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)
Esempio n. 4
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!')}
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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)