commands: Allow device renaming.

This commit is contained in:
Damir Jelić 2018-10-14 11:51:03 +02:00
parent b65346d49c
commit f95f33d217
2 changed files with 27 additions and 4 deletions

View file

@ -99,6 +99,10 @@ class WeechatCommandParser(object):
delete_parser = subparsers.add_parser("delete") delete_parser = subparsers.add_parser("delete")
delete_parser.add_argument("device_id") 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) return WeechatCommandParser._run_parser(parser, args)
@staticmethod @staticmethod
@ -307,15 +311,19 @@ def hook_commands():
W.hook_command( W.hook_command(
# Command name and short description # Command name and short description
"devices", "devices",
"list or delete matrix devices", "list, delete or rename matrix devices",
# Synopsis # Synopsis
("list ||" ("list ||"
"delete <device-id>"), "delete <device-id> ||"
"set-name <name>"
),
# Description # 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 # Completions
("list ||" ("list ||"
"delete %(matrix_own_devices)"), "delete %(matrix_own_devices) ||"
"set-name %(matrix_own_devices)"),
# Callback # Callback
"matrix_devices_command_cb", "matrix_devices_command_cb",
"", "",
@ -636,6 +644,9 @@ def matrix_devices_command_cb(data, buffer, args):
server.devices() server.devices()
elif parsed_args.subcommand == "delete": elif parsed_args.subcommand == "delete":
server.delete_device(parsed_args.device_id) 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 return W.WEECHAT_RC_OK

View file

@ -37,6 +37,7 @@ from nio import (
KeysQueryResponse, KeysQueryResponse,
KeysClaimResponse, KeysClaimResponse,
DevicesResponse, DevicesResponse,
UpdateDeviceResponse,
DeleteDevicesAuthResponse, DeleteDevicesAuthResponse,
DeleteDevicesResponse, DeleteDevicesResponse,
TransportResponse, TransportResponse,
@ -600,6 +601,14 @@ class MatrixServer(object):
self.send_or_queue(request) self.send_or_queue(request)
return 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): def room_send_state(self, room_buffer, body, event_type):
if room_buffer.room.encrypted: if room_buffer.room.encrypted:
return return
@ -972,6 +981,9 @@ class MatrixServer(object):
elif isinstance(response, DevicesResponse): elif isinstance(response, DevicesResponse):
self.handle_devices_response(response) self.handle_devices_response(response)
elif isinstance(response, UpdateDeviceResponse):
self.info("Device name successfully updated")
elif isinstance(response, DeleteDevicesAuthResponse): elif isinstance(response, DeleteDevicesAuthResponse):
self.handle_delete_device_auth(response) self.handle_delete_device_auth(response)