Skip to content

Commit

Permalink
feat: close #9
Browse files Browse the repository at this point in the history
  • Loading branch information
yupix committed Jul 13, 2022
1 parent 60d2148 commit 99fca60
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 48 deletions.
16 changes: 11 additions & 5 deletions mipac/actions/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from mipac.core.models.drive import RawFile, RawFolder
from mipac.exception import ParameterError
from mipac.http import HTTPClient, Route
from mipac.models.drive import File, Folder
from mipac.util import remove_dict_empty

if TYPE_CHECKING:
from mipac.manager.client import ClientActions
from mipac.models.drive import File, Folder

__all__ = ('DriveActions', 'FileActions', 'FolderActions')

Expand Down Expand Up @@ -53,7 +53,7 @@ async def show_file(
auth=True,
lower=True,
)
return File(RawFile(res), client=self.__client)
return self.__client._modeler.create_file_instance(RawFile(res))

async def remove_file(self, file_id: Optional[str] = None) -> bool:
"""
Expand Down Expand Up @@ -116,7 +116,10 @@ async def get_files(
res = await self.__session.request(
Route('POST', '/api/drive/files'), json=data, auth=True, lower=True
)
return [File(RawFile(i), client=self.__client) for i in res]
return [
self.__client._modeler.create_file_instance(RawFile(i))
for i in res
]


class FolderActions(AbstractAction):
Expand Down Expand Up @@ -220,7 +223,10 @@ async def get_files(
res = await self.__session.request(
Route('POST', '/api/drive/files'), json=data, auth=True, lower=True
)
return [File(RawFile(i), client=self.__client) for i in res]
return [
self.__client._modeler.create_file_instance(RawFile(i))
for i in res
]


class DriveActions(AbstractAction):
Expand Down Expand Up @@ -262,4 +268,4 @@ async def get_folders(
lower=True,
auth=True,
)
return [Folder(RawFolder(i), client=self.__client) for i in data]
return [self.__client._modeler.new_folder(RawFolder(i)) for i in data]
11 changes: 5 additions & 6 deletions mipac/actions/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

__all__ = ['NoteActions']

from mipac.models.note import Note, NoteReaction, Poll
from mipac.util import check_multi_arg, remove_dict_empty

if TYPE_CHECKING:
from mipac.client import ClientActions
from mipac.models.note import Note, NoteReaction, Poll


class NoteActions:
Expand Down Expand Up @@ -129,7 +129,6 @@ async def send(
if files:
file_ids = [file.file_id for file in files]


field = {
'visibility': visibility,
'visibleUserIds': visible_user_ids,
Expand All @@ -142,7 +141,7 @@ async def send(
'replyId': reply_id,
'renoteId': renote_id,
'channelId': channel_id,
'fileIds': files
'fileIds': files,
}
if not check_multi_arg(content, files, renote_id, poll):
raise ParameterError(
Expand All @@ -169,7 +168,7 @@ async def send(
auth=True,
lower=True,
)
return Note(RawNote(res['created_note']), client=self.__client)
return self.__client._modeler.new_note(RawNote(res['created_note']))

async def delete(self, note_id: Optional[str] = None) -> bool:
"""
Expand Down Expand Up @@ -290,7 +289,7 @@ async def get_note(self, note_id: Optional[str] = None) -> Note:
auth=True,
lower=True,
)
return Note(RawNote(res), client=self.__client)
return self.__client._modeler.new_note(RawNote(res))

async def get_replies(
self,
Expand Down Expand Up @@ -330,7 +329,7 @@ async def get_replies(
auth=True,
lower=True,
)
return [Note(RawNote(i), client=self.__client) for i in res]
return [self.__client._modeler.new_note(RawNote(i)) for i in res]

async def get_reaction(
self, reaction: str, note_id: Optional[str] = None
Expand Down
15 changes: 8 additions & 7 deletions mipac/actions/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
from mipac.exception import NotExistRequiredData, ParameterError
from mipac.http import HTTPClient, Route
from mipac.manager.note import NoteManager
from mipac.models.note import Note
from mipac.models.user import User
from mipac.util import (
check_multi_arg,
get_cache_key,
key_builder,
remove_dict_empty,
)

__all__ = ['UserActions']

if TYPE_CHECKING:
from mipac.manager.client import ClientActions
from mipac.models.note import Note
from mipac.models.user import User

__all__ = ['UserActions']


class UserActions:
Expand All @@ -42,7 +43,7 @@ async def get_me(self) -> User:
"""

res = await self.__session.request(Route('POST', '/api/i'), auth=True)
return User(RawUser(res), client=self.__client)
return self.__client._modeler.create_user_instance(RawUser(res))

@cached(ttl=10, namespace='get_user', key_builder=key_builder)
async def get(
Expand Down Expand Up @@ -76,7 +77,7 @@ async def get(
data = await self.__session.request(
Route('POST', '/api/users/show'), json=field, auth=True, lower=True
)
return User(RawUser(data), client=self.__client)
return self.__client._modeler.create_user_instance(RawUser(data))

@get_cache_key
async def fetch(
Expand Down Expand Up @@ -114,7 +115,7 @@ async def fetch(
)
old_cache = Cache(namespace='get_user')
await old_cache.delete(kwargs['cache_key'].format('get_user'))
return User(RawUser(data), client=self.__client)
return self.__client._modeler.create_user_instance(RawUser(data))

async def get_notes(
self,
Expand Down Expand Up @@ -151,7 +152,7 @@ async def get_notes(
res = await self.__session.request(
Route('POST', '/api/users/notes'), json=data, auth=True, lower=True
)
return [Note(RawNote(i), client=self.__client) for i in res]
return [self.__client._modeler.new_note(RawNote(i)) for i in res]

def get_mention(self, user: Optional[User] = None) -> str:
"""
Expand Down
4 changes: 2 additions & 2 deletions mipac/manager/admin/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from mipac.core.models.user import RawUser
from mipac.http import HTTPClient, Route
from mipac.models.user import User

if TYPE_CHECKING:
from mipac.client import ClientActions
from mipac.models.user import User


class AdminUserManager:
Expand Down Expand Up @@ -69,7 +69,7 @@ async def show_user(self, user_id: Optional[str] = None) -> User:
auth=True,
lower=True,
)
return User(RawUser(res), client=self.__client)
return self.__client._modeler.create_user_instance(RawUser(res))

async def suspend(self, user_id: Optional[str] = None) -> bool:
"""
Expand Down
6 changes: 3 additions & 3 deletions mipac/manager/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from mipac.core.models.chat import RawChat
from mipac.exception import ParameterError
from mipac.http import HTTPClient, Route
from mipac.models.chat import Chat
from mipac.util import check_multi_arg

if TYPE_CHECKING:
from mipac.manager.client import ClientActions
from mipac.models.chat import Chat

__all__ = ('ChatManager',)

Expand Down Expand Up @@ -51,7 +51,7 @@ async def get_history(self, limit: int = 100, group: bool = True):
data = await self.__session.request(
Route('POST', '/api/messaging/history'), json=args, auth=True
)
return [Chat(RawChat(d), client=self.__client) for d in data]
return [self.__client._modeler.new_chat(RawChat(d)) for d in data]

async def send(
self,
Expand Down Expand Up @@ -88,7 +88,7 @@ async def send(
auth=True,
lower=True,
)
return Chat(RawChat(res), client=self.__client)
return self.__client._modeler.new_chat(RawChat(res))

async def delete(self, message_id: Optional[str] = None) -> bool:
"""
Expand Down
4 changes: 2 additions & 2 deletions mipac/manager/follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from mipac import AbstractManager
from mipac.http import HTTPClient, Route
from mipac.models.user import FollowRequest, User

if TYPE_CHECKING:
from mipac.client import ClientActions
from mipac.models.user import FollowRequest, User

__all__ = ('FollowManager', 'FollowRequestManager')

Expand Down Expand Up @@ -92,7 +92,7 @@ async def get_all(self) -> list[FollowRequest]:
"""

return [
FollowRequest(i['follower'])
self.__client._modeler.new_follow_request(i['follower'])
for i in await self.__session.request(
Route('POST', '/api/following/requests/list'),
auth=True,
Expand Down
40 changes: 37 additions & 3 deletions mipac/manager/modeler.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from mipac import File, User
from typing import TYPE_CHECKING, Any
from mipac.core.models.chat import RawChat

from mipac.core.models.drive import RawFolder
from mipac.core.models.emoji import RawEmoji
from mipac.core.models.instance import RawInstance
from mipac.core.models.note import RawNote
from mipac.models.chat import Chat
from mipac.models.drive import File, Folder
from mipac.models.emoji import Emoji
from mipac.models.instance import Instance
from mipac.models.note import Note, NoteReaction
from mipac.models.user import FollowRequest, User

if TYPE_CHECKING:
from mipac.core import RawFile, RawUser
from mipac.core.models.reaction import RawNoteReaction
from mipac.manager.client import ClientActions


Expand All @@ -16,9 +27,32 @@ class Modeler:

def __init__(self, client: ClientActions) -> None:
self._client = client

def new_note(self, raw_note: RawNote) -> Note:
return Note(raw_note, client=self._client)

def new_instance(self, raw_instance: RawInstance) -> Instance:
return Instance(raw_instance, client=self._client)

def new_chat(self, raw_chat: RawChat) -> Chat:
return Chat(raw_chat, client=self._client)

def create_user_instance(self, raw_user: RawUser) -> User:
return User(raw_user, client=self._client)

def create_file_instance(self, raw_file: RawFile) -> File:
return File(raw_file, client=self._client)

def new_follow_request(self, raw_follow_request: Any) -> FollowRequest:
return FollowRequest(raw_follow_request)

def new_note_reaction(
self, raw_note_reaction: RawNoteReaction
) -> NoteReaction:
return NoteReaction(raw_note_reaction, client=self._client)

def new_emoji(self, raw_emoji: RawEmoji) -> Emoji:
return Emoji(raw_emoji, client=self._client)

def new_folder(self, raw_folder: RawFolder) -> Folder:
return Folder(raw_folder, client=self._client)
14 changes: 9 additions & 5 deletions mipac/manager/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

from typing import TYPE_CHECKING, Optional

from mipac import AbstractManager
from mipac.abc.manager import AbstractManager
from mipac.core.models.emoji import RawEmoji
from mipac.core.models.reaction import RawNoteReaction
from mipac.http import HTTPClient, Route
from mipac.models.emoji import Emoji
from mipac.models.note import NoteReaction
from mipac.util import remove_dict_empty

if TYPE_CHECKING:
from mipac.client import ClientActions
from mipac.models.emoji import Emoji
from mipac.models.note import NoteReaction

__all__ = 'ReactionManager'

Expand Down Expand Up @@ -77,11 +77,15 @@ async def get_reaction(
lower=True,
)
return [
NoteReaction(RawNoteReaction(i), client=self.__client) for i in res
self.__client._modeler.new_note_reaction(RawNoteReaction(i))
for i in res
]

async def get_emoji_list(self) -> list[Emoji]:
data = await self.__session.request(
Route('GET', '/api/meta'), json={'detail': False}, auth=True
)
return [Emoji(RawEmoji(i)) for i in data['emojis']]
return [
self.__client._modeler.new_emoji(RawEmoji(i))
for i in data['emojis']
]
Loading

0 comments on commit 99fca60

Please sign in to comment.