diff --git a/files/helpers/const.py b/files/helpers/const.py index 419f9a688..e36ab1fc2 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -62,7 +62,9 @@ IS_LOCALHOST = SITE == "localhost" or SITE == "127.0.0.1" or SITE.startswith("19 if IS_LOCALHOST: SITE_FULL = 'http://' + SITE else: SITE_FULL = 'https://' + SITE -CHAT_ONLINE_CACHE_KEY = f'{SITE}_online' +LOGGED_IN_CACHE_KEY = f"{SITE}_loggedin" +LOGGED_OUT_CACHE_KEY = f"{SITE}_loggedout" +CHAT_ONLINE_CACHE_KEY = f"{SITE}_online" REDDIT_NOTIFS_CACHE_KEY = "reddit_notifications" MARSEYS_CACHE_KEY = "marseys" EMOJIS_CACHE_KEY = "emojis" diff --git a/files/routes/allroutes.py b/files/routes/allroutes.py index 5b9018430..02ad05bae 100644 --- a/files/routes/allroutes.py +++ b/files/routes/allroutes.py @@ -1,11 +1,19 @@ +import secrets + from files.helpers.const import * from files.helpers.settings import get_setting from files.helpers.cloudflare import CLOUDFLARE_AVAILABLE from files.routes.wrappers import * from files.__main__ import app +def session_init(): + if not session.get("session_id"): + session.permanent = True + session["session_id"] = secrets.token_hex(49) + @app.before_request def before_request(): + g.desires_auth = False if SITE == 'marsey.world' and request.path != '/kofi': abort(404) @@ -46,7 +54,7 @@ def before_request(): @app.after_request def after_request(response): if response.status_code < 400: - if CLOUDFLARE_AVAILABLE and CLOUDFLARE_COOKIE_VALUE and getattr(g, 'desires_auth', False): + if CLOUDFLARE_AVAILABLE and CLOUDFLARE_COOKIE_VALUE and g.desires_auth: logged_in = bool(getattr(g, 'v', None)) response.set_cookie("lo", CLOUDFLARE_COOKIE_VALUE if logged_in else '', max_age=60*60*24*365 if logged_in else 1, samesite="Lax") diff --git a/files/routes/jinja2.py b/files/routes/jinja2.py index a977ac3cc..0b6d15491 100644 --- a/files/routes/jinja2.py +++ b/files/routes/jinja2.py @@ -2,6 +2,9 @@ import time from os import environ, listdir, path +import user_agents + +from flask import g, session, has_request_context from jinja2 import pass_context from files.classes.user import User @@ -10,7 +13,6 @@ from files.helpers.const import * from files.helpers.settings import get_settings from files.helpers.sorting_and_time import make_age_string from files.routes.routehelpers import get_formkey -from files.routes.wrappers import calc_users, calc_chat_users from files.__main__ import app, cache @app.template_filter("formkey") @@ -41,6 +43,36 @@ def template_asset_siteimg(asset_path): def timestamp(timestamp): return make_age_string(timestamp) +@app.context_processor +def calc_users(): + loggedin_counter = 0 + loggedout_counter = 0 + loggedin_chat = 0 + v = getattr(g, 'v', None) if g else None + if has_request_context and g and g.desires_auth and not g.is_api_or_xhr: + loggedin = cache.get(LOGGED_IN_CACHE_KEY) or {} + loggedout = cache.get(LOGGED_OUT_CACHE_KEY) or {} + loggedin_chat = cache.get(CHAT_ONLINE_CACHE_KEY) or 0 + timestamp = int(time.time()) + + if v: + if session["session_id"] in loggedout: del loggedout[session["session_id"]] + loggedin[v.id] = timestamp + else: + ua = str(user_agents.parse(g.agent)) + if 'spider' not in ua.lower() and 'bot' not in ua.lower(): + loggedout[session["session_id"]] = (timestamp, ua) + + loggedin = {k: v for k, v in loggedin.items() if (timestamp - v) < LOGGEDIN_ACTIVE_TIME} + loggedout = {k: v for k, v in loggedout.items() if (timestamp - v[0]) < LOGGEDIN_ACTIVE_TIME} + cache.set(LOGGED_IN_CACHE_KEY, loggedin) + cache.set(LOGGED_OUT_CACHE_KEY, loggedout) + loggedin_counter = len(loggedin) + loggedout_counter = len(loggedout) + return {'loggedin_counter':loggedin_counter, + 'loggedout_counter':loggedout_counter, + 'loggedin_chat':loggedin_chat} + @app.context_processor def inject_constants(): return {"environ":environ, "SITE":SITE, "SITE_NAME":SITE_NAME, "SITE_FULL":SITE_FULL, @@ -56,8 +88,7 @@ def inject_constants(): "BADGE_THREAD":BADGE_THREAD, "SNAPPY_THREAD":SNAPPY_THREAD, "KOFI_TOKEN":KOFI_TOKEN, "KOFI_LINK":KOFI_LINK, "approved_embed_hosts":approved_embed_hosts, - "site_settings":get_settings(), "EMAIL":EMAIL, "calc_users":calc_users, "calc_chat_users":calc_chat_users, - "max": max, "min": min, "user_can_see":User.can_see, + "site_settings":get_settings(), "EMAIL":EMAIL, "max": max, "min": min, "user_can_see":User.can_see, "TELEGRAM_LINK":TELEGRAM_LINK, "EMAIL_REGEX_PATTERN":EMAIL_REGEX_PATTERN, "TRUESCORE_DONATE_MINIMUM":TRUESCORE_DONATE_MINIMUM, "DONATE_LINK":DONATE_LINK, "DONATE_SERVICE":DONATE_SERVICE, "BAN_EVASION_DOMAIN":BAN_EVASION_DOMAIN, diff --git a/files/routes/wrappers.py b/files/routes/wrappers.py index 6a01a1fbc..a7a7a5775 100644 --- a/files/routes/wrappers.py +++ b/files/routes/wrappers.py @@ -1,7 +1,4 @@ import time -import secrets - -import user_agents from flask import g, request, session from files.classes.clients import ClientAuth @@ -10,47 +7,7 @@ from files.helpers.const import * from files.helpers.get import get_account from files.helpers.settings import get_setting from files.routes.routehelpers import validate_formkey -from files.__main__ import app, cache, db_session, limiter - -def session_init(): - if not session.get("session_id"): - session.permanent = True - session["session_id"] = secrets.token_hex(49) - -def calc_users(v): - if g.is_api_or_xhr: - g.loggedin_counter = 0 - g.loggedout_counter = 0 - g.loggedin_chat = 0 - return '' - loggedin = cache.get(f'{SITE}_loggedin') or {} - loggedout = cache.get(f'{SITE}_loggedout') or {} - timestamp = int(time.time()) - - session_init() - if v: - if session["session_id"] in loggedout: del loggedout[session["session_id"]] - loggedin[v.id] = timestamp - else: - ua = str(user_agents.parse(g.agent)) - if 'spider' not in ua.lower() and 'bot' not in ua.lower(): - loggedout[session["session_id"]] = (timestamp, ua) - - loggedin = {k: v for k, v in loggedin.items() if (timestamp - v) < LOGGEDIN_ACTIVE_TIME} - loggedout = {k: v for k, v in loggedout.items() if (timestamp - v[0]) < LOGGEDIN_ACTIVE_TIME} - cache.set(f'{SITE}_loggedin', loggedin) - cache.set(f'{SITE}_loggedout', loggedout) - - g.loggedin_counter = len(loggedin) - g.loggedout_counter = len(loggedout) - return '' - -def calc_chat_users(v:Optional[User]): - if v and g.is_api_or_xhr: - g.loggedin_chat = 0 - elif not hasattr(g, 'loggedin_chat'): - g.loggedin_chat = cache.get(CHAT_ONLINE_CACHE_KEY) or 0 - return '' +from files.__main__ import app, db_session, limiter def get_logged_in_user(): if hasattr(g, 'v'): return g.v diff --git a/files/templates/header.html b/files/templates/header.html index 076c13a04..1a07804db 100644 --- a/files/templates/header.html +++ b/files/templates/header.html @@ -40,12 +40,10 @@ ' throwing shade right now', ] -%} - {{calc_users(v)}} - {{g.loggedin_counter+g.loggedout_counter}} {{VISITORS_HERE_FLAVOR|random|safe}} ({{g.loggedin_counter}} logged in) + {{loggedin_counter+loggedout_counter}} {{VISITORS_HERE_FLAVOR|random|safe}} ({{loggedin_counter}} logged in) {% endif %} {% else %} - {{calc_users(v)}} - {{g.loggedin_counter+g.loggedout_counter}} people here now ({{g.loggedin_counter}} logged in) + {{loggedin_counter+loggedout_counter}} people here now ({{loggedin_counter}} logged in) {% endif %} {% else %} @@ -176,7 +174,7 @@ - {{g.loggedin_chat}} + {{loggedin_chat}} diff --git a/files/templates/mobile_navigation_bar.html b/files/templates/mobile_navigation_bar.html index 218d07c5c..a03915614 100644 --- a/files/templates/mobile_navigation_bar.html +++ b/files/templates/mobile_navigation_bar.html @@ -1,4 +1,3 @@ -{{calc_chat_users(v)}}
@@ -47,11 +46,11 @@ {% if v %} - {% if FEATURES['CHAT'] and g.loggedin_chat >= CHAT_DISPLAY_USER_COUNT_MINIMUM -%} + {% if FEATURES['CHAT'] and loggedin_chat >= CHAT_DISPLAY_USER_COUNT_MINIMUM -%}