Esempio n. 1
0
def share_message():
    if 'entity_id' not in session:
        return auth_failed_response()

    entity_id = session['entity_id']
    
    if "message_name" not in request.json:
        return bad_parameter_response("message_name")
    if "other_entity_ids" not in request.json:
        return bad_parameter_response("other_entity_ids")
    
    message_name = request.json["message_name"]
    other_entity_ids = request.json["other_entity_ids"]
    
    if isinstance(other_entity_ids,str):
        other_entity_ids = [other_entity_ids]
    elif not isinstance(other_entity_ids,list):
        return bad_parameter_response("other_entity_ids")
    
    message_auth = MessageAuth.query.filter_by(message_name=message_name,entity_id=str(entity_id),is_owner=True).first()
    if not message_auth:
        return jsonify({"error":"not owner"})
    else:
        for eid in other_entity_ids:
            existing_message_auth = MessageAuth.query.filter_by(message_name=message_name, entity_id=str(eid)).first()
            if not existing_message_auth:
                new_message_auth = MessageAuth()
                new_message_auth.entity_id = str(eid)
                new_message_auth.message_name = message_name
                new_message_auth.is_owner = False
                db.session.add(new_message_auth)
                db.session.commit()
                
        return ok_response()
Esempio n. 2
0
def subscribe():
    """
    SUPPORTS: POST

    Start listening to a message for the plugin that sends this request.

    Accepts: JSON
        - message_name - the name of the message to subscribe to

    Returns: 
        403         - A connection with HPIT must be established first.
        404         - Could not find the plugin stored in the session.
        200:OK      - Mapped the message to the plugin
        200:EXISTS  - The mapping already exists
    """
    if 'message_name' not in request.json:
        return bad_parameter_response('message_name')

    if 'entity_id' not in session:
        return auth_failed_response()

    message_name = request.json['message_name']
    entity_id = session['entity_id']         

    plugin = Plugin.query.filter_by(entity_id=entity_id).first()

    if not plugin:
        return not_found_response()
        
    #message auth
    message_auth = MessageAuth.query.filter_by(message_name=message_name).first()
    if not message_auth: #this will be the owner
        if user_verified(message_name,plugin):
            new_message_auth = MessageAuth()
            new_message_auth.entity_id = str(entity_id)
            new_message_auth.message_name = message_name
            new_message_auth.is_owner = True
            db.session.add(new_message_auth)
            db.session.commit()
        else:
            return jsonify({"error":"invalid message name"})

    subscription = Subscription.query.filter_by(plugin=plugin, message_name=message_name).first()

    if subscription:
        return exists_response()

    subscription = Subscription()
    subscription.plugin = plugin
    subscription.message_name = message_name

    db.session.add(subscription)
    db.session.commit()

    return ok_response()
Esempio n. 3
0
def share_message():
    if 'entity_id' not in session:
        return auth_failed_response()

    entity_id = session['entity_id']

    if "message_name" not in request.json:
        return bad_parameter_response("message_name")
    if "other_entity_ids" not in request.json:
        return bad_parameter_response("other_entity_ids")

    message_name = request.json["message_name"]
    other_entity_ids = request.json["other_entity_ids"]

    if isinstance(other_entity_ids, str):
        other_entity_ids = [other_entity_ids]
    elif not isinstance(other_entity_ids, list):
        return bad_parameter_response("other_entity_ids")

    message_auth = MessageAuth.query.filter_by(message_name=message_name,
                                               entity_id=str(entity_id),
                                               is_owner=True).first()
    if not message_auth:
        return jsonify({"error": "not owner"})
    else:
        for eid in other_entity_ids:
            existing_message_auth = MessageAuth.query.filter_by(
                message_name=message_name, entity_id=str(eid)).first()
            if not existing_message_auth:
                new_message_auth = MessageAuth()
                new_message_auth.entity_id = str(eid)
                new_message_auth.message_name = message_name
                new_message_auth.is_owner = False
                db.session.add(new_message_auth)
                db.session.commit()

        return ok_response()
Esempio n. 4
0
def subscribe():
    """
    SUPPORTS: POST

    Start listening to a message for the plugin that sends this request.

    Accepts: JSON
        - message_name - the name of the message to subscribe to

    Returns: 
        403         - A connection with HPIT must be established first.
        404         - Could not find the plugin stored in the session.
        200:OK      - Mapped the message to the plugin
        200:EXISTS  - The mapping already exists
    """
    if 'message_name' not in request.json:
        return bad_parameter_response('message_name')

    if 'entity_id' not in session:
        return auth_failed_response()

    message_name = request.json['message_name']
    entity_id = session['entity_id']

    plugin = Plugin.query.filter_by(entity_id=entity_id).first()

    if not plugin:
        return not_found_response()

    #message auth
    message_auth = MessageAuth.query.filter_by(
        message_name=message_name).first()
    if not message_auth:  #this will be the owner
        if user_verified(message_name, plugin):
            new_message_auth = MessageAuth()
            new_message_auth.entity_id = str(entity_id)
            new_message_auth.message_name = message_name
            new_message_auth.is_owner = True
            db.session.add(new_message_auth)
            db.session.commit()
        else:
            return jsonify({"error": "invalid message name"})

    #remove old subscriptions
    subscriptions = Subscription.query.filter_by(plugin=plugin)
    for remove_subscription in subscriptions:
        now = datetime.now()
        if not remove_subscription.time:
            db.session.delete(remove_subscription)
        else:
            dt = now - remove_subscription.time
            if dt.days >= 1:
                db.session.delete(remove_subscription)
    db.session.commit()

    #add subscription
    subscription = Subscription.query.filter_by(
        plugin=plugin, message_name=message_name).first()

    if subscription:
        return exists_response()

    subscription = Subscription()
    subscription.plugin = plugin
    subscription.message_name = message_name
    subscription.time = datetime.now()

    db.session.add(subscription)
    db.session.commit()

    return ok_response()