Переделал закрепы чатов
This commit is contained in:
parent
0f462d4d39
commit
47ae518794
1 changed files with 8 additions and 45 deletions
|
@ -191,19 +191,16 @@ class ChatDAO(BaseDAO):
|
|||
|
||||
async def get_pinned_chats(self, user_id: int) -> SPinnedChats:
|
||||
|
||||
pinned_chats = (
|
||||
query = (
|
||||
select(
|
||||
func.json_build_object(
|
||||
"pinned_chats", func.json_agg(
|
||||
select(
|
||||
func.json_build_object(
|
||||
"chat_id", Chat.id,
|
||||
"chat_for", Chat.chat_for,
|
||||
"chat_name", Chat.chat_name,
|
||||
"avatar_image", Users.avatar_image,
|
||||
)
|
||||
func.json_build_object(
|
||||
"chat_id", Chat.id,
|
||||
"chat_for", Chat.chat_for,
|
||||
"chat_name", Chat.chat_name,
|
||||
"avatar_image", Users.avatar_image,
|
||||
)
|
||||
.scalar_subquery()
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -213,42 +210,8 @@ class ChatDAO(BaseDAO):
|
|||
.where(PinnedChat.user_id == user_id, Chat.visibility == True) # noqa: E712
|
||||
)
|
||||
|
||||
chats_with_descriptions = (
|
||||
select(UserChat.__table__.columns, Chat.__table__.columns)
|
||||
.select_from(UserChat)
|
||||
.join(Chat, UserChat.chat_id == Chat.id)
|
||||
.cte("chats_with_descriptions")
|
||||
)
|
||||
|
||||
chats_with_avatars = (
|
||||
select(
|
||||
chats_with_descriptions.c.chat_id,
|
||||
chats_with_descriptions.c.chat_for,
|
||||
chats_with_descriptions.c.chat_name,
|
||||
chats_with_descriptions.c.visibility,
|
||||
Users.id,
|
||||
Users.avatar_image,
|
||||
)
|
||||
.select_from(chats_with_descriptions)
|
||||
.join(Users, Users.id == chats_with_descriptions.c.user_id)
|
||||
.cte("chats_with_avatars")
|
||||
)
|
||||
|
||||
query = (
|
||||
select(
|
||||
chats_with_avatars.c.chat_id,
|
||||
chats_with_avatars.c.chat_for,
|
||||
chats_with_avatars.c.chat_name,
|
||||
chats_with_avatars.c.avatar_image,
|
||||
)
|
||||
.distinct()
|
||||
.select_from(PinnedChat)
|
||||
.join(chats_with_avatars, PinnedChat.chat_id == chats_with_avatars.c.chat_id) # noqa
|
||||
.where(chats_with_avatars.c.id == user_id, chats_with_avatars.c.visibility == True) # noqa: E712
|
||||
)
|
||||
|
||||
result = await self.session.execute(query)
|
||||
result = result.mappings().all()
|
||||
result = result.scalar_one()
|
||||
return SPinnedChats.model_validate(result)
|
||||
|
||||
async def pin_message(self, chat_id: int, message_id: int, user_id: int) -> None:
|
||||
|
@ -282,7 +245,7 @@ class ChatDAO(BaseDAO):
|
|||
(
|
||||
select(msg.message)
|
||||
.select_from(MessageAnswer)
|
||||
.join(msg, msg.id == MessageAnswer.answer_id, isouter=True)
|
||||
.join(msg, msg.id == MessageAnswer.answer_id, isouter=True) # noqa
|
||||
.where(MessageAnswer.self_id == Message.id, msg.visibility == True) # noqa: E712
|
||||
.scalar_subquery()
|
||||
).label("answer_message"),
|
||||
|
|
Loading…
Add table
Reference in a new issue