From e0d87557a9f119b8f3c411bf0f768e15bc2d69ae Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Tue, 4 Feb 2025 14:18:19 +0100 Subject: [PATCH] update notification --- app/cron_notification_channel.py | 48 ++++++++++++++++++++++++++++++++ app/cron_push_notifications.py | 19 ++++++++++--- 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 app/cron_notification_channel.py diff --git a/app/cron_notification_channel.py b/app/cron_notification_channel.py new file mode 100644 index 0000000..fe5c417 --- /dev/null +++ b/app/cron_notification_channel.py @@ -0,0 +1,48 @@ +from tqdm import tqdm +import asyncio +from pocketbase import PocketBase # Client also works the same + +from dotenv import load_dotenv +import os + +load_dotenv() + +pb_admin_email = os.getenv('POCKETBASE_ADMIN_EMAIL') +pb_password = os.getenv('POCKETBASE_PASSWORD') + + +pb = PocketBase('http://127.0.0.1:8090') +admin_data = pb.collection('_superusers').auth_with_password(pb_admin_email, pb_password) + + +#manually fix and fill the data for notificationChannels +#default setting is subscribed to all channels + +async def subscribe(user_id): + + try: + result = pb.collection("notificationChannels").get_full_list(query_params={"filter": f"user='{user_id}'"}) + exist = any(item.user == user_id for item in result) + + if exist == False: + pb.collection("notificationChannels").create({ + 'user': user_id, + 'earningsSurprise': True, + 'wiim': True, + }) + + except Exception as e: + print(e) + + + +async def run(): + all_users = pb.collection("users").get_full_list() + for item in tqdm(all_users): + user_id = item.id + await subscribe(user_id=user_id) + +try: + asyncio.run(run()) +except Exception as e: + print(e) \ No newline at end of file diff --git a/app/cron_push_notifications.py b/app/cron_push_notifications.py index b50ee0f..9898ec2 100644 --- a/app/cron_push_notifications.py +++ b/app/cron_push_notifications.py @@ -223,10 +223,21 @@ async def push_earnings_release(user_id): async def run(): all_users = pb.collection("users").get_full_list() for item in tqdm(all_users): - user_id = item.id - #is_pro = True if item.tier == 'Pro' else False - await push_wiim(user_id=user_id) - await push_earnings_release(user_id=user_id) + try: + user_id = item.id + #is_pro = True if item.tier == 'Pro' else False + result = pb.collection('notificationChannels').get_list(query_params={"filter": f"user='{user_id}'"}) + channels = result.items + for channel in channels: + + if channel.wiim == True: + await push_wiim(user_id=user_id) + + if channel.earnings_surprise == True: + await push_earnings_release(user_id=user_id) + + except Exception as e: + print(e) try: