From 50b995d3a0c9f5c7f1f5707fc14d064adc9dfc60 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Wed, 25 May 2022 22:16:26 +0200 Subject: [PATCH] fds --- files/classes/user.py | 1 - files/helpers/get.py | 2 +- files/helpers/wrappers.py | 30 +++++++++++++-------------- files/routes/admin.py | 9 +++++++- files/routes/login.py | 4 ++++ files/routes/search.py | 2 +- files/routes/static.py | 2 +- files/routes/users.py | 2 +- files/templates/admin/admin_home.html | 1 + files/templates/header.html | 7 ++++++- files/templates/sidebar_rDrama.html | 6 +++++- 11 files changed, 42 insertions(+), 24 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index 8f48a37f9..bd7b62fff 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -487,7 +487,6 @@ class User(Base): User.id != self.id ).order_by(User.username).all() - data = [x for x in data] output = [] for x in data: user = x[0] diff --git a/files/helpers/get.py b/files/helpers/get.py index 1118d33e4..c015b6f2e 100644 --- a/files/helpers/get.py +++ b/files/helpers/get.py @@ -279,7 +279,7 @@ def get_domain(s): domain_list.add(new_domain) - doms = [x for x in g.db.query(BannedDomain).filter(BannedDomain.domain.in_(domain_list)).all()] + doms = g.db.query(BannedDomain).filter(BannedDomain.domain.in_(domain_list)).all() if not doms: return None diff --git a/files/helpers/wrappers.py b/files/helpers/wrappers.py index f1bc0bdef..b8b7d0357 100644 --- a/files/helpers/wrappers.py +++ b/files/helpers/wrappers.py @@ -37,28 +37,26 @@ def get_logged_in_user(): abort(403) - - if not session.get("session_id"): session.permanent = True session["session_id"] = secrets.token_hex(49) - - sessions = cache.get(f'{SITE}_sessions') or {} - - timestamp = int(time.time()) - - sessions[session["session_id"]] = (bool(v), timestamp) - + + loggedin = cache.get(f'{SITE}_loggedin') or {} + loggedout = cache.get(f'{SITE}_loggedout') or {} g.loggedin_counter = 0 g.loggedout_counter = 0 - for val in sessions.values(): - if timestamp - val[1] < 15*60: - if val[0]: g.loggedin_counter += 1 - else: g.loggedout_counter += 1 - - - cache.set(f'{SITE}_sessions', sessions) + timestamp = int(time.time()) + if v: + if session["session_id"] in loggedout: del loggedout[session["session_id"]] + loggedin[v.id] = timestamp + else: + loggedout[session["session_id"]] = timestamp + + g.loggedin_counter = len([x for x in loggedin.values() if timestamp-x<15*60]) + cache.set(f'{SITE}_loggedin', loggedin) + g.loggedout_counter = len([x for x in loggedout.values() if timestamp-x<15*60]) + cache.set(f'{SITE}_loggedout', loggedout) return v diff --git a/files/routes/admin.py b/files/routes/admin.py index fdc265b1f..367488975 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -24,6 +24,13 @@ GUMROAD_TOKEN = environ.get("GUMROAD_TOKEN", "").strip() month = datetime.now().strftime('%B') +@app.get('/admin/loggedin') +@admin_level_required(2) +def loggedin_list(v): + ids = cache.get(f'{SITE}_loggedin').keys() + users = g.db.query(User).filter(User.id.in_(ids)).all() + return render_template("agendaposters.html", v=v, users=users) + @app.get('/admin/merge//') @admin_level_required(3) @@ -444,7 +451,7 @@ def monthly(v): @auth_required def shadowbanned(v): if not (v and v.admin_level > 1): abort(404) - users = [x for x in g.db.query(User).filter(User.shadowbanned != None).order_by(User.shadowbanned).all()] + users = g.db.query(User).filter(User.shadowbanned != None).order_by(User.shadowbanned).all() return render_template("shadowbanned.html", v=v, users=users) diff --git a/files/routes/login.py b/files/routes/login.py index 9e0b95a30..f5650460f 100644 --- a/files/routes/login.py +++ b/files/routes/login.py @@ -171,6 +171,10 @@ def me(v): @auth_required def logout(v): + loggedin = cache.get(f'{SITE}_loggedin') or {} + if session.get("lo_user") in loggedin: del loggedin[session["lo_user"]] + cache.set(f'{SITE}_loggedin', loggedin) + session.pop("lo_user", None) return {"message": "Logout successful!"} diff --git a/files/routes/search.py b/files/routes/search.py index a4cb08c9a..85866440d 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -282,7 +282,7 @@ def searchusers(v): total=users.count() - users=[x for x in users.offset(25 * (page-1)).limit(26)] + users = users.offset(25 * (page-1)).limit(26).all() next_exists=(len(users)>25) users=users[:25] diff --git a/files/routes/static.py b/files/routes/static.py index 60f4dbe30..d2535d045 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -542,7 +542,7 @@ def blocks(v): @auth_required def banned(v): - users = [x for x in g.db.query(User).filter(User.is_banned > 0, User.unban_utc == 0).all()] + users = g.db.query(User).filter(User.is_banned > 0, User.unban_utc == 0).all() return render_template("banned.html", v=v, users=users) @app.get("/formatting") diff --git a/files/routes/users.py b/files/routes/users.py index 462503855..3656d9630 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -268,7 +268,7 @@ def grassed(v): @app.get("/agendaposters") @auth_required def agendaposters(v): - users = [x for x in g.db.query(User).filter(User.agendaposter > 0).order_by(User.username).all()] + users = g.db.query(User).filter(User.agendaposter > 0).order_by(User.username).all() return render_template("agendaposters.html", v=v, users=users) diff --git a/files/templates/admin/admin_home.html b/files/templates/admin/admin_home.html index e62077a2d..8e7b235e2 100644 --- a/files/templates/admin/admin_home.html +++ b/files/templates/admin/admin_home.html @@ -24,6 +24,7 @@
  • Permabanned Users
  • Users with Chud Theme
  • Currently Grassed Users
  • +
  • Currently Logged-in Users
  • Safety

    diff --git a/files/templates/header.html b/files/templates/header.html index 1b210065b..8ef507418 100644 --- a/files/templates/header.html +++ b/files/templates/header.html @@ -14,7 +14,12 @@ {% include "journoid_banner.html" %} {%- else -%} - PEOPLE HERE NOW: {{g.loggedin_counter}} logged in, {{g.loggedout_counter}} logged out + + {% if v and v.admin_level > 1 %} + PEOPLE HERE NOW: {{g.loggedin_counter}} logged in, {{g.loggedout_counter}} logged out + {% else %} + PEOPLE HERE NOW: {{g.loggedin_counter}} logged in, {{g.loggedout_counter}} logged out + {% endif %} {%- endif -%} diff --git a/files/templates/sidebar_rDrama.html b/files/templates/sidebar_rDrama.html index 70017cdfd..374119148 100644 --- a/files/templates/sidebar_rDrama.html +++ b/files/templates/sidebar_rDrama.html @@ -41,7 +41,11 @@ set VISITORS_HERE_FLAVOR = [ PEOPLE HERE NOW -

    {{g.loggedin_counter}} logged in, {{g.loggedout_counter}} logged out

    + {% if v and v.admin_level > 1 %} +

    {{g.loggedin_counter}} logged in, {{g.loggedout_counter}} logged out

    + {% else %} +

    {{g.loggedin_counter}} logged in, {{g.loggedout_counter}} logged out

    + {% endif %} {% if sub %} {% if sub.sidebar_html %}