2024-12-06 16:08:48 +00:00
|
|
|
from datetime import datetime, timezone
|
2022-05-16 09:24:37 +01:00
|
|
|
from typing import Any, Optional
|
2022-05-14 10:18:00 +01:00
|
|
|
|
|
|
|
import requests
|
2024-12-06 16:08:48 +00:00
|
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
2022-05-14 10:18:00 +01:00
|
|
|
|
2022-06-23 13:42:45 +01:00
|
|
|
from app.brm.brn import BRN
|
2022-05-14 10:18:00 +01:00
|
|
|
from app.extensions import db
|
2024-12-06 16:08:48 +00:00
|
|
|
from app.models import AbstractConfiguration
|
2024-12-06 18:02:59 +00:00
|
|
|
from app.models.types import AwareDateTime
|
2022-05-14 10:18:00 +01:00
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class Activity(db.Model): # type: ignore
|
2024-12-06 16:08:48 +00:00
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
|
|
group_id: Mapped[Optional[int]]
|
|
|
|
activity_type: Mapped[str]
|
|
|
|
text: Mapped[str]
|
2024-12-06 18:02:59 +00:00
|
|
|
added: Mapped[datetime] = mapped_column(AwareDateTime())
|
2022-05-14 10:18:00 +01:00
|
|
|
|
2022-05-16 09:24:37 +01:00
|
|
|
def __init__(self, *,
|
|
|
|
id: Optional[int] = None,
|
|
|
|
group_id: Optional[int] = None,
|
|
|
|
activity_type: str,
|
|
|
|
text: str,
|
2024-12-06 16:08:48 +00:00
|
|
|
added: Optional[datetime] = None,
|
2022-05-16 09:24:37 +01:00
|
|
|
**kwargs: Any) -> None:
|
2022-06-17 14:02:10 +01:00
|
|
|
if not isinstance(activity_type, str) or len(activity_type) > 20 or activity_type == "":
|
2022-05-14 10:18:00 +01:00
|
|
|
raise TypeError("expected string for activity type between 1 and 20 characters")
|
2022-06-17 14:02:10 +01:00
|
|
|
if not isinstance(text, str):
|
2022-05-14 10:18:00 +01:00
|
|
|
raise TypeError("expected string for text")
|
2024-12-06 16:08:48 +00:00
|
|
|
if added is None:
|
|
|
|
added = datetime.now(tz=timezone.utc)
|
2022-05-16 09:24:37 +01:00
|
|
|
super().__init__(id=id,
|
|
|
|
group_id=group_id,
|
|
|
|
activity_type=activity_type,
|
|
|
|
text=text,
|
|
|
|
added=added,
|
|
|
|
**kwargs)
|
2022-05-14 10:18:00 +01:00
|
|
|
|
|
|
|
def notify(self) -> int:
|
|
|
|
count = 0
|
|
|
|
hooks = Webhook.query.filter(
|
2022-05-16 13:29:48 +01:00
|
|
|
Webhook.destroyed.is_(None)
|
2022-05-14 10:18:00 +01:00
|
|
|
)
|
|
|
|
for hook in hooks:
|
|
|
|
hook.send(self.text)
|
|
|
|
count += 1
|
|
|
|
return count
|
|
|
|
|
|
|
|
|
|
|
|
class Webhook(AbstractConfiguration):
|
2024-12-06 16:08:48 +00:00
|
|
|
format: Mapped[str]
|
|
|
|
url: Mapped[str]
|
2022-05-14 10:18:00 +01:00
|
|
|
|
2022-06-23 13:42:45 +01:00
|
|
|
@property
|
|
|
|
def brn(self) -> BRN:
|
|
|
|
return BRN(
|
|
|
|
group_id=0,
|
|
|
|
product="notify",
|
|
|
|
provider=self.format,
|
|
|
|
resource_type="conf",
|
2023-01-21 15:15:07 +00:00
|
|
|
resource_id=str(self.id)
|
2022-06-23 13:42:45 +01:00
|
|
|
)
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
def send(self, text: str) -> None:
|
2022-05-14 10:18:00 +01:00
|
|
|
if self.format == "telegram":
|
|
|
|
data = {"text": text}
|
|
|
|
else:
|
|
|
|
# Matrix as default
|
|
|
|
data = {"body": text}
|
2023-03-12 12:28:29 +00:00
|
|
|
requests.post(self.url, json=data, timeout=30)
|