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 f4337f5..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: @@ -76,12 +89,16 @@ def main(): MQTT_CLIENT.subscribe(MQTT_TOPIC) MQTT_CLIENT.loop_start() while True: - raw_msg = str(input(Back.RESET + Fore.RESET)) - pub_msg = f'[{user_name}] {displayed_name}: {raw_msg}' + user_input = input(Back.RESET + Fore.RESET) + raw_msg = str(user_input) 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"): + 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) else: print(Back.WHITE + Fore.RED + "Can't send empty message", end='\n')