mai2: rework photo uploads, relates to #67

This commit is contained in:
2024-10-06 03:47:10 -04:00
parent ed5e7dc561
commit 0cef797a8a
6 changed files with 272 additions and 42 deletions

View File

@ -1,9 +1,10 @@
from core.data.schema import BaseData, metadata
from titles.mai2.const import Mai2Constants
from uuid import uuid4
from typing import Optional, Dict, List
from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, and_
from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean, JSON, BigInteger, SmallInteger
from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean, JSON, BigInteger, SmallInteger, VARCHAR, INTEGER
from sqlalchemy.schema import ForeignKey
from sqlalchemy.sql import func, select
from sqlalchemy.engine import Row
@ -529,6 +530,22 @@ intimacy = Table(
mysql_charset="utf8mb4",
)
photo = Table( # end-of-credit memorial photos, NOT user portraits
"mai2_user_photo",
metadata,
Column("id", VARCHAR(36), primary_key=True, nullable=False),
Column(
"user",
ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"),
nullable=False,
),
Column("playlog_num", INTEGER, nullable=False),
Column("track_num", INTEGER, nullable=False),
Column("when_upload", TIMESTAMP, nullable=False, server_default=func.now()),
UniqueConstraint("user", "playlog_num", "track_num", name="mai2_user_photo_uk"),
mysql_charset="utf8mb4",
)
class Mai2ProfileData(BaseData):
async def get_all_profile_versions(self, user_id: int) -> Optional[List[Row]]:
result = await self.execute(detail.select(detail.c.user == user_id))
@ -945,6 +962,41 @@ class Mai2ProfileData(BaseData):
self.logger.error(f"Failed to update intimacy for user {user_id} and partner {partner_id}!")
async def put_user_photo(self, user_id: int, playlog_num: int, track_num: int) -> Optional[str]:
photo_id = str(uuid4())
sql = insert(photo).values(
id = photo_id,
user = user_id,
playlog_num = playlog_num,
track_num = track_num,
)
conflict = sql.on_duplicate_key_update(user = user_id)
result = await self.execute(conflict)
if result:
return photo_id
async def get_user_photo_by_id(self, photo_id: str) -> Optional[Row]:
result = await self.execute(photo.select(photo.c.id.like(photo_id)))
if result:
return result.fetchone()
async def get_user_photo_by_user_playlog_track(self, user_id: int, playlog_num: int, track_num: int) -> Optional[Row]:
result = await self.execute(photo.select(and_(and_(photo.c.user == user_id, photo.c.playlog_num == playlog_num), photo.c.track_num == track_num)))
if result:
return result.fetchone()
async def get_user_photos_by_user(self, user_id: int) -> Optional[List[Row]]:
result = await self.execute(photo.select(photo.c.user == user_id))
if result:
return result.fetchall()
async def delete_user_photo_by_id(self, photo_id: str) -> Optional[List[Row]]:
result = await self.execute(photo.delete(photo.c.id.like(photo_id)))
if not result:
self.logger.error(f"Failed to delete photo {photo_id}")
async def update_name(self, user_id: int, new_name: str) -> bool:
sql = detail.update(detail.c.user == user_id).values(
userName=new_name