Toggle the visibility of the given comment (collapse) for the
    given user.  Return the new visibility

    :param uid: the user id for which the change applies
    :param comid: the comment id to close/open
    :param collapse: if the comment is to be closed (1) or opened (0)
    :param recid: the record id to which the comment belongs
    :return: if the comment is visible or not after the update

def toggle_comment_visibility(uid, comid, collapse, recid):
    """
    Toggle the visibility of the given comment (collapse) for the
    given user.  Return the new visibility

    :param uid: the user id for which the change applies
    :param comid: the comment id to close/open
    :param collapse: if the comment is to be closed (1) or opened (0)
    :param recid: the record id to which the comment belongs
    :return: if the comment is visible or not after the update
    """
    # We rely on the client to tell if comment should be collapsed or
    # developed, to ensure consistency between our internal state and
    # client state.  Even if not strictly necessary, we store the
    # record ID for quicker retrieval of the collapsed comments of a
    # given discussion page. To prevent unnecessary population of the
    # table, only one distinct tuple (record ID, comment ID, user ID)
    # can be inserted (due to table definition). For the same purpose
    # we also check that comment to collapse exists, and corresponds
    # to an existing record: we cannot rely on the recid found as part
    # of the URL, as no former check is done. This rule is not applied
    # when deleting an entry, as in the worst case no line would be
    # removed. For optimized retrieval of row to delete, the id_bibrec
    # column is used, though not strictly necessary.
    if collapse:
        query = """SELECT id_bibrec from "cmtRECORDCOMMENT" WHERE id=%s"""
        params = (comid,)
        res = run_sql(query, params)
        if res:
            query = """INSERT INTO "cmtCOLLAPSED" (id_bibrec, "id_cmtRECORDCOMMENT", id_user)
                              VALUES (%s, %s, %s)"""
            params = (res[0][0], comid, uid)
            run_sql(query, params)
        return True
    else:
        query = """DELETE FROM "cmtCOLLAPSED" WHERE
                      "id_cmtRECORDCOMMENT"=%s and
                      id_user=%s and
                      id_bibrec=%s"""
        params = (comid, uid, recid)
        run_sql(query, params)
        return False