forked from rDrama/rDrama
move calc users to context processor (#31)
jinja more like i can't think of anything witty Co-authored-by: justcool393 <justcool393@gmail.com> Reviewed-on: rDrama/rDrama#31 Co-authored-by: justcool393 <justcool393@noreply.fsdfsd.net> Co-committed-by: justcool393 <justcool393@noreply.fsdfsd.net>master
parent
955ec104f2
commit
11059d1665
|
@ -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
|
if IS_LOCALHOST: SITE_FULL = 'http://' + SITE
|
||||||
else: SITE_FULL = 'https://' + 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"
|
REDDIT_NOTIFS_CACHE_KEY = "reddit_notifications"
|
||||||
MARSEYS_CACHE_KEY = "marseys"
|
MARSEYS_CACHE_KEY = "marseys"
|
||||||
EMOJIS_CACHE_KEY = "emojis"
|
EMOJIS_CACHE_KEY = "emojis"
|
||||||
|
|
|
@ -1,11 +1,19 @@
|
||||||
|
import secrets
|
||||||
|
|
||||||
from files.helpers.const import *
|
from files.helpers.const import *
|
||||||
from files.helpers.settings import get_setting
|
from files.helpers.settings import get_setting
|
||||||
from files.helpers.cloudflare import CLOUDFLARE_AVAILABLE
|
from files.helpers.cloudflare import CLOUDFLARE_AVAILABLE
|
||||||
from files.routes.wrappers import *
|
from files.routes.wrappers import *
|
||||||
from files.__main__ import app
|
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
|
@app.before_request
|
||||||
def before_request():
|
def before_request():
|
||||||
|
g.desires_auth = False
|
||||||
if SITE == 'marsey.world' and request.path != '/kofi':
|
if SITE == 'marsey.world' and request.path != '/kofi':
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
|
@ -46,7 +54,7 @@ def before_request():
|
||||||
@app.after_request
|
@app.after_request
|
||||||
def after_request(response):
|
def after_request(response):
|
||||||
if response.status_code < 400:
|
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))
|
logged_in = bool(getattr(g, 'v', None))
|
||||||
response.set_cookie("lo", CLOUDFLARE_COOKIE_VALUE if logged_in else '',
|
response.set_cookie("lo", CLOUDFLARE_COOKIE_VALUE if logged_in else '',
|
||||||
max_age=60*60*24*365 if logged_in else 1, samesite="Lax")
|
max_age=60*60*24*365 if logged_in else 1, samesite="Lax")
|
||||||
|
|
|
@ -2,6 +2,9 @@ import time
|
||||||
|
|
||||||
from os import environ, listdir, path
|
from os import environ, listdir, path
|
||||||
|
|
||||||
|
import user_agents
|
||||||
|
|
||||||
|
from flask import g, session, has_request_context
|
||||||
from jinja2 import pass_context
|
from jinja2 import pass_context
|
||||||
|
|
||||||
from files.classes.user import User
|
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.settings import get_settings
|
||||||
from files.helpers.sorting_and_time import make_age_string
|
from files.helpers.sorting_and_time import make_age_string
|
||||||
from files.routes.routehelpers import get_formkey
|
from files.routes.routehelpers import get_formkey
|
||||||
from files.routes.wrappers import calc_users, calc_chat_users
|
|
||||||
from files.__main__ import app, cache
|
from files.__main__ import app, cache
|
||||||
|
|
||||||
@app.template_filter("formkey")
|
@app.template_filter("formkey")
|
||||||
|
@ -41,6 +43,36 @@ def template_asset_siteimg(asset_path):
|
||||||
def timestamp(timestamp):
|
def timestamp(timestamp):
|
||||||
return make_age_string(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
|
@app.context_processor
|
||||||
def inject_constants():
|
def inject_constants():
|
||||||
return {"environ":environ, "SITE":SITE, "SITE_NAME":SITE_NAME, "SITE_FULL":SITE_FULL,
|
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,
|
"BADGE_THREAD":BADGE_THREAD, "SNAPPY_THREAD":SNAPPY_THREAD,
|
||||||
"KOFI_TOKEN":KOFI_TOKEN, "KOFI_LINK":KOFI_LINK,
|
"KOFI_TOKEN":KOFI_TOKEN, "KOFI_LINK":KOFI_LINK,
|
||||||
"approved_embed_hosts":approved_embed_hosts,
|
"approved_embed_hosts":approved_embed_hosts,
|
||||||
"site_settings":get_settings(), "EMAIL":EMAIL, "calc_users":calc_users, "calc_chat_users":calc_chat_users,
|
"site_settings":get_settings(), "EMAIL":EMAIL, "max": max, "min": min, "user_can_see":User.can_see,
|
||||||
"max": max, "min": min, "user_can_see":User.can_see,
|
|
||||||
"TELEGRAM_LINK":TELEGRAM_LINK, "EMAIL_REGEX_PATTERN":EMAIL_REGEX_PATTERN,
|
"TELEGRAM_LINK":TELEGRAM_LINK, "EMAIL_REGEX_PATTERN":EMAIL_REGEX_PATTERN,
|
||||||
"TRUESCORE_DONATE_MINIMUM":TRUESCORE_DONATE_MINIMUM,
|
"TRUESCORE_DONATE_MINIMUM":TRUESCORE_DONATE_MINIMUM,
|
||||||
"DONATE_LINK":DONATE_LINK, "DONATE_SERVICE":DONATE_SERVICE, "BAN_EVASION_DOMAIN":BAN_EVASION_DOMAIN,
|
"DONATE_LINK":DONATE_LINK, "DONATE_SERVICE":DONATE_SERVICE, "BAN_EVASION_DOMAIN":BAN_EVASION_DOMAIN,
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
import time
|
import time
|
||||||
import secrets
|
|
||||||
|
|
||||||
import user_agents
|
|
||||||
from flask import g, request, session
|
from flask import g, request, session
|
||||||
|
|
||||||
from files.classes.clients import ClientAuth
|
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.get import get_account
|
||||||
from files.helpers.settings import get_setting
|
from files.helpers.settings import get_setting
|
||||||
from files.routes.routehelpers import validate_formkey
|
from files.routes.routehelpers import validate_formkey
|
||||||
from files.__main__ import app, cache, db_session, limiter
|
from files.__main__ import app, 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 ''
|
|
||||||
|
|
||||||
def get_logged_in_user():
|
def get_logged_in_user():
|
||||||
if hasattr(g, 'v'): return g.v
|
if hasattr(g, 'v'): return g.v
|
||||||
|
|
|
@ -40,12 +40,10 @@
|
||||||
' throwing shade right now',
|
' throwing shade right now',
|
||||||
]
|
]
|
||||||
-%}
|
-%}
|
||||||
{{calc_users(v)}}
|
{{loggedin_counter+loggedout_counter}} {{VISITORS_HERE_FLAVOR|random|safe}} ({{loggedin_counter}} logged in)
|
||||||
{{g.loggedin_counter+g.loggedout_counter}} {{VISITORS_HERE_FLAVOR|random|safe}} ({{g.loggedin_counter}} logged in)
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% else %}
|
{% else %}
|
||||||
{{calc_users(v)}}
|
{{loggedin_counter+loggedout_counter}} people here now ({{loggedin_counter}} logged in)
|
||||||
{{g.loggedin_counter+g.loggedout_counter}} people here now ({{g.loggedin_counter}} logged in)
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
@ -176,7 +174,7 @@
|
||||||
<a class="nav-link position-relative" href="/chat">
|
<a class="nav-link position-relative" href="/chat">
|
||||||
<i class="fas fa-messages" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Chat"></i>
|
<i class="fas fa-messages" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Chat"></i>
|
||||||
<b class="text-lg" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Users in chat right now">
|
<b class="text-lg" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Users in chat right now">
|
||||||
{{g.loggedin_chat}}
|
{{loggedin_chat}}
|
||||||
</b>
|
</b>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
{{calc_chat_users(v)}}
|
|
||||||
<div class="container d-inline-flex d-lg-none">
|
<div class="container d-inline-flex d-lg-none">
|
||||||
<div class="row fixed-bottom bg-white border-top p-2" id="mobile-bottom-navigation-bar"
|
<div class="row fixed-bottom bg-white border-top p-2" id="mobile-bottom-navigation-bar"
|
||||||
style="z-index: 1000; bottom: 0px; transition: bottom cubic-bezier(0, 0, 0.2, 1) 220ms;">
|
style="z-index: 1000; bottom: 0px; transition: bottom cubic-bezier(0, 0, 0.2, 1) 220ms;">
|
||||||
|
@ -47,11 +46,11 @@
|
||||||
</a>
|
</a>
|
||||||
</button>
|
</button>
|
||||||
{% if v %}
|
{% 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 -%}
|
||||||
<button type="button" class="col px-0 btn btn-dead m-0 pt-0" style="background: None !important; border: None;">
|
<button type="button" class="col px-0 btn btn-dead m-0 pt-0" style="background: None !important; border: None;">
|
||||||
<a href="/chat" class="text-decoration-none">
|
<a href="/chat" class="text-decoration-none">
|
||||||
<div class="text-center {% if request.path=='/chat' %}text-primary{% else %}text-muted{% endif %}">
|
<div class="text-center {% if request.path=='/chat' %}text-primary{% else %}text-muted{% endif %}">
|
||||||
<b class="text-lg" style="padding-top:10px" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Users in chat right now">{{g.loggedin_chat}}</b>
|
<b class="text-lg" style="padding-top:10px" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Users in chat right now">{{loggedin_chat}}</b>
|
||||||
<div class="text-small">Chat</div>
|
<div class="text-small">Chat</div>
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
|
|
Loading…
Reference in New Issue