From b16811dd324eddb475c8d1a33500a262e6d45c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maro=C5=A1=20Kov=C3=A1=C4=8D?= Date: Sat, 12 Oct 2019 22:54:39 +0200 Subject: [PATCH 1/2] Add command with logic to change user's display name --- tmessage/auth.py | 18 ++++++++++++++++++ tmessage/cli.py | 20 ++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/tmessage/auth.py b/tmessage/auth.py index d1a49c7..192b4d8 100644 --- a/tmessage/auth.py +++ b/tmessage/auth.py @@ -56,3 +56,21 @@ def register(user_name, displayed_name, password, password_confirm): return jwt.decode(data['token'], verify=False) error = response.json() raise Exception(f'Error Registering User: {error["message"]}') + + +def change_display_name(user_name, displayed_name): + """ Change display name of a user """ + endpoint_url = f'{API_USER_URL}/changeDisplayName' + headers = { + "Content-Type": "application/json" + } + changed_user = { + "user_name": user_name, + "displayed_name": displayed_name + } + response = r.post(endpoint_url, json=changed_user, headers=headers) + if response.status_code == 200: + data = response.json() + return jwt.decode(data['token'], verify=False) + error = response.json() + raise Exception(f'Error changing the display name: {error["message"]}') diff --git a/tmessage/cli.py b/tmessage/cli.py index 8d3b389..785425d 100644 --- a/tmessage/cli.py +++ b/tmessage/cli.py @@ -94,12 +94,24 @@ def main(): MQTT_CLIENT.subscribe(MQTT_TOPIC) MQTT_CLIENT.loop_start() while True: - raw_msg = str(input(Back.RESET + Fore.RESET)) + user_input = input(Back.RESET + Fore.RESET) + raw_msg = str(user_input) pub_msg = f'[{user_name}] {displayed_name}: {raw_msg}' if raw_msg != '': - MQTT_CLIENT.publish(MQTT_TOPIC, pub_msg) - if IS_STORE: - store_messages(CURRENT_USER, raw_msg) + if raw_msg.startswith("/display-name"): + input_tokens = user_input.split() + if len(input_tokens) >= 2: + input_tokens.pop(0) + new_display_name = " ".join(input_tokens) + change_response = auth.change_display_name(user_name, new_display_name) + displayed_name = change_response["displayed_name"] + else: + print(Back.WHITE + Fore.RED + + "A new display name is missing", end='\n') + else: + MQTT_CLIENT.publish(MQTT_TOPIC, pub_msg) + if IS_STORE: + store_messages(CURRENT_USER, raw_msg) else: print(Back.WHITE + Fore.RED + "Can't send empty message", end='\n') From 724f1e386776baf1d87b3a71069fa9dfa2d568dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maro=C5=A1=20Kov=C3=A1=C4=8D?= Date: Sat, 12 Oct 2019 23:36:00 +0200 Subject: [PATCH 2/2] Code refactoring --- tmessage/cli.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tmessage/cli.py b/tmessage/cli.py index 814e27c..5c5623b 100644 --- a/tmessage/cli.py +++ b/tmessage/cli.py @@ -50,6 +50,19 @@ def on_message(client, userdata, message): store_messages(user, message) +def change_display_name(user_name, user_input): + input_tokens = user_input.split() + if len(input_tokens) >= 2: + input_tokens.pop(0) + new_display_name = " ".join(input_tokens) + change_response = auth.change_display_name(user_name, new_display_name) + print(change_response) + return change_response["displayed_name"] + else: + print(Back.WHITE + Fore.RED + + "A new display name is missing", end='\n') + + def main(): """ Register a new User or Authenticates the already registered User to send message """ try: @@ -78,19 +91,11 @@ def main(): while True: user_input = input(Back.RESET + Fore.RESET) raw_msg = str(user_input) - pub_msg = f'[{user_name}] {displayed_name}: {raw_msg}' if raw_msg != '': if raw_msg.startswith("/display-name"): - input_tokens = user_input.split() - if len(input_tokens) >= 2: - input_tokens.pop(0) - new_display_name = " ".join(input_tokens) - change_response = auth.change_display_name(user_name, new_display_name) - displayed_name = change_response["displayed_name"] - else: - print(Back.WHITE + Fore.RED + - "A new display name is missing", end='\n') + displayed_name = change_display_name(user_name, user_input) else: + pub_msg = f'[{user_name}] {displayed_name}: {raw_msg}' MQTT_CLIENT.publish(MQTT_TOPIC, pub_msg) if IS_STORE: store_messages(CURRENT_USER, raw_msg)