diff --git a/files/classes/comment.py b/files/classes/comment.py index 85e2ad3a6a..e7c2fcc6a8 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -120,64 +120,12 @@ class Comment(Base): if notif_utc: timestamp = notif_utc elif self.created_utc: timestamp = self.created_utc else: return None - - age = int(time.time()) - timestamp - - if age < 60: - return "just now" - elif age < 3600: - minutes = int(age / 60) - return f"{minutes}m ago" - elif age < 86400: - hours = int(age / 3600) - return f"{hours}hr ago" - elif age < 2678400: - days = int(age / 86400) - return f"{days}d ago" - - now = time.gmtime() - ctd = time.gmtime(timestamp) - - months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year) - if now.tm_mday < ctd.tm_mday: - months -= 1 - - if months < 12: - return f"{months}mo ago" - else: - years = int(months / 12) - return f"{years}yr ago" + return make_age_string(timestamp) @property @lazy def edited_string(self): - - age = int(time.time()) - self.edited_utc - - if age < 60: - return "just now" - elif age < 3600: - minutes = int(age / 60) - return f"{minutes}m ago" - elif age < 86400: - hours = int(age / 3600) - return f"{hours}hr ago" - elif age < 2678400: - days = int(age / 86400) - return f"{days}d ago" - - now = time.gmtime() - ctd = time.gmtime(self.edited_utc) - - months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year) - if now.tm_mday < ctd.tm_mday: - months -= 1 - - if months < 12: - return f"{months}mo ago" - else: - years = int(months / 12) - return f"{years}yr ago" + return make_age_string(self.edited_utc) @property @lazy diff --git a/files/classes/mod_logs.py b/files/classes/mod_logs.py index 9a8bec5e4c..e5f3605081 100644 --- a/files/classes/mod_logs.py +++ b/files/classes/mod_logs.py @@ -6,6 +6,7 @@ from files.helpers.lazy import lazy from copy import deepcopy from files.helpers.const import * from files.helpers.regex import censor_slurs +from files.helpers.sorting_and_time import make_age_string class ModAction(Base): __tablename__ = "modactions" @@ -32,37 +33,10 @@ class ModAction(Base): @property @lazy def age_string(self): - - age = int(time.time()) - self.created_utc - - if age < 60: - return "just now" - elif age < 3600: - minutes = int(age / 60) - return f"{minutes}m ago" - elif age < 86400: - hours = int(age / 3600) - return f"{hours}hr ago" - elif age < 2678400: - days = int(age / 86400) - return f"{days}d ago" - - now = time.gmtime() - ctd = time.gmtime(self.created_utc) - - months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year) - if now.tm_mday < ctd.tm_mday: - months -= 1 - - if months < 12: - return f"{months}mo ago" - else: - years = int(months / 12) - return f"{years}yr ago" + return make_age_string(self.created_utc) @property def note(self): - if self.kind=="ban_user": if self.target_post: return f'for post' elif self.target_comment_id: return f'for comment' @@ -73,11 +47,8 @@ class ModAction(Base): @property @lazy def string(self): - output = ACTIONTYPES[self.kind]["str"].format(self=self, cc=CC_TITLE) - if self.note: output += f" ({self.note})" - return output @property diff --git a/files/classes/sub_logs.py b/files/classes/sub_logs.py index 0b3ddc8a42..51ecf2dae9 100644 --- a/files/classes/sub_logs.py +++ b/files/classes/sub_logs.py @@ -5,6 +5,7 @@ import time from files.helpers.lazy import lazy from files.helpers.const import * from files.helpers.regex import censor_slurs +from files.helpers.sorting_and_time import make_age_string class SubAction(Base): __tablename__ = "subactions" @@ -32,42 +33,13 @@ class SubAction(Base): @property @lazy def age_string(self): - - age = int(time.time()) - self.created_utc - - if age < 60: - return "just now" - elif age < 3600: - minutes = int(age / 60) - return f"{minutes}m ago" - elif age < 86400: - hours = int(age / 3600) - return f"{hours}hr ago" - elif age < 2678400: - days = int(age / 86400) - return f"{days}d ago" - - now = time.gmtime() - ctd = time.gmtime(self.created_utc) - - months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year) - if now.tm_mday < ctd.tm_mday: - months -= 1 - - if months < 12: - return f"{months}mo ago" - else: - years = int(months / 12) - return f"{years}yr ago" + return make_age_string(self.created_utc) @property @lazy def string(self): - output = ACTIONTYPES[self.kind]["str"].format(self=self, cc=CC_TITLE) - if self._note: output += f" ({self._note})" - return output @property diff --git a/files/classes/submission.py b/files/classes/submission.py index 4b09fd004c..90702b80c6 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -9,6 +9,7 @@ from files.__main__ import Base from files.helpers.const import * from files.helpers.regex import * from files.helpers.lazy import lazy +from files.helpers.sorting_and_time import make_age_string from .flags import Flag from .comment import Comment, normalize_urls_runtime from .saves import SaveRelationship @@ -100,64 +101,12 @@ class Submission(Base): @property @lazy def age_string(self): - - age = int(time.time()) - self.created_utc - - if age < 60: - return "just now" - elif age < 3600: - minutes = int(age / 60) - return f"{minutes}m ago" - elif age < 86400: - hours = int(age / 3600) - return f"{hours}hr ago" - elif age < 2678400: - days = int(age / 86400) - return f"{days}d ago" - - now = time.gmtime() - ctd = time.gmtime(self.created_utc) - - months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year) - if now.tm_mday < ctd.tm_mday: - months -= 1 - - if months < 12: - return f"{months}mo ago" - else: - years = int(months / 12) - return f"{years}yr ago" + return make_age_string(self.created_utc) @property @lazy def edited_string(self): - - age = int(time.time()) - self.edited_utc - - if age < 60: - return "just now" - elif age < 3600: - minutes = int(age / 60) - return f"{minutes}m ago" - elif age < 86400: - hours = int(age / 3600) - return f"{hours}hr ago" - elif age < 2678400: - days = int(age / 86400) - return f"{days}d ago" - - now = time.gmtime() - ctd = time.gmtime(self.edited_utc) - months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year) - if now.tm_mday < ctd.tm_mday: - months -= 1 - - if months < 12: - return f"{months}mo ago" - else: - years = int(months / 12) - return f"{years}yr ago" - + return make_age_string(self.edited_utc) @property @lazy diff --git a/files/classes/views.py b/files/classes/views.py index b3007003de..2d8d1fee37 100644 --- a/files/classes/views.py +++ b/files/classes/views.py @@ -4,6 +4,8 @@ from files.__main__ import Base from files.helpers.lazy import * import time +from files.helpers.sorting_and_time import make_age_string + class ViewerRelationship(Base): __tablename__ = "viewers" @@ -26,36 +28,9 @@ class ViewerRelationship(Base): @property @lazy def last_view_since(self): - return int(time.time()) - self.last_view_utc @property @lazy def last_view_string(self): - - age = self.last_view_since - - if age < 60: - return "just now" - elif age < 3600: - minutes = int(age / 60) - return f"{minutes}m ago" - elif age < 86400: - hours = int(age / 3600) - return f"{hours}hr ago" - elif age < 2678400: - days = int(age / 86400) - return f"{days}d ago" - - now = time.gmtime() - ctd = time.gmtime(self.last_view_utc) - - months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year) - if now.tm_mday < ctd.tm_mday: - months -= 1 - - if months < 12: - return f"{months}mo ago" - else: - years = int(months / 12) - return f"{years}yr ago" + return make_age_string(self.last_view_since) diff --git a/files/helpers/jinja2.py b/files/helpers/jinja2.py index deebac7aad..836b64ca8d 100644 --- a/files/helpers/jinja2.py +++ b/files/helpers/jinja2.py @@ -29,34 +29,7 @@ def template_asset_siteimg(asset_path): @app.template_filter("timestamp") def timestamp(timestamp): - - age = int(time.time()) - timestamp - - if age < 60: - return "just now" - elif age < 3600: - minutes = int(age / 60) - return f"{minutes}m ago" - elif age < 86400: - hours = int(age / 3600) - return f"{hours}hr ago" - elif age < 2678400: - days = int(age / 86400) - return f"{days}d ago" - - now = time.gmtime() - ctd = time.gmtime(timestamp) - - months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year) - if now.tm_mday < ctd.tm_mday: - months -= 1 - - if months < 12: - return f"{months}mo ago" - else: - years = int(months / 12) - return f"{years}yr ago" - + return make_age_string(timestamp) @app.context_processor def inject_constants(): diff --git a/files/helpers/sorting_and_time.py b/files/helpers/sorting_and_time.py index 0dc6691e39..5895aa4e50 100644 --- a/files/helpers/sorting_and_time.py +++ b/files/helpers/sorting_and_time.py @@ -1,4 +1,5 @@ import time +from typing import Optional from files.helpers.const import * from sqlalchemy.sql import func @@ -44,3 +45,30 @@ def sort_objects(sort, objects, cls, include_shadowbanned=False): return objects.order_by(cls.upvotes - cls.downvotes, cls.created_utc.desc()) else: return objects.order_by(cls.downvotes - cls.upvotes, cls.created_utc.desc()) + +def make_age_string(compare:Optional[int]) -> str: + if not compare: return "never" + age = int(time.time()) - compare + + if age < 60: + return "just now" + elif age < 3600: + minutes = int(age / 60) + return f"{minutes}m ago" + elif age < 86400: + hours = int(age / 3600) + return f"{hours}hr ago" + elif age < 2678400: + days = int(age / 86400) + return f"{days}d ago" + + now = time.gmtime() + ctd = time.gmtime(compare) + months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year) + if now.tm_mday < ctd.tm_mday: + months -= 1 + if months < 12: + return f"{months}mo ago" + else: + years = int(months / 12) + return f"{years}yr ago"