Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tmessage/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Nephyx,
To adapt the code to the protected route on API (I suggested this at Haider8/tmessage-api#3), when a request to change the displayed name is made, the request's header need the Authorization argument with a value of Bearer [access_token], too.
Whenever the user receives a token, it should be saved to a variable so that it can be sent later on in the request's header.

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"]}')
27 changes: 22 additions & 5 deletions tmessage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent



def main():
""" Register a new User or Authenticates the already registered User to send message """
try:
Expand All @@ -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')
Expand Down