From 0210adafa5d55ff760eea88ba1508b788ef46d4b Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Wed, 20 Mar 2024 20:41:45 +1300 Subject: [PATCH] show sticky posts at top of their community fixes #16 --- app/community/forms.py | 1 + app/community/routes.py | 11 ++++++++--- app/community/util.py | 1 + app/post/routes.py | 4 ++++ app/static/scss/_typography.scss | 13 +++++++++++++ app/static/structure.css | 12 ++++++++++++ app/static/styles.css | 12 ++++++++++++ app/templates/community/add_post.html | 3 +++ app/templates/post/_post_teaser.html | 3 ++- app/templates/post/post_edit.html | 3 +++ 10 files changed, 59 insertions(+), 4 deletions(-) diff --git a/app/community/forms.py b/app/community/forms.py index 4d293fa2..bb700ede 100644 --- a/app/community/forms.py +++ b/app/community/forms.py @@ -89,6 +89,7 @@ class CreatePostForm(FlaskForm): render_kw={'placeholder': 'Text (optional)'}) image_file = FileField(_('Image')) # flair = SelectField(_l('Flair'), coerce=int) + sticky = BooleanField(_l('Sticky')) nsfw = BooleanField(_l('NSFW')) nsfl = BooleanField(_l('Gore/gross')) notify_author = BooleanField(_l('Notify about replies')) diff --git a/app/community/routes.py b/app/community/routes.py index ce09ad64..ed350e22 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -120,6 +120,8 @@ def show_community(community: Community): page = request.args.get('page', 1, type=int) sort = request.args.get('sort', '' if current_user.is_anonymous else current_user.default_sort) + if sort is None: + sort = '' low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1' if low_bandwidth: post_layout = None @@ -171,13 +173,13 @@ def show_community(community: Community): posts = posts.filter(or_(Post.instance_id.not_in(instance_ids), Post.instance_id == None)) if sort == '' or sort == 'hot': - posts = posts.order_by(desc(Post.ranking)).order_by(desc(Post.posted_at)) + posts = posts.order_by(desc(Post.sticky)).order_by(desc(Post.ranking)).order_by(desc(Post.posted_at)) elif sort == 'top': - posts = posts.filter(Post.posted_at > utcnow() - timedelta(days=7)).order_by(desc(Post.score)) + posts = posts.filter(Post.posted_at > utcnow() - timedelta(days=7)).order_by(desc(Post.sticky)).order_by(desc(Post.score)) elif sort == 'new': posts = posts.order_by(desc(Post.posted_at)) elif sort == 'active': - posts = posts.order_by(desc(Post.last_active)) + posts = posts.order_by(desc(Post.sticky)).order_by(desc(Post.last_active)) per_page = 100 if post_layout == 'masonry': per_page = 200 @@ -444,6 +446,8 @@ def add_post(actor): if community.nsfl: form.nsfl.data = True form.nsfw.render_kw = {'disabled': True} + if not(community.is_moderator() or community.is_owner() or current_user.is_admin()): + form.sticky.render_kw = {'disabled': True} form.communities.choices = [(c.id, c.display_name()) for c in current_user.communities()] @@ -485,6 +489,7 @@ def add_post(actor): 'commentsEnabled': post.comments_enabled, 'sensitive': post.nsfw, 'nsfl': post.nsfl, + 'stickied': post.sticky, 'published': ap_datetime(utcnow()), 'audience': community.ap_profile_id } diff --git a/app/community/util.py b/app/community/util.py index f96e915d..aedb3615 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -185,6 +185,7 @@ def url_to_thumbnail_file(filename) -> File: def save_post(form, post: Post): post.indexable = current_user.indexable + post.sticky = form.sticky.data post.nsfw = form.nsfw.data post.nsfl = form.nsfl.data post.notify_author = form.notify_author.data diff --git a/app/post/routes.py b/app/post/routes.py index 568a4518..f506c68f 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -649,6 +649,7 @@ def post_edit(post_id: int): 'commentsEnabled': post.comments_enabled, 'sensitive': post.nsfw, 'nsfl': post.nsfl, + 'stickied': post.sticky, 'published': ap_datetime(post.posted_at), 'updated': ap_datetime(post.edited_at), 'audience': post.community.ap_profile_id @@ -722,6 +723,9 @@ def post_edit(post_id: int): form.notify_author.data = post.notify_author form.nsfw.data = post.nsfw form.nsfl.data = post.nsfl + form.sticky.data = post.sticky + if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()): + form.sticky.render_kw = {'disabled': True} return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post, markdown_editor=current_user.markdown_editor, moderating_communities=moderating_communities(current_user.get_id()), diff --git a/app/static/scss/_typography.scss b/app/static/scss/_typography.scss index a5cdd97a..780e81d2 100644 --- a/app/static/scss/_typography.scss +++ b/app/static/scss/_typography.scss @@ -309,6 +309,19 @@ h1 { } } +.fe-sticky-left::before { + position: relative; + top: 2px; + content: "\e934"; +} + +.fe-sticky-right::before { + position: relative; + top: 2px; + content: "\e933"; +} + + a.no-underline { text-decoration: none; &:hover { diff --git a/app/static/structure.css b/app/static/structure.css index ec7d61e8..0500d613 100644 --- a/app/static/structure.css +++ b/app/static/structure.css @@ -333,6 +333,18 @@ h1 .fe-bell, h1 .fe-no-bell { /* possibly e985 */ } +.fe-sticky-left::before { + position: relative; + top: 2px; + content: "\e934"; +} + +.fe-sticky-right::before { + position: relative; + top: 2px; + content: "\e933"; +} + a.no-underline { text-decoration: none; } diff --git a/app/static/styles.css b/app/static/styles.css index abe90c6f..149e1db0 100644 --- a/app/static/styles.css +++ b/app/static/styles.css @@ -332,6 +332,18 @@ h1 .fe-bell, h1 .fe-no-bell { /* possibly e985 */ } +.fe-sticky-left::before { + position: relative; + top: 2px; + content: "\e934"; +} + +.fe-sticky-right::before { + position: relative; + top: 2px; + content: "\e933"; +} + a.no-underline { text-decoration: none; } diff --git a/app/templates/community/add_post.html b/app/templates/community/add_post.html index 1f0a07c3..f957d17a 100644 --- a/app/templates/community/add_post.html +++ b/app/templates/community/add_post.html @@ -98,6 +98,9 @@