Skip to content

Commit

Permalink
Update get_dm_rooms to use the new list_direct_rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
nexy7574 committed Feb 8, 2024
1 parent 80fb594 commit 8527355
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 44 deletions.
2 changes: 1 addition & 1 deletion dev/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ printf '\nPerforming last-minute formatting...\n'
black src
isort src
git add . || true
git commit -am "Format code for release $VERSION" || true
git commit -am "[auto] Format code for release $VERSION" || true

printf '\nCreating release branch...\n'
git tag -m "Release $VERSION_P" "$VERSION"
Expand Down
19 changes: 4 additions & 15 deletions src/niobot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from .attachment import BaseAttachment
from .commands import Command, Module
from .exceptions import *
from .patches.nio__responses import DirectRoomsErrorResponse, DirectRoomsResponse
from .utils import Typing, force_await, run_blocking
from .utils.help_command import default_help_command

Expand Down Expand Up @@ -734,20 +733,10 @@ async def get_dm_rooms(
:param user: The user ID or object to get DM rooms for.
:return: A dictionary of user IDs to lists of rooms, or a list of rooms.
"""
# When https://github.com/poljar/matrix-nio/pull/451/ is merged in the next version of matrix-nio,
# this function should be changed to use Api.list_direct_rooms.
# For now, I'll just pull the code from the PR and whack it in here.
# It's ugly, but it's better than what we had before:
# https://github.com/nexy7574/niobot/blob/216509/src/niobot/client.py#L668-L701

result = await self._send(
DirectRoomsResponse,
"GET",
nio.Api._build_path(
["user", self.user_id, "account_data", "m.direct"], {"access-token", self.access_token}
),
)
if isinstance(result, DirectRoomsErrorResponse):
if not hasattr(self, "list_direct_rooms"):
raise RuntimeError("You must have matrix-nio version 0.24.0 or later to use this feature.")
result = await self.list_direct_rooms()
if isinstance(result, nio.DirectRoomsErrorResponse()):
raise GenericMatrixError("Failed to get DM rooms", response=result)
if user:
user_id = self._get_id(user)
Expand Down
31 changes: 3 additions & 28 deletions src/niobot/patches/nio__responses.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
# OUTDATED AND NOT USED ANYMORE
# Pull from https://github.com/poljar/matrix-nio/pull/451 temporarily.
from dataclasses import dataclass, field
from typing import Any, Dict, List, Union

from nio.responses import ErrorResponse, Response
from nio.responses import DirectRoomsErrorResponse, DirectRoomsResponse

__all__ = (
"DirectRoomsErrorResponse",
"DirectRoomsResponse",
)


class DirectRoomsErrorResponse(ErrorResponse):
pass


@dataclass
class DirectRoomsResponse(Response):
"""A response containing a list of direct rooms.
Attributes:
rooms (List[str]): The rooms joined by the account.
"""

rooms: Dict[str, List[str]] = field()

@classmethod
def from_dict(
cls,
parsed_dict: Dict[Any, Any],
) -> Union["DirectRoomsResponse", DirectRoomsErrorResponse]:
if parsed_dict.get("errcode") is not None:
return DirectRoomsErrorResponse.from_dict(parsed_dict)
return cls(parsed_dict)
)

0 comments on commit 8527355

Please sign in to comment.