home - popular and all lists

This commit is contained in:
rimu 2024-01-12 17:15:08 +13:00
parent dff156fd12
commit f9f236da13
8 changed files with 145 additions and 100 deletions

View file

@ -21,16 +21,33 @@ from app.models import Community, CommunityMember, Post, Site, User, utcnow, Dom
@bp.route('/', methods=['HEAD', 'GET', 'POST'])
@bp.route('/index', methods=['GET', 'POST'])
def index():
@bp.route('/home', methods=['GET', 'POST'])
@bp.route('/home/<sort>', methods=['GET', 'POST'])
def index(sort='hot'):
if 'application/ld+json' in request.headers.get('Accept', '') or 'application/activity+json' in request.headers.get(
'Accept', ''):
return activitypub_application()
return home_page('home', sort)
@bp.route('/popular', methods=['GET'])
@bp.route('/popular/<sort>', methods=['GET'])
def popular(sort='hot'):
return home_page('popular', sort)
@bp.route('/all', methods=['GET'])
@bp.route('/all/<sort>', methods=['GET'])
def all_posts(sort='hot'):
return home_page('all', sort)
def home_page(type, sort='hot'):
verification_warning()
# If nothing has changed since their last visit, return HTTP 304
current_etag = f"home_{hash(str(g.site.last_active))}"
current_etag = f"{type}_{hash(str(g.site.last_active))}"
if current_user.is_anonymous and request_etag_matches(current_etag):
return return_304(current_etag)
@ -39,110 +56,66 @@ def index():
if current_user.is_anonymous:
flash(_('Create an account to tailor this feed to your interests.'))
posts = Post.query.filter(Post.from_bot == False, Post.nsfw == False, Post.nsfl == False)
posts = posts.join(Community, Community.id == Post.community_id).filter(Community.show_home == True)
posts = posts.join(Community, Community.id == Post.community_id)
if type == 'home':
posts = posts.filter(Community.show_home == True)
elif type == 'popular':
posts = posts.filter(Community.show_popular == True).filter(Post.score > 100)
elif type == 'all':
posts = posts.filter(Community.show_all == True)
content_filters = {}
else:
posts = Post.query.join(CommunityMember, Post.community_id == CommunityMember.community_id).filter(CommunityMember.is_banned == False)
posts = posts.join(User, CommunityMember.user_id == User.id).filter(User.id == current_user.id)
if type == 'home':
posts = Post.query.join(CommunityMember, Post.community_id == CommunityMember.community_id).filter(
CommunityMember.is_banned == False)
# posts = posts.join(User, CommunityMember.user_id == User.id).filter(User.id == current_user.id)
posts = posts.filter(CommunityMember.user_id == current_user.id)
elif type == 'popular':
posts = Post.query.filter(Post.from_bot == False)
posts = posts.join(Community, Community.id == Post.community_id)
posts = posts.filter(Community.show_popular == True, Post.score > 100)
elif type == 'all':
posts = Post.query
posts = posts.join(Community, Community.id == Post.community_id)
posts = posts.filter(Community.show_all == True)
domains_ids = blocked_domains(current_user.id)
if domains_ids:
posts = posts.filter(or_(Post.domain_id.not_in(domains_ids), Post.domain_id == None))
content_filters = user_filters_home(current_user.id)
posts = posts.order_by(desc(Post.ranking)).paginate(page=page, per_page=100, error_out=False)
# Sorting
if sort == 'hot':
posts = posts.order_by(desc(Post.ranking))
elif sort == 'top':
posts = posts.filter(Post.posted_at > utcnow() - timedelta(days=1)).order_by(desc(Post.score))
elif sort == 'new':
posts = posts.order_by(desc(Post.posted_at))
next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None
prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev and page != 1 else None
# Pagination
posts = posts.paginate(page=page, per_page=100, error_out=False)
if type == 'home':
next_url = url_for('main.index', page=posts.next_num, sort=sort) if posts.has_next else None
prev_url = url_for('main.index', page=posts.prev_num, sort=sort) if posts.has_prev and page != 1 else None
elif type == 'popular':
next_url = url_for('main.popular', page=posts.next_num, sort=sort) if posts.has_next else None
prev_url = url_for('main.popular', page=posts.prev_num, sort=sort) if posts.has_prev and page != 1 else None
elif type == 'all':
next_url = url_for('main.all_posts', page=posts.next_num, sort=sort) if posts.has_next else None
prev_url = url_for('main.all_posts', page=posts.prev_num, sort=sort) if posts.has_prev and page != 1 else None
active_communities = Community.query.filter_by(banned=False).order_by(desc(Community.last_active)).limit(5).all()
return render_template('index.html', posts=posts, active_communities=active_communities, show_post_community=True,
POST_TYPE_IMAGE=POST_TYPE_IMAGE, POST_TYPE_LINK=POST_TYPE_LINK, low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1',
POST_TYPE_IMAGE=POST_TYPE_IMAGE, POST_TYPE_LINK=POST_TYPE_LINK,
low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1',
SUBSCRIPTION_PENDING=SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER,
etag=f"home_{hash(str(g.site.last_active))}", next_url=next_url, prev_url=prev_url,
rss_feed=f"https://{current_app.config['SERVER_NAME']}/feed", rss_feed_name=f"Posts on " + g.site.name,
title=f"{g.site.name} - {g.site.description}", description=shorten_string(markdown_to_text(g.site.sidebar), 150),
content_filters=content_filters, moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()))
@bp.route('/new', methods=['HEAD', 'GET', 'POST'])
def new_posts():
verification_warning()
# If nothing has changed since their last visit, return HTTP 304
current_etag = f"new_{hash(str(g.site.last_active))}"
if current_user.is_anonymous and request_etag_matches(current_etag):
return return_304(current_etag)
page = request.args.get('page', 1, type=int)
if current_user.is_anonymous:
posts = Post.query.filter(Post.from_bot == False, Post.nsfw == False, Post.nsfl == False)
content_filters = {}
else:
posts = Post.query.join(CommunityMember, Post.community_id == CommunityMember.community_id).filter(
CommunityMember.is_banned == False)
posts = posts.join(User, CommunityMember.user_id == User.id).filter(User.id == current_user.id)
domains_ids = blocked_domains(current_user.id)
if domains_ids:
posts = posts.filter(or_(Post.domain_id.not_in(domains_ids), Post.domain_id == None))
content_filters = user_filters_home(current_user.id)
posts = posts.order_by(desc(Post.posted_at)).paginate(page=page, per_page=100, error_out=False)
next_url = url_for('main.new_posts', page=posts.next_num) if posts.has_next else None
prev_url = url_for('main.new_posts', page=posts.prev_num) if posts.has_prev and page != 1 else None
active_communities = Community.query.filter_by(banned=False).order_by(desc(Community.last_active)).limit(5).all()
return render_template('new_posts.html', posts=posts, active_communities=active_communities,
POST_TYPE_IMAGE=POST_TYPE_IMAGE, POST_TYPE_LINK=POST_TYPE_LINK, show_post_community=True,
SUBSCRIPTION_PENDING=SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER,
etag=f"new_{hash(str(g.site.last_active))}", next_url=next_url, prev_url=prev_url,
etag=f"{type}_{hash(str(g.site.last_active))}", next_url=next_url, prev_url=prev_url,
rss_feed=f"https://{current_app.config['SERVER_NAME']}/feed",
rss_feed_name=f"Posts on " + g.site.name, low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1',
content_filters=content_filters, moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()))
@bp.route('/top', methods=['HEAD', 'GET', 'POST'])
def top_posts():
verification_warning()
# If nothing has changed since their last visit, return HTTP 304
current_etag = f"best_{hash(str(g.site.last_active))}"
if current_user.is_anonymous and request_etag_matches(current_etag):
return return_304(current_etag)
page = request.args.get('page', 1, type=int)
if current_user.is_anonymous:
posts = Post.query.filter(Post.from_bot == False, Post.nsfw == False, Post.nsfl == False)
content_filters = {}
else:
posts = Post.query.join(CommunityMember, Post.community_id == CommunityMember.community_id).filter(
CommunityMember.is_banned == False)
posts = posts.join(User, CommunityMember.user_id == User.id).filter(User.id == current_user.id)
domains_ids = blocked_domains(current_user.id)
if domains_ids:
posts = posts.filter(or_(Post.domain_id.not_in(domains_ids), Post.domain_id == None))
content_filters = user_filters_home(current_user.id)
posts = posts.filter(Post.posted_at > utcnow() - timedelta(days=1)).order_by(desc(Post.score)).paginate(page=page, per_page=100, error_out=False)
next_url = url_for('main.top_posts', page=posts.next_num) if posts.has_next else None
prev_url = url_for('main.top_posts', page=posts.prev_num) if posts.has_prev and page != 1 else None
active_communities = Community.query.filter_by(banned=False).order_by(desc(Community.last_active)).limit(5).all()
return render_template('top_posts.html', posts=posts, active_communities=active_communities,
POST_TYPE_IMAGE=POST_TYPE_IMAGE, POST_TYPE_LINK=POST_TYPE_LINK, show_post_community=True,
SUBSCRIPTION_PENDING=SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER,
etag=f"best_{hash(str(g.site.last_active))}", next_url=next_url, prev_url=prev_url,
rss_feed=f"https://{current_app.config['SERVER_NAME']}/feed",
rss_feed_name=f"Posts on " + g.site.name, low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1',
content_filters=content_filters, moderating_communities=moderating_communities(current_user.get_id()),
rss_feed_name=f"Posts on " + g.site.name,
title=f"{g.site.name} - {g.site.description}",
description=shorten_string(markdown_to_text(g.site.sidebar), 150),
content_filters=content_filters, type=type, sort=sort,
moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()))

View file

@ -49,6 +49,18 @@
content: "\e937";
}
.fe-home::before {
content: "\e979";
}
.fe-popular::before {
content: "\e9e6";
}
.fe-all::before {
content: "\e986";
}
.fe-activity::before {
content: "\e900";
}

View file

@ -76,6 +76,18 @@ nav, etc which are used site-wide */
content: "\e937";
}
.fe-home::before {
content: "\e979";
}
.fe-popular::before {
content: "\e9e6";
}
.fe-all::before {
content: "\e986";
}
.fe-activity::before {
content: "\e900";
}
@ -399,6 +411,10 @@ fieldset legend {
}
}
.low_bandwidth .dropdown-toggle::after {
display: none;
}
.skip-link {
position: absolute;
top: -40px;

View file

@ -29,6 +29,12 @@ nav, etc which are used site-wide */
}
}
.low_bandwidth {
.dropdown-toggle::after {
display: none;
}
}
.skip-link {
position: absolute;
top: -40px; /* Adjust as needed to hide the link off-screen */

View file

@ -75,6 +75,18 @@
content: "\e937";
}
.fe-home::before {
content: "\e979";
}
.fe-popular::before {
content: "\e9e6";
}
.fe-all::before {
content: "\e986";
}
.fe-activity::before {
content: "\e900";
}
@ -611,6 +623,10 @@ nav.navbar {
background-color: antiquewhite;
}
.dropdown-header {
color: black;
}
@media (prefers-color-scheme: dark) {
body {
background-color: #777;

View file

@ -263,6 +263,10 @@ nav.navbar {
background-color: antiquewhite;
}
.dropdown-header {
color: black;
}
@media (prefers-color-scheme: dark) {
body {

View file

@ -1,11 +1,11 @@
<div class="btn-group mt-1 mb-2">
<a href="/" class="btn {{ 'btn-primary' if request.path == '/' else 'btn-outline-secondary' }}" rel="nofollow">
<a href="/{{ type }}/hot" class="btn {{ 'btn-primary' if sort == 'hot' else 'btn-outline-secondary' }}" rel="nofollow">
{{ _('Hot') }}
</a>
<a href="/top" class="btn {{ 'btn-primary' if request.path == '/top' else 'btn-outline-secondary' }}" rel="nofollow">
<a href="/{{ type }}/top" class="btn {{ 'btn-primary' if sort == 'top' else 'btn-outline-secondary' }}" rel="nofollow">
{{ _('Top') }}
</a>
<a href="/new" class="btn {{ 'btn-primary' if request.path == '/new' else 'btn-outline-secondary' }}" rel="nofollow">
<a href="/{{ type }}/new" class="btn {{ 'btn-primary' if sort == 'new' else 'btn-outline-secondary' }}" rel="nofollow">
{{ _('New') }}
</a>
</div>

View file

@ -68,7 +68,7 @@
{% endif %}
{% endblock %}
</head>
<body class="d-flex flex-column" style="padding-top: 78px;">
<body class="d-flex flex-column{{ ' low_bandwidth' if low_bandwidth }}" style="padding-top: 78px;">
<!-- Page content -->
{% block navbar %}
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
@ -91,13 +91,31 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent" role="navigation">
<ul class="nav navbar-nav ml-md-4">
{% if current_user.is_anonymous %}
<li class="nav-item"><a class="nav-link" href="/">{{ _('Home') }}</a></li>
<li class="nav-item"><a class="nav-link" href="/communities">{{ _('Communities') }}</a></li>
<li class="nav-item dropdown{% if active_parent == 'home' %} active{% endif %}">
<a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="/home" role="button" aria-haspopup="true" aria-expanded="false">{{ _('Home') }}</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item{% if active_child == 'popular' %} active{% endif %}" href="/home"><span class="fe fe-home"></span>{{ _('Home') }}</a></li>
<li><a class="dropdown-item{% if active_child == 'popular' %} active{% endif %}" href="/popular"><span class="fe fe-popular"></span>{{ _('Popular') }}</a></li>
<li><a class="dropdown-item{% if active_child == 'all_posts' %} active{% endif %}" href="/all"><span class="fe fe-all"></span>{{ _('All posts') }}</a></li>
</ul>
<li class="nav-item dropdown{% if active_parent == 'communities' %} active{% endif %}">
<a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="/communities" role="button" aria-haspopup="true" aria-expanded="false">{{ _('Communities') }}</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item{% if active_child == 'list_communities' %} active{% endif %}" href="/communities">{{ _('Browse all') }}</a></li>
<li><a class="dropdown-item{% if active_child == 'list_topics' %} active{% endif %}" href="/topics">{{ _('By topic') }}</a></li>
</ul>
</li>
<li class="nav-item"><a class="nav-link" href="/auth/login">{{ _('Log in') }}</a></li>
<li class="nav-item"><a class="nav-link" href="/auth/register">{{ _('Register') }}</a></li>
<li class="nav-item"><a class="nav-link" href="/donate">{{ _('Donate') }}</a></li>
{% else %}
<li class="nav-item"><a class="nav-link" href="/">{{ _('Home') }}</a></li>
<li class="nav-item dropdown{% if active_parent == 'home' %} active{% endif %}">
<a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="/home" role="button" aria-haspopup="true" aria-expanded="false">{{ _('Home') }}</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item{% if active_child == 'popular' %} active{% endif %}" href="/home"><span class="fe fe-home"></span>{{ _('Home') }}</a></li>
<li><a class="dropdown-item{% if active_child == 'popular' %} active{% endif %}" href="/popular"><span class="fe fe-popular"></span>{{ _('Popular') }}</a></li>
<li><a class="dropdown-item{% if active_child == 'all_posts' %} active{% endif %}" href="/all"><span class="fe fe-all"></span>{{ _('All posts') }}</a></li>
</ul>
<li class="nav-item dropdown{% if active_parent == 'communities' %} active{% endif %}">
<a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="/communities" role="button" aria-haspopup="true" aria-expanded="false">{{ _('Communities') }}</a>
<ul class="dropdown-menu">