social media crossposting tool. 3rd time's the charm
mastodon
misskey
crossposting
bluesky
1from dataclasses import dataclass, field
2from typing import TypeVar
3
4from cross.attachments import Attachment
5from cross.fragments import Fragment
6
7T = TypeVar("T", bound=Attachment)
8
9
10class AttachmentKeeper:
11 def __init__(self) -> None:
12 self._map: dict[type, Attachment] = {}
13
14 def put(self, cls: type[T], attachment: T) -> None:
15 self._map[cls] = attachment
16
17 def get(self, cls: type[T]) -> T | None:
18 instance = self._map.get(cls)
19 if instance is None:
20 return None
21 if not isinstance(instance, cls):
22 raise TypeError(f"Expected {cls.__name__}, got {type(instance).__name__}")
23 return instance
24
25
26@dataclass
27class Post:
28 id: str
29 parent_id: str | None
30 text: str # utf-8 text
31 attachments: AttachmentKeeper
32 fragments: list[Fragment] = field(default_factory=list)