diff --git a/files/helpers/const.py b/files/helpers/const.py index 6c2e08823..b88be8ad5 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -34,6 +34,19 @@ ROLES={} THEMES = {"TheMotte", "dramblr", "reddit", "transparent", "win98", "dark", "light", "coffee", "tron", "4chan", "midnight"} +SORTS_COMMON = { + "top": 'fa-arrow-alt-circle-up', + "bottom": 'fa-arrow-alt-circle-down', + "new": 'fa-sparkles', + "old": 'fa-book', + "controversial": 'fa-bullhorn', + "comments": 'fa-comments' +} +SORTS_POSTS = { + "hot": "fa-fire", + "bump": "fa-arrow-up" +} +SORTS_POSTS.update(SORTS_COMMON) IMGUR_KEY = environ.get("IMGUR_KEY").strip() PUSHER_ID = environ.get("PUSHER_ID", "").strip() diff --git a/files/helpers/contentsorting.py b/files/helpers/contentsorting.py index bdb4fb1db..e4d07bea3 100644 --- a/files/helpers/contentsorting.py +++ b/files/helpers/contentsorting.py @@ -1,10 +1,15 @@ import time +from typing import Union from sqlalchemy.sql import func +from sqlalchemy.orm import Query +from files.classes.comment import Comment +from files.classes.submission import Submission from files.helpers.const import * + def apply_time_filter(objects, t, cls): now = int(time.time()) if t == 'hour': @@ -22,35 +27,37 @@ def apply_time_filter(objects, t, cls): return objects.filter(cls.created_utc >= cutoff) -def sort_objects(objects, sort, cls): +def sort_objects(objects: Query, sort: str, cls: type[Union[Comment, Submission]]): if sort == 'hot': ti = int(time.time()) + 3600 - return objects.order_by( + ordered = objects.order_by( -100000 * (cls.upvotes + 1) - / (func.power((ti - cls.created_utc) / 1000, 1.23)), - cls.created_utc.desc()) - elif sort == 'bump' and cls.__name__ == 'Submission': - return objects.filter(cls.comment_count > 1).order_by( - cls.bump_utc.desc(), cls.created_utc.desc()) - elif sort == 'comments' and cls.__name__ == 'Submission': - return objects.order_by( - cls.comment_count.desc(), cls.created_utc.desc()) + / (func.power((ti - cls.created_utc) / 1000, 1.23))) + elif sort == 'bump' and cls is Submission: + ordered = objects.filter(cls.comment_count > 1).order_by(cls.bump_utc.desc()) + elif sort == 'comments': + if cls is Submission: + ordered = objects.order_by(cls.comment_count.desc()) + elif cls is Comment: + ordered = objects.order_by(cls.descendant_count.desc()) + else: + ordered = objects elif sort == 'controversial': - return objects.order_by( + ordered = objects.order_by( (cls.upvotes + 1) / (cls.downvotes + 1) + (cls.downvotes + 1) / (cls.upvotes + 1), - cls.downvotes.desc(), cls.created_utc.desc()) + cls.downvotes.desc()) elif sort == 'top': - return objects.order_by( - cls.downvotes - cls.upvotes, cls.created_utc.desc()) + ordered = objects.order_by(cls.downvotes - cls.upvotes) elif sort == 'bottom': - return objects.order_by( - cls.upvotes - cls.downvotes, cls.created_utc.desc()) + ordered = objects.order_by(cls.upvotes - cls.downvotes) elif sort == 'old': return objects.order_by(cls.created_utc) - else: # default, or sort == 'new' - return objects.order_by(cls.created_utc.desc()) + else: + ordered = objects + ordered = ordered.order_by(cls.created_utc.desc()) + return ordered # Presently designed around files.helpers.get.get_comment_trees_eager @@ -65,6 +72,8 @@ def sort_comment_results(comments, sort): / (pow(((ti - c.created_utc) / 1000), 1.23)), DESC - c.created_utc ) + elif sort == 'comments': + key_func = lambda c: DESC - c.descendant_count elif sort == 'controversial': key_func = lambda c: ( (c.upvotes + 1) / (c.downvotes + 1) diff --git a/files/routes/settings.py b/files/routes/settings.py index 170719a94..e22d1501d 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -215,14 +215,14 @@ def settings_profile_post(v): defaultsortingcomments = request.values.get("defaultsortingcomments") if defaultsortingcomments: - if defaultsortingcomments in {"new", "old", "controversial", "top", "bottom"}: + if defaultsortingcomments in SORTS_ALL: v.defaultsortingcomments = defaultsortingcomments updated = True else: abort(400) defaultsorting = request.values.get("defaultsorting") if defaultsorting: - if defaultsorting in {"hot", "bump", "new", "old", "comments", "controversial", "top", "bottom"}: + if defaultsorting in SORTS_POSTS: v.defaultsorting = defaultsorting updated = True else: abort(400) diff --git a/files/templates/changelog.html b/files/templates/changelog.html index 3da3f88cc..59a046d59 100644 --- a/files/templates/changelog.html +++ b/files/templates/changelog.html @@ -1,4 +1,5 @@ {% extends "settings2.html" %} +{%- import 'component/sorting_time.html' as sorting_time with context -%} {% block pagetitle %}Changelog{% endblock %} @@ -35,27 +36,7 @@
- + {{sorting_time.sort_dropdown(false)}} {% endblock %} @@ -72,13 +53,9 @@ {% endif %}
-
-
- {% include "submission_listing.html" %} -
diff --git a/files/templates/component/sorting_time.html b/files/templates/component/sorting_time.html new file mode 100644 index 000000000..e6e72ee11 --- /dev/null +++ b/files/templates/component/sorting_time.html @@ -0,0 +1,12 @@ +{%- macro sort_dropdown(is_comment, extra_query) -%} + +{%- endmacro -%} diff --git a/files/templates/home.html b/files/templates/home.html index 1f59182c1..072b9f8cf 100644 --- a/files/templates/home.html +++ b/files/templates/home.html @@ -1,5 +1,5 @@ {% extends "default.html" %} - +{%- import 'component/sorting_time.html' as sorting_time with context -%} {% block desktopBanner %} {% if v and environ.get("FP") %} @@ -73,30 +73,8 @@ {% if t != "all" %}All{% endif %} - - + {% set ccmode_text = 'ccmode=' . ccmode %} + {{sorting_time.sort_dropdown(false, ccmode_text)}} {% endblock %} @@ -104,21 +82,14 @@ {% endblock %} - {% block content %} -
-
-
- {% include "submission_listing.html" %} -
- {% endblock %} {% block pagenav %} diff --git a/files/templates/home_comments.html b/files/templates/home_comments.html index 2237caf0d..bed5b8768 100644 --- a/files/templates/home_comments.html +++ b/files/templates/home_comments.html @@ -1,4 +1,5 @@ {% extends "default.html" %} +{%- import 'component/sorting_time.html' as sorting_time with context -%} {% block sortnav %}{% endblock %} @@ -30,39 +31,17 @@
- + {{sorting_time.sort_dropdown(true)}}
-
-
- {% include "comments.html" %} -
- - {% endblock %} {% block pagenav %} diff --git a/files/templates/search.html b/files/templates/search.html index 2e232a0d0..abd06a5ff 100644 --- a/files/templates/search.html +++ b/files/templates/search.html @@ -1,4 +1,5 @@ {% extends "home.html" %} +{%- import 'component/sorting_time.html' as sorting_time with context -%} {% block pagetype %}search{% endblock %} @@ -30,25 +31,8 @@ - + {% set query_text = 'q=' . query | urlencode %} + {{sorting_time.sort_dropdown(false, query_text)}} {% endif %} {% endblock %} @@ -56,11 +40,8 @@ {% block content %}
-
-
-
  • @@ -73,19 +54,16 @@
    Showing {% block listinglength %}{{listing | length}}{% endblock %} of {{total}} result{{'s' if total != 1 else ''}} for

    {{query}}

    -
- - {% if not '/users/' in request.path %}
- + {# TODO: Constantify sorts #}
@@ -132,15 +110,11 @@
-
-
- {% block listing_template %} {% include "submission_listing.html" %} {% endblock %} -
@@ -150,14 +124,11 @@ - {% endblock %} diff --git a/files/templates/settings_filters.html b/files/templates/settings_filters.html index 477542a8a..577551bd0 100644 --- a/files/templates/settings_filters.html +++ b/files/templates/settings_filters.html @@ -1,24 +1,15 @@ {% extends "settings.html" %} - {% block pagetitle %}Profile Settings - {{SITE_TITLE}}{% endblock %} - {% block content %} - -
-
-
-

Frontpage Size

-
-

Change how many posts appear on every page.

@@ -28,9 +19,7 @@ {% endfor %}
-
-
@@ -48,14 +37,12 @@

Change the default sorting for comments.

-
-
@@ -67,21 +54,17 @@

Change the default sorting for posts.

-
-
-
-

Change the default time filter for posts.

@@ -91,34 +74,24 @@ {% endfor %}
-
-
-

Tab Behaviour

-
-
-
-
- Enable if you would like to automatically open links to other pages in the site in new tabs. -
-
@@ -136,51 +109,34 @@
Enable if you would like to automatically open links to other sites in new tabs. - - - - -

Twitter Links

-
-
-
-
- Enable if you would like to automatically convert twitter.com links to nitter.net links. -
-
-
-

Reddit Links

-
-
-

Change the domain you would like to view reddit posts in.

@@ -190,28 +146,21 @@ {% endfor %}
-
-
-
-
- Enable if you would like to automatically sort reddit.com links by controversial. -
-
diff --git a/files/templates/submission.html b/files/templates/submission.html index 930dc535a..79b7b3dbb 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -1,5 +1,5 @@ {% extends "default.html" %} - +{%- import 'component/sorting_time.html' as sorting_time with context -%} {% if p.should_hide_score %} {% set ups="" %} @@ -447,27 +447,11 @@
- + {{sorting_time.sort_dropdown(true)}} + {% if comment_info and p.comment_count >= 2%} +

+			
+			{% endif %}
 		
{% if v %} diff --git a/files/templates/userpage.html b/files/templates/userpage.html index f949ebe93..04c21b064 100644 --- a/files/templates/userpage.html +++ b/files/templates/userpage.html @@ -1,5 +1,5 @@ {% extends "default.html" %} - +{%- import 'component/sorting_time.html' as sorting_time with context -%} {% block pagetype %}userpage{% endblock %} @@ -593,25 +593,7 @@
- + {{sorting_time.sort_dropdown(false)}}
{% endif %} diff --git a/files/templates/userpage_comments.html b/files/templates/userpage_comments.html index f1540505b..97429fa49 100644 --- a/files/templates/userpage_comments.html +++ b/files/templates/userpage_comments.html @@ -1,4 +1,5 @@ {% extends "userpage.html" %} +{%- import 'component/sorting_time.html' as sorting_time with context -%} {% block content %} @@ -52,23 +53,7 @@
- + {{sorting_time.sort_dropdown(true)}} {% endif %}