Commit cfb630ed authored by mathieui's avatar mathieui

muctab: use more coroutines in affiliation/role code

parent 0221081b
...@@ -231,7 +231,7 @@ class MucTab(ChatTab): ...@@ -231,7 +231,7 @@ class MucTab(ChatTab):
muc.leave_groupchat(self.core.xmpp, self.jid.bare, self.own_nick, muc.leave_groupchat(self.core.xmpp, self.jid.bare, self.own_nick,
message) message)
def change_affiliation( async def change_affiliation(
self, self,
nick_or_jid: Union[str, JID], nick_or_jid: Union[str, JID],
affiliation: str, affiliation: str,
...@@ -266,30 +266,25 @@ class MucTab(ChatTab): ...@@ -266,30 +266,25 @@ class MucTab(ChatTab):
) )
return return
async def do_set_affiliation(room: JID, jid: JID, nick: Optional[str], affiliation: str, reason: str): try:
try: await self.core.xmpp['xep_0045'].set_affiliation(
await self.core.xmpp['xep_0045'].set_affiliation( self.jid.bare,
room, nick=nick,
nick=nick, jid=jid,
jid=jid, affiliation=affiliation,
affiliation=affiliation, reason=reason
reason=reason )
) self.core.information(
self.core.information( f"Affiliation of {jid} set to {affiliation} successfully",
f"Affiliation of {jid} set to {affiliation} successfully", "Info"
"Info" )
) except (IqError, IqTimeout) as exc:
except (IqError, IqTimeout) as exc: self.core.information(
self.core.information( f"Could not set affiliation '{affiliation}' for '{jid}': {exc}",
f"Could not set affiliation '{affiliation}' for '{jid}': {exc}", "Warning",
"Warning", )
)
asyncio.ensure_future(
do_set_affiliation(self.jid.bare, jid, nick, affiliation, reason)
)
def change_role(self, nick: str, role: str, reason: str = '') -> None: async def change_role(self, nick: str, role: str, reason: str = '') -> None:
""" """
Change the role of a nick Change the role of a nick
""" """
...@@ -308,15 +303,18 @@ class MucTab(ChatTab): ...@@ -308,15 +303,18 @@ class MucTab(ChatTab):
self.core.information('Invalid nick', 'Info') self.core.information('Invalid nick', 'Info')
return return
async def do_set_role(room: JID, nick: str, role: str, reason: str): try:
try: await self.core.xmpp['xep_0045'].set_role(
await self.core.xmpp['xep_0045'].set_role(room, nick, role, reason=reason) self.jid.bare, nick, role=role, reason=reason
except (IqError, IqTimeout) as e: )
self.core.information( self.core.information(
"Could not set role '%s' for '%s': %s" % (role, nick, e), f'Role of {nick} changed to {role} successfully.'
"Warning") 'Info'
)
asyncio.ensure_future(do_set_role(self.jid.bare, nick, role, reason)) except (IqError, IqTimeout) as e:
self.core.information(
"Could not set role '%s' for '%s': %s" % (role, nick, e),
"Warning")
@refresh_wrapper.conditional @refresh_wrapper.conditional
def print_info(self, nick: str) -> bool: def print_info(self, nick: str) -> bool:
...@@ -1623,7 +1621,7 @@ class MucTab(ChatTab): ...@@ -1623,7 +1621,7 @@ class MucTab(ChatTab):
self.input.refresh() self.input.refresh()
@command_args_parser.quoted(1, 1) @command_args_parser.quoted(1, 1)
def command_kick(self, args: List[str]) -> None: async def command_kick(self, args: List[str]) -> None:
""" """
/kick <nick> [reason] /kick <nick> [reason]
""" """
...@@ -1635,10 +1633,10 @@ class MucTab(ChatTab): ...@@ -1635,10 +1633,10 @@ class MucTab(ChatTab):
else: else:
reason = '' reason = ''
nick = args[0] nick = args[0]
self.change_role(nick, 'none', reason) await self.change_role(nick, 'none', reason)
@command_args_parser.quoted(1, 1) @command_args_parser.quoted(1, 1)
def command_ban(self, args: List[str]) -> None: async def command_ban(self, args: List[str]) -> None:
""" """
/ban <nick> [reason] /ban <nick> [reason]
""" """
...@@ -1647,29 +1645,27 @@ class MucTab(ChatTab): ...@@ -1647,29 +1645,27 @@ class MucTab(ChatTab):
return return
nick = args[0] nick = args[0]
msg = args[1] if len(args) == 2 else '' msg = args[1] if len(args) == 2 else ''
self.change_affiliation(nick, 'outcast', msg) await self.change_affiliation(nick, 'outcast', msg)
@command_args_parser.quoted(2, 1, ['']) @command_args_parser.quoted(2, 1, [''])
def command_role(self, args: List[str]) -> None: async def command_role(self, args: List[str]) -> None:
""" """
/role <nick> <role> [reason] /role <nick> <role> [reason]
Changes the role of a user Changes the role of a user
roles can be: none, visitor, participant, moderator roles can be: none, visitor, participant, moderator
""" """
def callback(iq: Iq) -> None:
if iq['type'] == 'error':
self.core.room_error(iq, self.jid.bare)
if args is None: if args is None:
self.core.command.help('role') self.core.command.help('role')
return return
nick, role, reason = args[0], args[1].lower(), args[2] nick, role, reason = args[0], args[1].lower(), args[2]
self.change_role(nick, role, reason) try:
await self.change_role(nick, role, reason)
except IqError as iq:
self.core.room_error(iq, self.jid.bare)
@command_args_parser.quoted(0, 2) @command_args_parser.quoted(0, 2)
def command_affiliation(self, args: List[str]) -> None: async def command_affiliation(self, args: List[str]) -> None:
""" """
/affiliation [<nick or jid> <affiliation>] /affiliation [<nick or jid> <affiliation>]
Changes the affiliation of a user Changes the affiliation of a user
...@@ -1683,7 +1679,7 @@ class MucTab(ChatTab): ...@@ -1683,7 +1679,7 @@ class MucTab(ChatTab):
# List affiliations # List affiliations
if not args: if not args:
asyncio.ensure_future(self.get_users_affiliations(room)) await self.get_users_affiliations(room)
return None return None
if len(args) != 2: if len(args) != 2:
...@@ -1692,7 +1688,7 @@ class MucTab(ChatTab): ...@@ -1692,7 +1688,7 @@ class MucTab(ChatTab):
nick, affiliation = args[0], args[1].lower() nick, affiliation = args[0], args[1].lower()
# Set affiliation # Set affiliation
self.change_affiliation(nick, affiliation) await self.change_affiliation(nick, affiliation)
async def get_users_affiliations(self, jid: JID) -> None: async def get_users_affiliations(self, jid: JID) -> None:
owners, admins, members, outcasts = await asyncio.gather( owners, admins, members, outcasts = await asyncio.gather(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment