PyDrocsid/cogs

View on GitHub
integrations/reddit/models.py

Summary

Maintainability
A
0 mins
Test Coverage
from __future__ import annotations

from datetime import datetime, timedelta
from typing import Union

from discord.utils import utcnow
from sqlalchemy import BigInteger, Column, String

from PyDrocsid.database import Base, UTCDateTime, db, delete, filter_by


class RedditChannel(Base):
    __tablename__ = "reddit_channel"

    subreddit: Union[Column, str] = Column(String(32), primary_key=True)
    channel: Union[Column, int] = Column(BigInteger, primary_key=True)

    @staticmethod
    async def create(subreddit: str, channel: int) -> RedditChannel:
        row = RedditChannel(subreddit=subreddit, channel=channel)
        await db.add(row)
        return row


class RedditPost(Base):
    __tablename__ = "reddit_post"

    post_id: Union[Column, str] = Column(String(16), primary_key=True, unique=True)
    timestamp: Union[Column, datetime] = Column(UTCDateTime)

    @staticmethod
    async def create(post_id: str) -> RedditPost:
        row = RedditPost(post_id=post_id, timestamp=utcnow())
        await db.add(row)
        return row

    @staticmethod
    async def clean():
        drop_before_timestamp = utcnow() - timedelta(weeks=1)
        await db.exec(delete(RedditPost).filter(RedditPost.timestamp < drop_before_timestamp))

    @staticmethod
    async def post(post_id: str) -> bool:
        if await db.exists(filter_by(RedditPost, post_id=post_id)):
            return False

        await RedditPost.create(post_id)
        return True