From f6b6997f6136d7b700acb7394771c1ef889ecaa7 Mon Sep 17 00:00:00 2001 From: TLSM Date: Mon, 20 Jun 2022 16:25:03 -0400 Subject: [PATCH] Persist activity timestamp to users. --- files/classes/user.py | 14 ++++++++++++++ files/helpers/const.py | 1 + files/helpers/wrappers.py | 10 ++++++++-- files/routes/admin.py | 4 ++-- files/templates/userpage.html | 8 ++++++++ schema.sql | 3 ++- 6 files changed, 35 insertions(+), 5 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index aaa750e18..f7997ef1d 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -70,6 +70,7 @@ class User(Base): received_award_count = Column(Integer, default=0) created_utc = Column(Integer) admin_level = Column(Integer, default=0) + last_active = Column(Integer, default=0, nullable=False) coins_spent = Column(Integer, default=0) lootboxes_bought = Column(Integer, default=0) agendaposter = Column(Integer, default=0) @@ -198,6 +199,12 @@ class User(Base): return time.strftime("%d %b %Y", time.gmtime(self.created_utc)) + @property + @lazy + def last_active_date(self): + if self.last_active == 0: + return "never" + return str(time.strftime("%d %b %Y", time.gmtime(self.last_active))) @property @lazy @@ -673,6 +680,13 @@ class User(Base): def created_datetime(self): return str(time.strftime("%d/%B/%Y %H:%M:%S UTC", time.gmtime(self.created_utc))) + @property + @lazy + def last_active_datetime(self): + if self.last_active == 0: + return "never" + return str(time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime(self.last_active))) + @lazy def subscribed_idlist(self, page=1): posts = g.db.query(Subscription.submission_id).filter_by(user_id=self.id).all() diff --git a/files/helpers/const.py b/files/helpers/const.py index 2a174f093..12f250356 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -143,6 +143,7 @@ HOLE_COST = 0 HOLE_INACTIVITY_DELETION = False PIN_LIMIT = 3 POST_RATE_LIMIT = '1/second;2/minute;10/hour;50/day' +LOGGEDIN_ACTIVE_TIME = 15 * 60 NOTIFICATIONS_ID = 1 AUTOJANNY_ID = 2 diff --git a/files/helpers/wrappers.py b/files/helpers/wrappers.py index 3c750939d..600ff69b0 100644 --- a/files/helpers/wrappers.py +++ b/files/helpers/wrappers.py @@ -53,15 +53,21 @@ def get_logged_in_user(): if v: if session["session_id"] in loggedout: del loggedout[session["session_id"]] loggedin[v.id] = timestamp + # Check against last_active + ACTIVE_TIME to reduce frequency of + # UPDATEs in exchange for a ±ACTIVE_TIME margin of error. + if (v.last_active + LOGGEDIN_ACTIVE_TIME) < timestamp: + v.last_active = timestamp + g.db.add(v) + g.db.commit() else: ua = str(user_agents.parse(g.agent)) if not ua.startswith('Spider') and 'bot' not in ua.lower(): loggedout[session["session_id"]] = (timestamp, ua) - g.loggedin_counter = len([x for x in loggedin.values() if timestamp-x<15*60]) + g.loggedin_counter = len([x for x in loggedin.values() if timestamp-x < LOGGEDIN_ACTIVE_TIME]) cache.set(f'{SITE}_loggedin', loggedin) - g.loggedout_counter = len([x for x in loggedout.values() if timestamp-x[0]<15*60]) + g.loggedout_counter = len([x for x in loggedout.values() if timestamp-x[0] < LOGGEDIN_ACTIVE_TIME]) cache.set(f'{SITE}_loggedout', loggedout) g.v = v diff --git a/files/routes/admin.py b/files/routes/admin.py index 929d0d594..4a592676b 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -63,14 +63,14 @@ def kippy(v): @app.get('/admin/loggedin') @admin_level_required(2) def loggedin_list(v): - ids = [x for x,val in cache.get(f'{SITE}_loggedin').items() if time.time()-val<15*60] + ids = [x for x,val in cache.get(f'{SITE}_loggedin').items() if time.time()-val < LOGGEDIN_ACTIVE_TIME] users = g.db.query(User).filter(User.id.in_(ids)).order_by(User.admin_level.desc(), User.truecoins.desc()).all() return render_template("loggedin.html", v=v, users=users) @app.get('/admin/loggedout') @admin_level_required(2) def loggedout_list(v): - users = sorted([val[1] for x,val in cache.get(f'{SITE}_loggedout').items() if time.time()-val[0]<15*60]) + users = sorted([val[1] for x,val in cache.get(f'{SITE}_loggedout').items() if time.time()-val[0] < LOGGEDIN_ACTIVE_TIME]) return render_template("loggedout.html", v=v, users=users) @app.get('/admin/merge//') diff --git a/files/templates/userpage.html b/files/templates/userpage.html index c1870e4ef..60a8923a9 100644 --- a/files/templates/userpage.html +++ b/files/templates/userpage.html @@ -125,6 +125,10 @@ follows {{u.follow_count}} user{{'s' if u.follow_count != 1 else ''}}   joined {{u.created_date}} + + {% if v and v.admin_level >= 2 -%} + last active {{u.last_active_date}} + {%- endif %} {% if u.basedcount %}

Based Count: {{u.basedcount}}

{% endif %} @@ -427,6 +431,10 @@ {% endif %}
joined {{u.created_date}} + + {% if v and v.admin_level >= 2 -%} +
last active {{u.last_active_date}} + {%- endif %} {% if u.bio_html %}
{{u.bio_html | safe}}
diff --git a/schema.sql b/schema.sql index 748a1edaa..0f0dc8c51 100644 --- a/schema.sql +++ b/schema.sql @@ -679,7 +679,8 @@ CREATE TABLE public.users ( total_held_lottery_tickets integer DEFAULT 0 NOT NULL, total_lottery_winnings integer DEFAULT 0 NOT NULL, can_gamble boolean DEFAULT true NOT NULL, - offsitementions boolean DEFAULT false NOT NULL + offsitementions boolean DEFAULT false NOT NULL, + last_active integer DEFAULT 0 NOT NULL );