implement changing group chat avatar

main
htylight 2023-10-06 16:44:27 +08:00
parent ea038cab9f
commit 8b1ed7da10
4 changed files with 44 additions and 7 deletions

View File

@ -116,6 +116,15 @@ async def select_full_profile(group_chat_id: str) -> Tuple[GroupChat, list[Tuple
return group_chat, res.all()
async def update_group_avatar(id: str, avatar_name: str):
session = async_session()
await session.execute(
update(GroupChat).where(GroupChat.id == id).values(avatar=avatar_name)
)
await session.commit()
await session.close()
async def update_group_name(group_chat_id: str, new_name: str):
session = async_session()
try:

View File

@ -115,6 +115,15 @@ async def update_profile_sign(id: str, sign: str):
await session.close()
async def update_profile_status(id: str, status: str):
session = async_session()
await session.execute(
update(UserProfile).where(UserProfile.user_id == id).values(status=status)
)
await session.commit()
await session.close()
async def update_profile_avatar(id: str, avatar_name: str):
session = async_session()
await session.execute(

View File

@ -1,6 +1,8 @@
from anyio import open_file
from fastapi import APIRouter
from pydantic import BaseModel
from .user_profile import Uint8List
from ..crud import group_chat_crud
from ..response_models.group_chat_response import (
BaseResponseModel,
@ -8,6 +10,7 @@ from ..response_models.group_chat_response import (
MemberNameAvatarResponse,
FullProfileResponse,
)
from ..utils import static_file
router = APIRouter(prefix="/group_chat", tags=["group_chat"])
@ -102,6 +105,19 @@ async def get_full_profile(group_chat_id: str):
}
@router.post("/change_avatar")
async def change_avatar(group_chat_id: str, file: Uint8List):
avatar_dir_path = static_file.create_avatar_dir("group_chat", "avatars")
avatar_filename = static_file.create_avatar_filename()
async with await open_file(avatar_dir_path / avatar_filename, "wb") as f:
await f.write(bytearray(file.file))
await group_chat_crud.update_group_avatar(group_chat_id, avatar_filename)
return {"code": 10300, "msg": "Update Avatar Successfully", "data": avatar_filename}
@router.post("/change_name", response_model=BaseResponseModel)
async def change_name(group_chat_id: str, new_name: str):
try:

View File

@ -1,5 +1,3 @@
from typing import Optional
from fastapi import APIRouter
from pydantic import BaseModel
from fastapi.responses import FileResponse, JSONResponse
@ -25,11 +23,12 @@ class Uint8List(BaseModel):
class ChangedProfile(BaseModel):
id: str
nickname: Optional[str] = None
location: Optional[str] = None
birthday: Optional[str] = None
gender: Optional[str] = None
sign: Optional[str] = None
nickname: str | None = None
location: str | None = None
birthday: str | None = None
gender: str | None = None
sign: str | None = None
status: str | None = None
@router.get("/my", response_model=UserProfileResponse)
@ -80,6 +79,10 @@ async def change_profile(aspect: str, changed_profile: ChangedProfile):
await user_crud.update_profile_sign(
changed_profile.id, changed_profile.sign
)
case "status":
await user_crud.update_profile_status(
changed_profile.id, changed_profile.status
)
case _:
return {"code": 10301, "msg": f"No /change/{aspect} Path"}
return {"code": 10300, "msg": f"Update {aspect} Profile Successfully"}