Merge branch 'main' into feature/image_post_editing

This commit is contained in:
xmatt 2024-12-14 22:32:46 +00:00
commit cf73c996b9
3 changed files with 38 additions and 7 deletions

View file

@ -116,9 +116,39 @@ def home_page(sort, view_filter):
posts = posts.order_by(desc(Post.last_active)) posts = posts.order_by(desc(Post.last_active))
# Pagination # Pagination
posts = posts.paginate(page=page, per_page=100 if current_user.is_authenticated and not low_bandwidth else 50, error_out=False) if view_filter == 'subscribed' and current_user.is_authenticated and sort == 'new':
next_url = url_for('main.index', page=posts.next_num, sort=sort, view_filter=view_filter) if posts.has_next else None # use python list instead of DB query
prev_url = url_for('main.index', page=posts.prev_num, sort=sort, view_filter=view_filter) if posts.has_prev and page != 1 else None posts = posts.all()
# exclude extra cross-posts from feed
already_seen = []
limit = 100 if not low_bandwidth else 50
#i = -1 # option 1: don't exclude cross-posts
#i = min(limit - 1, len(posts) - 1) # option 2: exclude cross-posts from the first page only
i = min((limit * 10) - 1, len(posts) - 1) # option 3: exclude cross-posts across a 'magic number' of pages
#i = len(posts) - 1 # option 4: exclude all cross-posts ever
while i >= 0:
if not posts[i].cross_posts:
i -= 1
continue
if posts[i].id in already_seen:
posts.pop(i)
else:
already_seen.extend(posts[i].cross_posts)
i -= 1
# paginate manually (can't use paginate())
start = (page - 1) * limit
end = start + limit
posts = posts[start:end]
next_page = page + 1 if len(posts) == limit else None
previous_page = page - 1 if page != 1 else None
next_url = url_for('main.index', page=next_page, sort=sort, view_filter=view_filter) if next_page else None
prev_url = url_for('main.index', page=previous_page, sort=sort, view_filter=view_filter) if previous_page else None
else:
posts = posts.paginate(page=page, per_page=100 if current_user.is_authenticated and not low_bandwidth else 50, error_out=False)
next_url = url_for('main.index', page=posts.next_num, sort=sort, view_filter=view_filter) if posts.has_next else None
prev_url = url_for('main.index', page=posts.prev_num, sort=sort, view_filter=view_filter) if posts.has_prev and page != 1 else None
# Active Communities # Active Communities
active_communities = Community.query.filter_by(banned=False) active_communities = Community.query.filter_by(banned=False)

View file

@ -15,12 +15,12 @@
{% include "_home_nav.html" %} {% include "_home_nav.html" %}
{% include "_view_filter_nav.html" %} {% include "_view_filter_nav.html" %}
<div class="post_list h-feed"> <div class="post_list h-feed">
{% for post in posts.items -%} {% for post in posts %}
{% include 'post/_post_teaser.html' %} {% include 'post/_post_teaser.html' %}
{% else -%} {% else %}
<p>{{ _('No posts yet. Join some communities to see more.') }}</p> <p>{{ _('No posts yet. Join some communities to see more.') }}</p>
<p><a class="btn btn-primary" href="/communities">{{ _('More communities') }}</a></p> <p><a class="btn btn-primary" href="/communities">{{ _('More communities') }}</a></p>
{% endfor -%} {% endfor %}
</div> </div>
<nav aria-label="Pagination" class="mt-4" role="navigation"> <nav aria-label="Pagination" class="mt-4" role="navigation">

View file

@ -17,6 +17,7 @@ import math
from urllib.parse import urlparse, parse_qs, urlencode from urllib.parse import urlparse, parse_qs, urlencode
from functools import wraps from functools import wraps
import flask import flask
import requests
from bs4 import BeautifulSoup, MarkupResemblesLocatorWarning from bs4 import BeautifulSoup, MarkupResemblesLocatorWarning
import warnings import warnings
import jwt import jwt
@ -1111,7 +1112,7 @@ def in_sorted_list(arr, target):
# Makes a still image from a video url, without downloading the whole video file # Makes a still image from a video url, without downloading the whole video file
def generate_image_from_video_url(video_url, output_path, length=2): def generate_image_from_video_url(video_url, output_path, length=2):
response = httpx_client.get(video_url, stream=True, timeout=5, response = requests.get(video_url, stream=True, timeout=5,
headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0'}) # Imgur requires a user agent headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0'}) # Imgur requires a user agent
content_type = response.headers.get('Content-Type') content_type = response.headers.get('Content-Type')
if content_type: if content_type: