Something went wrong. Try again.
a fork of EvalEx by ezylang with a handful of breaking changes
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178import loggingimport sqlite3from abc import ABC, abstractmethodfrom collections.abc import Callablefrom typing import Any, cast
from cross.post import Post, PostReffrom database.connection import DatabasePool
columns: list[str] = [ "user", "service", "identifier", "parent", "root", "reposted", "extra_data", "crossposted",]placeholders: str = ", ".join(["?" for _ in columns])column_names: str = ", ".join(columns)
class Service: def __init__(self, url: str, db: DatabasePool) -> None: self.url: str = url self.db: DatabasePool = db self.log: logging.Logger = logging.getLogger(self.__class__.__name__) # self._lock: threading.Lock = threading.Lock()
def _get_post(self, url: str, user: str, identifier: str) -> sqlite3.Row | None: cursor = self.db.get_conn().cursor() _ = cursor.execute( """ SELECT * FROM posts WHERE service = ? AND user = ? AND identifier = ? """, (url, user, identifier), ) return cast(sqlite3.Row, cursor.fetchone())
def _get_post_by_id(self, id: int) -> sqlite3.Row | None: cursor = self.db.get_conn().cursor() _ = cursor.execute("SELECT * FROM posts WHERE id = ?", (id,)) return cast(sqlite3.Row, cursor.fetchone())
def _get_mappings( self, original: int, service: str, user: str ) -> list[sqlite3.Row]: cursor = self.db.get_conn().cursor() _ = cursor.execute( """ SELECT * FROM posts AS p JOIN mappings AS m ON p.id = m.mapped WHERE m.original = ? AND p.service = ? AND p.user = ? ORDER BY p.id ASC; """, (original, service, user), ) return cursor.fetchall()
def _find_mapped_thread( self, parent: str, iservice: str, iuser: str, oservice: str, ouser: str ) -> tuple[str, str, int, int] | None: reply_data = self._get_post(iservice, iuser, parent) if not reply_data: return None
reply_mappings: list[sqlite3.Row] | None = self._get_mappings( reply_data["id"], oservice, ouser ) if not reply_mappings: return None
reply_identifier: sqlite3.Row = reply_mappings[-1] root_identifier: sqlite3.Row = reply_mappings[0]
if reply_data["root"]: root_data = self._get_post_by_id(reply_data["root"]) if not root_data: return None
root_mappings = self._get_mappings(reply_data["root"], oservice, ouser) if not root_mappings: return None root_identifier = root_mappings[0]
return ( root_identifier["identifier"], # real ids reply_identifier["identifier"], root_identifier["id"], # db ids reply_identifier["id"], )
def _insert_post(self, post_data: dict[str, Any]) -> int: values = [post_data.get(col) for col in columns] cursor = self.db.get_conn().cursor() _ = cursor.execute( f"INSERT INTO posts ({column_names}) VALUES ({placeholders}) RETURNING id", values, ) return int(cast(sqlite3.Row, cursor.fetchone())["id"])
def _insert_post_mapping(self, original: int, mapped: int): cursor = self.db.get_conn().cursor() _ = cursor.execute( "INSERT OR IGNORE INTO mappings (original, mapped) VALUES (?, ?);", (original, mapped), ) _ = cursor.execute( "INSERT OR IGNORE INTO mappings (original, mapped) VALUES (?, ?);", (mapped, original), )
def _delete_post(self, url: str, user: str, identifier: str): cursor = self.db.get_conn().cursor() _ = cursor.execute( """ DELETE FROM posts WHERE identifier = ? AND service = ? AND user = ? """, (identifier, url, user), )
def _delete_post_by_id(self, id: int): cursor = self.db.get_conn().cursor() _ = cursor.execute("DELETE FROM posts WHERE id = ?", (id,))
def _is_post_crossposted(self, url: str, user: str, identifier: str) -> bool: cursor = self.db.get_conn().cursor() row = cursor.execute( """ SELECT crossposted FROM posts WHERE service = ? AND user = ? AND identifier = ? """, (url, user, identifier), ).fetchone() return bool(row and row["crossposted"])
class OutputService(Service): def accept_post(self, post: Post): self.log.warning("NOT IMPLEMENTED (%s), accept_post %s", self.url, post.id)
def delete_post(self, post: PostRef): self.log.warning("NOT IMPLEMENTED (%s), delete_post %s", self.url, post.id)
def accept_repost(self, repost: PostRef, reposted: PostRef): self.log.warning( "NOT IMPLEMENTED (%s), accept_repost %s of %s", self.url, repost.id, reposted.id, )
def delete_repost(self, repost: PostRef): self.log.warning("NOT IMPLEMENTED (%s), delete_repost %s", self.url, repost.id)
class InputService(ABC, Service): outputs: list[OutputService] submitter: Callable[[Callable[[], None]], None]
@abstractmethod async def listen(self): pass