add "hot" comment sorting

remotes/1693176582716663532/tmp_refs/heads/watchparty
Aevann1 2022-10-11 18:41:09 +02:00
parent 0ba14cb771
commit e3a3cbd358
10 changed files with 30 additions and 27 deletions

View File

@ -121,7 +121,7 @@ class User(Base):
mfa_secret = deferred(Column(String))
is_private = Column(Boolean, default=False)
stored_subscriber_count = Column(Integer, default=0)
defaultsortingcomments = Column(String, default="top")
defaultsortingcomments = Column(String, default="hot")
defaultsorting = Column(String, default="hot")
defaulttime = Column(String, default=DEFAULT_TIME_FILTER)
is_nofollow = Column(Boolean, default=False)

View File

@ -2,6 +2,7 @@ import time
from files.classes.comment import Comment
from files.classes.submission import Submission
from files.helpers.const import *
from sqlalchemy.sql import func
def apply_time_filter(t, objects, Class):
now = int(time.time())
@ -21,8 +22,12 @@ def apply_time_filter(t, objects, Class):
return objects.filter(Class.created_utc >= cutoff)
def sort_comments(sort, comments):
if sort == 'new':
if sort == 'hot':
ti = int(time.time()) + 3600
if SITE_NAME == 'rDrama': metric = Comment.realupvotes
else: metric = Comment.upvotes - Comment.downvotes
return comments.order_by(-1000000*(metric + 1)/(func.power(((ti - Comment.created_utc)/1000), 1.23)), Comment.created_utc.desc())
elif sort == 'new':
return comments.order_by(Comment.id.desc())
elif sort == 'old':
return comments.order_by(Comment.id)
@ -30,13 +35,19 @@ def sort_comments(sort, comments):
return comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc(), Comment.id.desc())
elif sort == "bottom":
return comments.order_by(Comment.upvotes - Comment.downvotes)
elif SITE_NAME == 'rDrama':
return comments.order_by(Comment.realupvotes.desc(), Comment.id.desc())
else:
return comments.order_by(Comment.downvotes - Comment.upvotes, Comment.id.desc())
def sort_posts(sort, posts):
if sort == "new":
if sort == 'hot':
ti = int(time.time()) + 3600
if SITE_NAME == 'rDrama':
return posts.order_by(-1000000*(Submission.realupvotes + 1 + Submission.comment_count/5)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc())
else:
return posts.order_by(-1000000*(Submission.upvotes - Submission.downvotes + 1)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc())
elif sort == "bump":
return posts.filter(Submission.comment_count > 1).order_by(Submission.bump_utc.desc(), Submission.created_utc.desc())
elif sort == "new":
return posts.order_by(Submission.created_utc.desc())
elif sort == "old":
return posts.order_by(Submission.created_utc)

View File

@ -69,7 +69,7 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None, sub=None):
top_comment = c
if v: defaultsortingcomments = v.defaultsortingcomments
else: defaultsortingcomments = "top"
else: defaultsortingcomments = "hot"
sort=request.values.get("sort", defaultsortingcomments)
if v:

View File

@ -115,16 +115,7 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false"
if not (v and v.shadowbanned):
posts = posts.join(Submission.author).filter(User.shadowbanned == None)
if sort == 'hot':
ti = int(time.time()) + 3600
if SITE_NAME == 'rDrama':
posts = posts.order_by(-1000000*(Submission.realupvotes + 1 + Submission.comment_count/5)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc())
else:
posts = posts.order_by(-1000000*(Submission.upvotes - Submission.downvotes + 1)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc())
elif sort == "bump":
posts = posts.filter(Submission.comment_count > 1).order_by(Submission.bump_utc.desc(), Submission.created_utc.desc())
else:
posts = sort_posts(sort, posts)
posts = sort_posts(sort, posts)
if v: size = v.frontsize or 0
else: size = 25

View File

@ -148,7 +148,7 @@ def post_id(pid, anything=None, v=None, sub=None):
if post.new or 'megathread' in post.title.lower(): defaultsortingcomments = 'new'
elif v: defaultsortingcomments = v.defaultsortingcomments
else: defaultsortingcomments = "top"
else: defaultsortingcomments = "hot"
sort = request.values.get("sort", defaultsortingcomments)
if post.club and not (v and (v.paid_dues or v.id == post.author_id)): abort(403)

View File

@ -240,7 +240,7 @@ def settings_profile_post(v):
defaultsortingcomments = request.values.get("defaultsortingcomments")
if defaultsortingcomments:
if defaultsortingcomments in {"new", "old", "controversial", "top", "bottom"}:
if defaultsortingcomments in {"new", "old", "controversial", "top", "hot", "bottom"}:
v.defaultsortingcomments = defaultsortingcomments
updated = True
else: abort(400)

View File

@ -71,7 +71,7 @@
<p>Change the default sorting for comments.</p>
<div class="input-group mb2">
<select autocomplete="off" id='defaultsortingcomments' class="form-control" form="profile-settings" name="defaultsortingcomments" onchange="post_toast(this,'/settings/profile?defaultsortingcomments='+document.getElementById('defaultsortingcomments').value)">
{% for entry in ["new", "old", "top", "bottom", "controversial"] %}
{% for entry in ["new", "old", "top", "hot", "bottom", "controversial"] %}
<option value="{{entry}}"{{' selected' if v.defaultsortingcomments==entry}}>{{entry}}</option>
{% endfor %}
</select>

View File

@ -952,6 +952,7 @@
<div class="comments-count py-3">
<div class="dropdown dropdown-actions">
<button class="btn btn-secondary dropdown-toggle" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{% if sort=="hot" %}<i class="fas fa-fire mr-1"></i>{% endif %}
{% if sort=="top" %}<i class="fas fa-arrow-alt-circle-up mr-1"></i>{% endif %}
{% if sort=="bottom" %}<i class="fas fa-arrow-alt-circle-down mr-1"></i>{% endif %}
{% if sort=="new" %}<i class="fas fa-sparkles mr-1"></i>{% endif %}
@ -960,6 +961,7 @@
{{sort | capitalize}}
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 31px, 0px);">
{% if sort != "hot" %}<a class="dropdown-item" href="?sort=hot"><i class="fas fa-fire mr-2"></i>Hot</a>{% endif %}
{% if sort != "top" %}<a class="dropdown-item" href="?sort=top"><i class="fas fa-arrow-alt-circle-up mr-2"></i>Top</a>{% endif %}
{% if sort != "bottom" %}<a class="dropdown-item" href="?sort=bottom"><i class="fas fa-arrow-alt-circle-down mr-2"></i>Bottom</a>{% endif %}
{% if sort != "new" %}<a class="dropdown-item" href="?sort=new"><i class="fas fa-sparkles mr-2"></i>New</a>{% endif %}

View File

@ -940,7 +940,7 @@ CREATE TABLE public.users (
stored_subscriber_count integer DEFAULT 0 NOT NULL,
original_username character varying(30),
customtitle character varying(1000),
defaultsorting character varying(15) DEFAULT 'hot'::character varying NOT NULL,
defaultsorting character varying(15) NOT NULL,
defaulttime character varying(5) NOT NULL,
namecolor character varying(6) NOT NULL,
titlecolor character varying(6) NOT NULL,
@ -949,7 +949,7 @@ CREATE TABLE public.users (
hidevotedon boolean DEFAULT false NOT NULL,
newtab boolean DEFAULT false NOT NULL,
flairchanged integer,
defaultsortingcomments character varying(15) DEFAULT 'top'::character varying NOT NULL,
defaultsortingcomments character varying(15) NOT NULL,
theme character varying(15) NOT NULL,
song character varying(50),
slurreplacer boolean DEFAULT true NOT NULL,
@ -2679,4 +2679,3 @@ ALTER TABLE ONLY public.comment_option_votes
--
-- PostgreSQL database dump complete
--

View File

@ -3,19 +3,19 @@ INSERT INTO public.users (username, passhash, created_utc, admin_level, over_18,
customtitleplain, theme, themecolor, reddit, css, profilecss, coins, agendaposter,
post_count, comment_count, background, verified, truecoins, cardview, profileurl, highres, last_viewed_post_notifs, last_viewed_log_notifs, pronouns, marsify, house) VALUES
('AutoJanny', '', extract(epoch from now()), 0, true, true, '', '', 0, false,
0, 'AutoJanny', '', 'hot', 'top', 'day', 'ff66ac', 'ff66ac',
0, 'AutoJanny', '', 'hot', 'hot', 'day', 'ff66ac', 'ff66ac',
'', 'dark', 'ff66ac', 'old.reddit.com', '', '', 0, 0,
0, 0, '', 'Verified', 0, false, '/i/pfps/1.webp', '/i/pfps/1.webp', 0, 0, 'clean/itup', 0, ''),
('Snappy', '', extract(epoch from now()), 0, true, true, '', '', 0, false,
0, 'Snappy', '', 'hot', 'top', 'day', '62ca56', 'e4432d',
0, 'Snappy', '', 'hot', 'hot', 'day', '62ca56', 'e4432d',
'', 'dark', '30409f', 'old.reddit.com', '', '', 0, 0,
0, 0, '', 'Verified', 0, false, '/i/pfps/2.webp', '/i/pfps/2.webp', 0, 0,'beep/boop', 0, ''),
('longpostbot', '', extract(epoch from now()), 0, true, true, '', '', 0, false,
0, 'longpostbot', '', 'hot', 'top', 'day', '62ca56', 'e4432d',
0, 'longpostbot', '', 'hot', 'hot', 'day', '62ca56', 'e4432d',
'', 'dark', '30409f', 'old.reddit.com', '', '', 0, 0,
0, 0, '', 'Verified', 0, false, '/i/pfps/3.webp', '/i/pfps/3.webp', 0, 0, 'tl/dr', 0, ''),
('zozbot', '', extract(epoch from now()), 0, true, true, '', '', 0, false,
0, 'zozbot', '', 'hot', 'top', 'day', '62ca56', 'e4432d',
0, 'zozbot', '', 'hot', 'hot', 'day', '62ca56', 'e4432d',
'', 'dark', '30409f', 'old.reddit.com', '', '', 0, 0,
0, 0, '', 'Verified', 0, false, '/i/pfps/4.webp', '/i/pfps/4.webp', 0, 0,'zoz/zle', 0, '');
--