feat(relationship): implement relationship(friends, blocks) api (close #6)
This commit is contained in:
@@ -8,6 +8,7 @@ from .beatmapset import (
|
||||
BeatmapsetResp as BeatmapsetResp,
|
||||
)
|
||||
from .legacy import LegacyOAuthToken, LegacyUserStatistics
|
||||
from .relationship import Relationship, RelationshipResp, RelationshipType
|
||||
from .team import Team, TeamMember
|
||||
from .user import (
|
||||
DailyChallengeStats,
|
||||
@@ -53,6 +54,9 @@ __all__ = [
|
||||
"LegacyUserStatistics",
|
||||
"OAuthToken",
|
||||
"RankHistory",
|
||||
"Relationship",
|
||||
"RelationshipResp",
|
||||
"RelationshipType",
|
||||
"Team",
|
||||
"TeamMember",
|
||||
"User",
|
||||
|
||||
62
app/database/relationship.py
Normal file
62
app/database/relationship.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from enum import Enum
|
||||
|
||||
from .user import User
|
||||
|
||||
from pydantic import BaseModel
|
||||
from sqlmodel import (
|
||||
Field,
|
||||
Relationship as SQLRelationship,
|
||||
SQLModel,
|
||||
select,
|
||||
)
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
|
||||
class RelationshipType(str, Enum):
|
||||
FOLLOW = "Friend"
|
||||
BLOCK = "Block"
|
||||
|
||||
|
||||
class Relationship(SQLModel, table=True):
|
||||
__tablename__ = "relationship" # pyright: ignore[reportAssignmentType]
|
||||
user_id: int = Field(
|
||||
default=None, foreign_key="users.id", primary_key=True, index=True
|
||||
)
|
||||
target_id: int = Field(
|
||||
default=None, foreign_key="users.id", primary_key=True, index=True
|
||||
)
|
||||
type: RelationshipType = Field(default=RelationshipType.FOLLOW, nullable=False)
|
||||
target: "User" = SQLRelationship(
|
||||
sa_relationship_kwargs={"foreign_keys": "[Relationship.target_id]"}
|
||||
)
|
||||
|
||||
|
||||
class RelationshipResp(BaseModel):
|
||||
target_id: int
|
||||
# FIXME: target: User
|
||||
mutual: bool = False
|
||||
type: RelationshipType
|
||||
|
||||
@classmethod
|
||||
async def from_db(
|
||||
cls, session: AsyncSession, relationship: Relationship
|
||||
) -> "RelationshipResp":
|
||||
target_relationship = (
|
||||
await session.exec(
|
||||
select(Relationship).where(
|
||||
Relationship.user_id == relationship.target_id,
|
||||
Relationship.target_id == relationship.user_id,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
mutual = bool(
|
||||
target_relationship is not None
|
||||
and relationship.type == RelationshipType.FOLLOW
|
||||
and target_relationship.type == RelationshipType.FOLLOW
|
||||
)
|
||||
return cls(
|
||||
target_id=relationship.target_id,
|
||||
# target=relationship.target,
|
||||
mutual=mutual,
|
||||
type=relationship.type,
|
||||
)
|
||||
@@ -65,7 +65,7 @@ class Score(ScoreBase, table=True):
|
||||
nkatu: int = Field(exclude=True)
|
||||
nlarge_tick_miss: int | None = Field(default=None, exclude=True)
|
||||
nslider_tail_hit: int | None = Field(default=None, exclude=True)
|
||||
gamemode: GameMode = Field(index=True, alias="ruleset_id")
|
||||
gamemode: GameMode = Field(index=True)
|
||||
|
||||
# optional
|
||||
beatmap: "Beatmap" = Relationship()
|
||||
|
||||
Reference in New Issue
Block a user