<%@ language = "JavaScript" %>
<%
/*
    $Revision$
    $Id$

    File:               get_mobile_ajax.asp
    Description:        Ophalen van allerlei info om client-side data te hebben
    Parameters:         init_key
                        niveau
    Context:
    Note:

*/ %>

<%
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc"-->
<!-- #include file="../MSG/msg.inc"-->

<%
var action = getFParam("action");
var result = {};
protectRequest.validateToken();

switch (action)
{
    case "msg_react": { /* Toggled je reactie */
        var msg_key = getFParamInt("msg_key", -1);
        if (msg_key == -1) {
            result = { "success": false };
            break;
        }
        var this_msg = msg.func_enabled(msg_key);
        var react_type = getFParamSafe("react_type");
        var all_reactions = ["like", "metoo"]; // Whitelist
        user.auth_required_or_abort(this_msg.canRead && inArray(react_type, all_reactions)); /* Met read-rechten mag je al reacten */
        var sql = "SELECT 'x'"
                + "  FROM fac_nieuws"
                + " WHERE fac_nieuws_titel = " + safe.quoted_sql_upper("##" + react_type + "##")
                + "   AND fac_nieuws_omschrijving IS NULL"
                + "   AND fac_nieuws_parentkey = " + msg_key
                + "   AND prs_perslid_key = " + user_key;
        var oRs = Oracle.Execute(sql);
        var reacted = !oRs.EoF;
        oRs.Close();
        if (reacted) { /* -> Un-react */
            sql = "DELETE"
                + "  FROM fac_nieuws"
                + " WHERE fac_nieuws_titel = " + safe.quoted_sql_upper("##" + react_type + "##")
                + "   AND fac_nieuws_omschrijving IS NULL"
                + "   AND fac_nieuws_parentkey = " + msg_key
                + "   AND prs_perslid_key = " + user_key;
            Oracle.Execute(sql);
        } else { /* -> React */
            sql = "INSERT INTO fac_nieuws (fac_nieuws_parentkey, prs_perslid_key, fac_nieuws_titel, fac_nieuws_show)"
                + "     VALUES (" + msg_key + ", " + user_key + ", " + safe.quoted_sql_upper("##" + react_type + "##") + ", 4)";
            Oracle.Execute(sql);
        }
        sql = "SELECT COUNT (*) reactions"
            + "  FROM fac_nieuws"
            + " WHERE fac_nieuws_parentkey = " + msg_key
            + "   AND fac_nieuws_titel = " + safe.quoted_sql_upper("##" + react_type + "##")
            + "   AND fac_nieuws_omschrijving IS NULL";
        oRs = Oracle.Execute(sql);
        var result = { "success": true, "msg_key": msg_key, "react_type": react_type, "msg_count": oRs("reactions").Value, "state": !reacted };
        oRs.Close();
        break;
    }

    case "msg_delete": {
        var msg_key = getFParamInt("msg_key");
        var this_msg = msg.func_enabled(msg_key);
        user.auth_required_or_abort(this_msg.canChangeMsg);
        var sql = "DELETE FROM fac_nieuws WHERE fac_nieuws_parentkey = " + msg_key
        Oracle.Execute(sql);
        sql = "DELETE FROM fac_nieuws WHERE fac_nieuws_key = " + msg_key;
        Oracle.Execute(sql);
        result = { "success": true };
        break;
    }
}
Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
