Skip to content

Commit

Permalink
名称をMediaに統一
Browse files Browse the repository at this point in the history
  • Loading branch information
sushichan044 committed Oct 2, 2024
1 parent 9666eb6 commit abdf92d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
8 changes: 4 additions & 4 deletions common/birdxplorer_common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,19 +688,19 @@ class XUser(BaseModel):


# ref: https://developer.x.com/en/docs/x-api/data-dictionary/object-model/media
XMediaType: TypeAlias = Literal["photo", "video", "animated_gif"]
MediaType: TypeAlias = Literal["photo", "video", "animated_gif"]


class XMedia(BaseModel):
class Media(BaseModel):
media_key: str

type: XMediaType
type: MediaType
url: HttpUrl
width: NonNegativeInt
height: NonNegativeInt


MediaDetails: TypeAlias = List[XMedia]
MediaDetails: TypeAlias = List[Media]


class Post(BaseModel):
Expand Down
21 changes: 13 additions & 8 deletions common/birdxplorer_common/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column, relationship
from sqlalchemy.types import CHAR, DECIMAL, JSON, Integer, String

from .models import BinaryBool, LanguageIdentifier, MediaDetails, NonNegativeInt
from .models import (
BinaryBool,
LanguageIdentifier,
Media,
MediaDetails,
MediaType,
NonNegativeInt,
)
from .models import Note as NoteModel
from .models import NoteId, NotesClassification, NotesHarmful, ParticipantId
from .models import Post as PostModel
Expand All @@ -20,8 +27,6 @@
UserEnrollment,
UserId,
UserName,
XMedia,
XMediaType,
)
from .models import XUser as XUserModel
from .settings import GlobalSettings
Expand Down Expand Up @@ -94,18 +99,18 @@ class PostMediaAssociation(Base):
__tablename__ = "post_media"

post_id: Mapped[PostId] = mapped_column(ForeignKey("posts.post_id"), primary_key=True)
media_key: Mapped[str] = mapped_column(ForeignKey("x_medias.media_key"), primary_key=True)
media_key: Mapped[str] = mapped_column(ForeignKey("media.media_key"), primary_key=True)

# このテーブルにアクセスした時点でほぼ間違いなく MediaRecord も必要なので一気に引っ張る
media: Mapped["MediaRecord"] = relationship(back_populates="post_media_association", lazy="joined")


class MediaRecord(Base):
__tablename__ = "x_medias"
__tablename__ = "media"

media_key: Mapped[str] = mapped_column(primary_key=True)

type: Mapped[XMediaType] = mapped_column(nullable=False)
type: Mapped[MediaType] = mapped_column(nullable=False)
url: Mapped[HttpUrl] = mapped_column(nullable=False)
width: Mapped[NonNegativeInt] = mapped_column(nullable=False)
height: Mapped[NonNegativeInt] = mapped_column(nullable=False)
Expand Down Expand Up @@ -204,8 +209,8 @@ def engine(self) -> Engine:
return self._engine

@classmethod
def _media_record_to_model(cls, media_record: MediaRecord) -> XMedia:
return XMedia(
def _media_record_to_model(cls, media_record: MediaRecord) -> Media:
return Media(
media_key=media_record.media_key,
type=media_record.type,
url=media_record.url,
Expand Down

0 comments on commit abdf92d

Please sign in to comment.