2023-08-27 23:43:16 +03:00
|
|
|
import logging
|
2023-10-15 23:21:10 +03:00
|
|
|
from datetime import datetime, timedelta
|
2023-08-27 23:43:16 +03:00
|
|
|
|
2023-10-15 23:21:10 +03:00
|
|
|
from bson import json_util
|
2023-08-27 23:43:16 +03:00
|
|
|
from libbot.pyrogram.classes import PyroClient
|
|
|
|
|
|
|
|
from classes.enums import GarbageType
|
|
|
|
from classes.location import Location
|
|
|
|
from classes.pyrouser import PyroUser
|
2023-11-05 15:26:10 +02:00
|
|
|
from modules.database import col_users
|
|
|
|
from modules.database_api import col_entries
|
2023-09-25 00:47:09 +03:00
|
|
|
from modules.utils import from_utc
|
2023-08-27 23:43:16 +03:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def remind(app: PyroClient) -> None:
|
2023-09-25 00:47:09 +03:00
|
|
|
utcnow = datetime.utcnow()
|
2023-08-27 23:43:16 +03:00
|
|
|
|
2023-10-15 23:21:10 +03:00
|
|
|
logger.debug("Performing reminder lookup for %s (UTCNOW)", utcnow)
|
|
|
|
|
2023-08-27 23:43:16 +03:00
|
|
|
users = await col_users.find(
|
2023-09-25 00:47:09 +03:00
|
|
|
{"time_hour": utcnow.hour, "time_minute": utcnow.minute}
|
2023-08-27 23:43:16 +03:00
|
|
|
).to_list()
|
|
|
|
|
2023-10-15 23:21:10 +03:00
|
|
|
logger.debug(
|
|
|
|
"Found following reminders for %s (UTC NOW): %s",
|
|
|
|
utcnow,
|
|
|
|
json_util.dumps(users, indent=None),
|
|
|
|
)
|
|
|
|
|
2023-08-27 23:43:16 +03:00
|
|
|
for user_db in users:
|
2023-10-15 23:21:10 +03:00
|
|
|
logger.debug("Processing user %s...", json_util.dumps(user_db, indent=None))
|
|
|
|
|
2023-09-25 00:47:09 +03:00
|
|
|
user = await PyroUser.from_dict(**user_db)
|
2023-08-27 23:43:16 +03:00
|
|
|
|
|
|
|
if not user.enabled or user.location is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
location: Location = await app.get_location(user.location.id) # type: ignore
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
|
2023-09-25 00:47:09 +03:00
|
|
|
user_date = from_utc(
|
2023-10-15 23:21:10 +03:00
|
|
|
datetime.utcnow() + timedelta(days=user.offset),
|
2023-09-25 00:47:09 +03:00
|
|
|
user.location.timezone.zone,
|
2023-10-15 23:21:10 +03:00
|
|
|
).replace(hour=0, minute=0, second=0, microsecond=0)
|
2023-08-27 23:43:16 +03:00
|
|
|
|
|
|
|
entries = await col_entries.find(
|
|
|
|
{
|
2023-08-30 14:00:21 +03:00
|
|
|
"locations": location.id,
|
2023-10-15 23:21:10 +03:00
|
|
|
"date": user_date,
|
2023-08-27 23:43:16 +03:00
|
|
|
}
|
|
|
|
).to_list()
|
|
|
|
|
|
|
|
logger.info("Entries of %s for %s: %s", user.id, user_date, entries)
|
|
|
|
|
|
|
|
for entry in entries:
|
2023-10-15 23:21:10 +03:00
|
|
|
logger.debug(
|
|
|
|
"Sending %s notification about %s",
|
|
|
|
user.id,
|
|
|
|
json_util.dumps(entry, indent=None),
|
|
|
|
)
|
|
|
|
|
2023-08-27 23:43:16 +03:00
|
|
|
try:
|
|
|
|
garbage_type = app._(
|
|
|
|
str(GarbageType(entry["garbage_type"]).value),
|
|
|
|
"garbage_types",
|
|
|
|
locale=user.locale,
|
|
|
|
)
|
|
|
|
garbage_date = datetime.strftime(
|
|
|
|
entry["date"], app._("date", "formats", locale=user.locale)
|
|
|
|
)
|
|
|
|
|
|
|
|
await app.send_message(
|
|
|
|
user.id,
|
2023-08-29 17:32:37 +03:00
|
|
|
app._("reminder", "messages", locale=user.locale).format(
|
2023-08-27 23:43:16 +03:00
|
|
|
type=garbage_type, date=garbage_date
|
|
|
|
),
|
|
|
|
)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.warning(
|
|
|
|
"Could not send a notification to %s due to %s", user.id, exc
|
|
|
|
)
|
|
|
|
continue
|