Full completed project
This commit is contained in:
1
server/modules/handler/__init__.py
Normal file
1
server/modules/handler/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
__all__ = ['handler','outgoing', 'tasks']
|
||||
BIN
server/modules/handler/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
server/modules/handler/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/modules/handler/__pycache__/handler.cpython-311.pyc
Normal file
BIN
server/modules/handler/__pycache__/handler.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/modules/handler/__pycache__/outgoing.cpython-311.pyc
Normal file
BIN
server/modules/handler/__pycache__/outgoing.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/modules/handler/__pycache__/tasks.cpython-311.pyc
Normal file
BIN
server/modules/handler/__pycache__/tasks.cpython-311.pyc
Normal file
Binary file not shown.
BIN
server/modules/handler/__pycache__/threading.cpython-311.pyc
Normal file
BIN
server/modules/handler/__pycache__/threading.cpython-311.pyc
Normal file
Binary file not shown.
1007
server/modules/handler/handler.py
Normal file
1007
server/modules/handler/handler.py
Normal file
File diff suppressed because it is too large
Load Diff
36
server/modules/handler/outgoing.py
Normal file
36
server/modules/handler/outgoing.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from modules.data.config import read as config_read
|
||||
|
||||
import requests
|
||||
|
||||
def post_slot(sio, sid=None):
|
||||
if sid:
|
||||
sio.emit("post_slot", room=sid)
|
||||
else:
|
||||
sio.emit("post_slot")
|
||||
|
||||
def send_ntfy(sio, info, sid, username):
|
||||
url = config_read("notifications", "ntfyUrl")
|
||||
if url == "https://ntfy.example.com":
|
||||
return
|
||||
|
||||
user_id = sio.get_session(sid)['id']
|
||||
nfty_topic = f"{username}-{user_id[:8]}"
|
||||
if url[-1] != "/":
|
||||
url = url + "/"
|
||||
print(url + ntfy_topic)
|
||||
|
||||
message = info['message'].encode(encoding='utf-8')
|
||||
title = info['title']
|
||||
|
||||
print(f"ntfy: {nfty_topic}")
|
||||
try:
|
||||
requests.post(f"{url}{ntfy_topic}", data=message, headers={"Title": title})
|
||||
except:
|
||||
log("WARN", "Notification server cannot be reached, ensure ntfy is up and that the provided url is correct")
|
||||
|
||||
def send_notification(sio, info, sid=None, username=None):
|
||||
if sid:
|
||||
send_ntfy(sio, info, sid, username)
|
||||
sio.emit("notification", info, room=sid)
|
||||
else:
|
||||
sio.emit("notification", info)
|
||||
100
server/modules/handler/tasks.py
Normal file
100
server/modules/handler/tasks.py
Normal file
@@ -0,0 +1,100 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user