This repository has been archived on 2025-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
Files
beopen/server/modules/handler/tasks.py
2025-02-10 12:37:33 +00:00

101 lines
3.9 KiB
Python

import eventlet
from modules.track.logging import log
from modules.data.database import connect as db_connect
from modules.data.config import read as config_read
from modules.data.datetime import timestamp
from modules.handler import outgoing
from modules.user.content import notification
from modules.user.info import auth as auth_info
from modules.start.start import final_startup
def user_service(sio, sid):
user_id = sio.get_session(sid)['id']
db = db_connect()
db.create(None)
log("INFO", f"Starting user service for {user_id}")
while True:
eventlet.sleep(30)
user_notification_service(db, sio, sid, user_id)
def user_notification_service(db_con, sio, sid, user_id):
notifications = notification(user_id=user_id)
notifications.columns = ['notification_id','title', 'content']
username = auth_info(user_id=user_id).get()['username']
notif_queue = notifications.get_unsent()['notifications']
if notif_queue:
for notif in notif_queue:
outgoing.send_notification(sio, notif, sid, username)
db_con.cur.execute("UPDATE notifications_sent SET time_sent = ?, sent = ? WHERE user_id = ? AND notification_id = ?", (timestamp().now, True, user_id, notif['notification_id']))
db_con.commit()
def post_time_notification():
if timestamp().is_valid_time():
log("INFO", "Sending post time notifications")
post_time_limit = int(config_read('posts', 'posttimelimit'))
title = "post-" + config_read('miscellaneous', 'ServerCode')
content = f"you have {post_time_limit} minutes to post"
target = "all-" + config_read('miscellaneous', 'ServerCode')
# notifications has a sepcial code for sending notifications accross the server
# if the target is set to "all-<unique server code>" the entire server is notified
notification_data = {'title': title, 'content': content, 'target_id': target, "expire_after": post_time_limit*60}
notification().create(notification_data)
notification_created = True
log("INFO", "Sent post time notifications")
else:
notification_created = False
return notification_created
def notification_remove(db_con):
db_con.cur.execute("SELECT notification_id, time_created, expire_after FROM notifications")
rez = db_con.cur.fetchall()
if rez:
for notif in rez:
if notif[1] + notif[2] < timestamp().now:
notification(notification_id=notif[0]).delete()
def startup_notif():
server_code = config_read('miscellaneous', 'servercode')
notif_data = {'target_id': "all-"+server_code, 'title': "Server is up", 'content': "The server is now on and functioning", 'expire_after': 180}
notification().create(notif_data)
def server_service(session):
db = db_connect()
db.create(None)
log("INFO", f"Starting server background service")
while session.mode != "normal":
eventlet.sleep(1)
log("INFO", "Server mode normal, continuing startup")
final_startup(session)
startup_notif()
while True:
# keeps the service running forever
post_notification = False
post_notif_title = "post-" + config_read('miscellaneous','ServerCode')
db.cur.execute("SELECT time_created FROM notifications WHERE title=?", (post_notif_title,))
rez = db.cur.fetchall()
if rez:
for notif in rez:
if timestamp().start < notif[0] and timestamp().end > notif[0]:
post_notification = True
today_end = timestamp().end
while timestamp().now < today_end:
# keeps running this loop until the end of the day then it returns to the while above on the start of the new day
#print(f"now: {timestamp().now}")
eventlet.sleep(10)
# removes expired notifications
notification_remove(db)
if not post_notification:
post_notification = post_time_notification()