Merge pull request 'Adding Subscribed/Local/All nav views' (#293) from JollyDevelopment/pyfedi:jollydev/add-view-filter-to-home-page into main

Reviewed-on: https://codeberg.org/rimu/pyfedi/pulls/293
This commit is contained in:
rimu 2024-08-16 00:27:29 +00:00
commit e12838cf9f
5 changed files with 52 additions and 13 deletions

View file

@ -34,44 +34,62 @@ from app.models import Community, CommunityMember, Post, Site, User, utcnow, Dom
@bp.route('/', methods=['HEAD', 'GET', 'POST'])
@bp.route('/home', methods=['GET', 'POST'])
@bp.route('/home/<sort>', methods=['GET', 'POST'])
@bp.route('/home/<sort>/<view_filter>', methods=['GET', 'POST'])
@cache.cached(make_cache_key=make_cache_key)
def index(sort=None):
def index(sort=None, view_filter=None):
if 'application/ld+json' in request.headers.get('Accept', '') or 'application/activity+json' in request.headers.get(
'Accept', ''):
return activitypub_application()
if 'view_filter' in request.view_args:
view_filter = request.view_args['view_filter']
return CachedResponse(
response=home_page('home', sort),
response=home_page('home', sort, view_filter),
timeout=50 if current_user.is_anonymous else 5,
)
@bp.route('/popular', methods=['GET'])
@bp.route('/popular/<sort>', methods=['GET'])
@bp.route('/popular/<sort>/<view_filter>', methods=['GET', 'POST'])
@cache.cached(timeout=5, make_cache_key=make_cache_key)
def popular(sort=None):
def popular(sort=None, view_filter=None):
if 'view_filter' in request.view_args:
view_filter = request.view_args['view_filter']
return CachedResponse(
response=home_page('popular', sort),
response=home_page('popular', sort, view_filter),
timeout=50 if current_user.is_anonymous else 5,
)
@bp.route('/all', methods=['GET'])
@bp.route('/all/<sort>', methods=['GET'])
@bp.route('/all/<sort>/<view_filter>', methods=['GET', 'POST'])
@cache.cached(timeout=5, make_cache_key=make_cache_key)
def all_posts(sort=None):
def all_posts(sort=None, view_filter=None):
if 'view_filter' in request.view_args:
view_filter = request.view_args['view_filter']
return CachedResponse(
response=home_page('all', sort),
response=home_page('all', sort, view_filter),
timeout=50 if current_user.is_anonymous else 5,
)
def home_page(type, sort):
def home_page(type, sort, view_filter):
verification_warning()
if sort is None:
sort = current_user.default_sort if current_user.is_authenticated else 'hot'
if view_filter is None:
view_filter = 'all'
# If nothing has changed since their last visit, return HTTP 304
current_etag = f"{type}_{sort}_{hash(str(g.site.last_active))}"
if current_user.is_anonymous and request_etag_matches(current_etag):
@ -128,6 +146,12 @@ def home_page(type, sort):
posts = posts.filter(Post.user_id.not_in(blocked_accounts))
content_filters = user_filters_home(current_user.id)
# view filter - subscribed/local/all
if view_filter == 'subscribed':
posts = posts.filter(CommunityMember.user_id == current_user.id)
elif view_filter == 'local':
posts = posts.filter(Post.instance_id == 1)
# Sorting
if sort == 'hot':
posts = posts.order_by(desc(Post.ranking)).order_by(desc(Post.posted_at))
@ -169,6 +193,7 @@ def home_page(type, sort):
recently_upvoted = []
recently_downvoted = []
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, POST_TYPE_VIDEO=POST_TYPE_VIDEO, POST_TYPE_POLL=POST_TYPE_POLL,
low_bandwidth=low_bandwidth, recently_upvoted=recently_upvoted,
@ -179,7 +204,7 @@ def home_page(type, sort):
#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,
content_filters=content_filters, type=type, sort=sort, view_filter=view_filter,
announcement=allowlist_html(get_setting('announcement', '')),
moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()),

View file

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

View file

@ -0,0 +1,13 @@
<div class="btn-group mt-1 mb-2" style="float: right;">
{% if not current_user.is_anonymous %}
<a href="/{{ type }}/{{ sort }}/subscribed" class="btn {{ 'btn-primary' if view_filter == 'subscribed' else 'btn-outline-secondary' }}" rel="nofollow noindex">
{{ _('Subscribed') }}
</a>
{% endif %}
<a href="/{{ type }}/{{ sort }}/local" class="btn {{ 'btn-primary' if view_filter == 'local' else 'btn-outline-secondary' }}" rel="nofollow noindex">
{{ _('Local') }}
</a>
<a href="/{{ type }}/{{ sort }}/all" class="btn {{ 'btn-primary' if view_filter == 'all' else 'btn-outline-secondary' }}" rel="nofollow noindex">
{{ _('All') }}
</a>
</div>

View file

@ -13,6 +13,7 @@
<div id="home_announcement">{{ announcement|safe }}</div>
{% endif -%}
{% include "_home_nav.html" %}
{% include "_view_filter_nav.html" %}
<div class="post_list">
{% for post in posts.items -%}
{% include 'post/_post_teaser.html' %}

View file

@ -169,7 +169,7 @@ def gibberish(length: int = 10) -> str:
# used by @cache.cached() for home page and post caching
def make_cache_key(sort=None, post_id=None):
def make_cache_key(sort=None, post_id=None, view_filter=None):
if current_user.is_anonymous:
return f'{request.url}_{sort}_{post_id}_anon_{request.headers.get("Accept")}_{request.headers.get("Accept-Language")}' # The Accept header differentiates between activitypub requests and everything else
else: