| from datetime import datetime |
| from sqlalchemy import Column, String, DateTime, Text, Integer |
| from .base import Base |
| import json |
|
|
|
|
| class WatchChannel(Base): |
| """Database model for storing watch channel subscriptions""" |
| __tablename__ = "watch_channels" |
|
|
| id = Column(String, primary_key=True, index=True) |
| resource_id = Column(String, nullable=False, index=True) |
| resource_uri = Column(String, nullable=False) |
| resource_type = Column(String, nullable=False, default="acl") |
| |
| calendar_id = Column(String, nullable=False, index=True) |
| user_id = Column(String, nullable=False, index=True) |
| |
| |
| webhook_address = Column(String, nullable=False) |
| webhook_token = Column(String, nullable=True) |
| webhook_type = Column(String, nullable=False, default="web_hook") |
| |
| |
| params = Column(Text, nullable=True) |
| |
| |
| created_at = Column(DateTime, default=datetime.utcnow, nullable=False) |
| expires_at = Column(DateTime, nullable=True) |
| last_notification_at = Column(DateTime, nullable=True) |
| |
| |
| is_active = Column(String, default="true", nullable=False) |
| notification_count = Column(Integer, default=0, nullable=False) |
|
|
| def is_expired(self) -> bool: |
| """Check if the channel has expired""" |
| if self.expires_at is None: |
| return False |
| return datetime.utcnow() > self.expires_at |
|
|
| def to_channel_dict(self): |
| """Convert to Channel schema format""" |
| return { |
| "kind": "api#channel", |
| "id": self.id, |
| "resourceId": self.resource_id, |
| "resourceUri": self.resource_uri, |
| "token": self.webhook_token, |
| "expiration": self.expires_at.isoformat() if self.expires_at else None, |
| "type": self.webhook_type, |
| "address": self.webhook_address, |
| "params": json.loads(self.params) if self.params else None |
| } |