diff --git a/matrix/commands.py b/matrix/commands.py index 322579a..0f2be8d 100644 --- a/matrix/commands.py +++ b/matrix/commands.py @@ -99,6 +99,10 @@ class WeechatCommandParser(object): delete_parser = subparsers.add_parser("delete") delete_parser.add_argument("device_id") + name_parser = subparsers.add_parser("set-name") + name_parser.add_argument("device_id") + name_parser.add_argument("device_name", nargs="*") + return WeechatCommandParser._run_parser(parser, args) @staticmethod @@ -307,15 +311,19 @@ def hook_commands(): W.hook_command( # Command name and short description "devices", - "list or delete matrix devices", + "list, delete or rename matrix devices", # Synopsis ("list ||" - "delete "), + "delete ||" + "set-name " + ), # Description - ("device-id: device id of the device to delete"), + ("device-id: device id of the device to delete\n" + " name: new device name to set\n"), # Completions ("list ||" - "delete %(matrix_own_devices)"), + "delete %(matrix_own_devices) ||" + "set-name %(matrix_own_devices)"), # Callback "matrix_devices_command_cb", "", @@ -636,6 +644,9 @@ def matrix_devices_command_cb(data, buffer, args): server.devices() elif parsed_args.subcommand == "delete": server.delete_device(parsed_args.device_id) + elif parsed_args.subcommand == "set-name": + new_name = " ".join(parsed_args.device_name).strip("\"") + server.rename_device(parsed_args.device_id, new_name) return W.WEECHAT_RC_OK diff --git a/matrix/server.py b/matrix/server.py index 0bd96c7..174a658 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -37,6 +37,7 @@ from nio import ( KeysQueryResponse, KeysClaimResponse, DevicesResponse, + UpdateDeviceResponse, DeleteDevicesAuthResponse, DeleteDevicesResponse, TransportResponse, @@ -600,6 +601,14 @@ class MatrixServer(object): self.send_or_queue(request) return + def rename_device(self, device_id, display_name): + content = { + "display_name": display_name + } + + _, request = self.client.update_device(device_id, content) + self.send_or_queue(request) + def room_send_state(self, room_buffer, body, event_type): if room_buffer.room.encrypted: return @@ -972,6 +981,9 @@ class MatrixServer(object): elif isinstance(response, DevicesResponse): self.handle_devices_response(response) + elif isinstance(response, UpdateDeviceResponse): + self.info("Device name successfully updated") + elif isinstance(response, DeleteDevicesAuthResponse): self.handle_delete_device_auth(response)