diff --git a/app/main/routes.py b/app/main/routes.py index 4927c1bc..b46ec14f 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -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/', methods=['GET', 'POST']) +@bp.route('/home//', 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/', methods=['GET']) +@bp.route('/popular//', 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/', methods=['GET']) +@bp.route('/all//', 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()), diff --git a/app/templates/_home_nav.html b/app/templates/_home_nav.html index 30f055da..97dcb973 100644 --- a/app/templates/_home_nav.html +++ b/app/templates/_home_nav.html @@ -1,14 +1,14 @@ \ No newline at end of file diff --git a/app/templates/_view_filter_nav.html b/app/templates/_view_filter_nav.html new file mode 100644 index 00000000..1ab95e58 --- /dev/null +++ b/app/templates/_view_filter_nav.html @@ -0,0 +1,13 @@ +
+ {% if not current_user.is_anonymous %} + + {{ _('Subscribed') }} + + {% endif %} + + {{ _('Local') }} + + + {{ _('All') }} + +
\ No newline at end of file diff --git a/app/templates/index.html b/app/templates/index.html index 2e8798bf..dfd874ee 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -13,6 +13,7 @@
{{ announcement|safe }}
{% endif -%} {% include "_home_nav.html" %} + {% include "_view_filter_nav.html" %}
{% for post in posts.items -%} {% include 'post/_post_teaser.html' %} diff --git a/app/utils.py b/app/utils.py index c85a98b1..eef7bb28 100644 --- a/app/utils.py +++ b/app/utils.py @@ -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: