diff --git a/ktvmanager/lib/checker.py b/ktvmanager/lib/checker.py index baf5e58..69fba50 100644 --- a/ktvmanager/lib/checker.py +++ b/ktvmanager/lib/checker.py @@ -103,6 +103,14 @@ def validate_account() -> Tuple[Response, int]: username, stream, result["data"]["user_info"]["exp_date"] ) return jsonify({"message": "Account is valid and updated", "data": result}), 200 + + if result.get("data", {}).get("user_info", {}).get("max_connections") and data.get("max_connections") and result["data"]["user_info"]["max_connections"] != data.get("max_connections"): + from ktvmanager.lib.database import update_max_connections + update_max_connections( + username, stream, result["data"]["user_info"]["max_connections"] + ) + return jsonify({"message": "Account is valid and updated", "data": result}), 200 + return jsonify({"message": "Account is valid", "data": result}), 200 else: return jsonify({"message": "Account is invalid"}), 401 \ No newline at end of file diff --git a/ktvmanager/lib/database.py b/ktvmanager/lib/database.py index 3b8af6a..0c3e680 100644 --- a/ktvmanager/lib/database.py +++ b/ktvmanager/lib/database.py @@ -127,7 +127,7 @@ def add_account(user_id: int) -> Response: data = request.form res = single_account_check(data, get_latest_urls_from_dns()) encrypted_password = encrypt_password(data["password"]) - query = "INSERT INTO userAccounts (username, stream, streamURL, expiaryDate, password, userID) VALUES (%s, %s, %s, %s, %s, %s)" + query = "INSERT INTO userAccounts (username, stream, streamURL, expiaryDate, password, userID, maxConnections) VALUES (%s, %s, %s, %s, %s, %s, %s)" params = ( data["username"], data["stream"], @@ -135,6 +135,7 @@ def add_account(user_id: int) -> Response: res["data"]["user_info"]["exp_date"], encrypted_password, user_id, + res["data"]["user_info"]["max_connections"], ) result = _execute_query(query, params) return jsonify(result) @@ -153,6 +154,19 @@ def update_expiry_date(username: str, stream: str, expiry_date: str) -> None: _execute_query(query, params) +def update_max_connections(username: str, stream: str, max_connections: int) -> None: + """Updates the max connections of an account. + + Args: + username: The username of the account. + stream: The stream of the account. + max_connections: The new max connections value. + """ + query = "UPDATE userAccounts SET maxConnections = %s WHERE username = %s AND stream = %s" + params = (max_connections, username, stream) + _execute_query(query, params) + + def delete_account(user_id: int) -> Response: """Deletes an account for a user.