Files
mrtuxie-quotes-module/quotes/quotes.py
2025-08-28 15:42:53 +10:00

46 lines
1.5 KiB
Python

from typing import Optional
from .data import Quote, QuoteInfo, QuotesData
from .lib import utc_now
class QuoteRegistry:
def __init__(self, data: QuotesData):
self.data = data
# TODO: For efficiency we could have a cache here
# Caching quotesinfo by community.
# Particularly efficient for autocomplete.
async def init(self):
await self.data.init()
async def get_community_quotes(self, communityid: int) -> list[QuoteInfo]:
return await QuoteInfo.fetch_where(communityid=communityid, is_deleted=False)
async def get_quoteinfo(self, quoteid: int) -> Optional[QuoteInfo]:
return await QuoteInfo.fetch(quoteid)
async def get_quote(self, quoteid: int) -> Optional[Quote]:
return await Quote.fetch(quoteid)
async def get_quote_label(self, communityid: int, label: int) -> Optional[QuoteInfo]:
results = await QuoteInfo.fetch_where(communityid=communityid, quotelabel=label, is_deleted=False)
return results[0] if results else None
async def create_quote(
self,
communityid: int,
content: str,
created_by: Optional[int] = None
) -> QuoteInfo:
quote = await Quote.create(
content=content,
communityid=communityid,
created_by=created_by,
)
info = await QuoteInfo.fetch(quote.quoteid)
assert info is not None
return info
async def delete_quote(self, quoteid: int):
await self.data.quotes.update_where(quoteid=quoteid).set(deleted_at=utc_now())