43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
import uuid
|
|
from typing import TYPE_CHECKING
|
|
|
|
from tortoise import fields
|
|
|
|
from .base import TimestampedModel
|
|
|
|
if TYPE_CHECKING:
|
|
from .channel import Channel
|
|
|
|
|
|
class Post(TimestampedModel):
|
|
message_id = fields.IntField()
|
|
text = fields.TextField()
|
|
deleted_from_channel_at = fields.DatetimeField(null=True)
|
|
published_at = fields.DatetimeField(null=True)
|
|
|
|
channel: fields.ForeignKeyRelation['Channel'] = fields.ForeignKeyField(
|
|
'models.Channel', related_name='posts', on_delete=fields.CASCADE, index=True
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
channel_id: uuid.UUID
|
|
|
|
class Meta:
|
|
table = 'post'
|
|
unique_together = (('channel_id', 'message_id'),)
|
|
|
|
@property
|
|
def url(self) -> str | None:
|
|
if self.channel.username:
|
|
return f'https://t.me/{self.channel.username}/{self.message_id}'
|
|
|
|
telegram_id = self.channel.telegram_id
|
|
if telegram_id is None:
|
|
return None
|
|
|
|
channel_id = telegram_id
|
|
if telegram_id < 0:
|
|
channel_id = -telegram_id - 1000000000000
|
|
|
|
return f'https://t.me/c/{channel_id}/{self.message_id}'
|