From d9290981473e69d6de04ff8aba68985d6736a93c Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Fri, 21 Jun 2024 17:28:49 +0800 Subject: [PATCH] view bookmarks of comments #26 --- app/post/routes.py | 43 +++++++++++++++++++++- app/templates/post/post_options.html | 7 +++- app/templates/post/post_reply_options.html | 5 +++ app/templates/user/_bookmarks_nav.html | 12 ++++++ app/templates/user/bookmarks.html | 11 +++++- app/templates/user/bookmarks_comments.html | 43 ++++++++++++++++++++++ app/user/routes.py | 26 ++++++++++++- 7 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 app/templates/user/_bookmarks_nav.html create mode 100644 app/templates/user/bookmarks_comments.html diff --git a/app/post/routes.py b/app/post/routes.py index eb467f91..bac75813 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -853,8 +853,12 @@ def post_options(post_id: int): if current_user.is_anonymous or not current_user.is_admin(): if post.deleted: abort(404) + + existing_bookmark = [] + if current_user.is_authenticated: + existing_bookmark = PostBookmark.query.filter(PostBookmark.post_id == post_id, PostBookmark.user_id == current_user.id).first() - return render_template('post/post_options.html', post=post, + return render_template('post/post_options.html', post=post, existing_bookmark=existing_bookmark, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), menu_topics=menu_topics(), site=g.site) @@ -867,7 +871,14 @@ def post_reply_options(post_id: int, comment_id: int): if current_user.is_anonymous or not current_user.is_admin(): if post.deleted or post_reply.deleted: abort(404) + + existing_bookmark = [] + if current_user.is_authenticated: + existing_bookmark = PostReplyBookmark.query.filter(PostReplyBookmark.post_reply_id == comment_id, + PostReplyBookmark.user_id == current_user.id).first() + return render_template('post/post_reply_options.html', post=post, post_reply=post_reply, + existing_bookmark=existing_bookmark, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), menu_topics=menu_topics(), site=g.site @@ -1608,6 +1619,36 @@ def post_bookmark(post_id: int): return redirect(url_for('activitypub.post_ap', post_id=post.id)) +@bp.route('/post//remove_bookmark', methods=['GET', 'POST']) +@login_required +def post_remove_bookmark(post_id: int): + post = Post.query.get_or_404(post_id) + if post.deleted: + abort(404) + existing_bookmark = PostBookmark.query.filter(PostBookmark.post_id == post_id, PostBookmark.user_id == current_user.id).first() + if existing_bookmark: + db.session.delete(existing_bookmark) + db.session.commit() + flash(_('Bookmark has been removed.')) + return redirect(url_for('activitypub.post_ap', post_id=post.id)) + + +@bp.route('/post//comment//remove_bookmark', methods=['GET', 'POST']) +@login_required +def post_reply_remove_bookmark(post_id: int, comment_id: int): + post = Post.query.get_or_404(post_id) + post_reply = PostReply.query.get_or_404(comment_id) + + if post.deleted or post_reply.deleted: + abort(404) + existing_bookmark = PostReplyBookmark.query.filter(PostReplyBookmark.post_reply_id == comment_id, PostReplyBookmark.user_id == current_user.id).first() + if existing_bookmark: + db.session.delete(existing_bookmark) + db.session.commit() + flash(_('Bookmark has been removed.')) + return redirect(url_for('activitypub.post_ap', post_id=post.id)) + + @bp.route('/post//report', methods=['GET', 'POST']) @login_required def post_report(post_id: int): diff --git a/app/templates/post/post_options.html b/app/templates/post/post_options.html index fb72f87f..36c7e048 100644 --- a/app/templates/post/post_options.html +++ b/app/templates/post/post_options.html @@ -26,8 +26,13 @@ {{ _('Delete') }} {% endif -%} {% endif -%} -
  • + {% if existing_bookmark -%} +
  • + {{ _('Remove bookmark') }}
  • + {% else -%} +
  • {{ _('Bookmark') }}
  • + {% endif -%} {% if post.user_id == current_user.id and not post.mea_culpa -%}
  • {{ _("I made a mistake with this post and have changed my mind about the topic") }}
  • diff --git a/app/templates/post/post_reply_options.html b/app/templates/post/post_reply_options.html index 211f107a..30c8c2dd 100644 --- a/app/templates/post/post_reply_options.html +++ b/app/templates/post/post_reply_options.html @@ -26,8 +26,13 @@ {{ _('Delete') }} {% endif -%} {% endif -%} + {% if existing_bookmark -%} +
  • + {{ _('Remove bookmark') }}
  • + {% else -%}
  • {{ _('Bookmark') }}
  • + {% endif -%} {% if post_reply.user_id != current_user.id -%}
  • {{ _('Block author @%(author_name)s', author_name=post_reply.author.user_name) }}
  • diff --git a/app/templates/user/_bookmarks_nav.html b/app/templates/user/_bookmarks_nav.html new file mode 100644 index 00000000..4d504141 --- /dev/null +++ b/app/templates/user/_bookmarks_nav.html @@ -0,0 +1,12 @@ + \ No newline at end of file diff --git a/app/templates/user/bookmarks.html b/app/templates/user/bookmarks.html index 5535a2fa..f90e1ecc 100644 --- a/app/templates/user/bookmarks.html +++ b/app/templates/user/bookmarks.html @@ -8,11 +8,20 @@ {% block app_content %}
    + +

    {{ _('Bookmarks') }}

    + {% include 'user/_bookmarks_nav.html' %}
    {% for post in posts.items -%} {% include 'post/_post_teaser.html' %} {% else -%} -

    {{ _('No posts have been bookmarked.') }}

    +

    {{ _('No posts have been bookmarked. Use the three dots on each post to find the bookmark function.') }}

    {% endfor -%}
    diff --git a/app/templates/user/bookmarks_comments.html b/app/templates/user/bookmarks_comments.html new file mode 100644 index 00000000..7f488fd9 --- /dev/null +++ b/app/templates/user/bookmarks_comments.html @@ -0,0 +1,43 @@ +{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %} + {% extends 'themes/' + theme() + '/base.html' %} +{% else %} + {% extends "base.html" %} +{% endif %} +{% set active_child = 'bookmarks' %} + +{% block app_content %} +
    +
    + +

    {{ _('Bookmarks of comments') }}

    + {% include 'user/_bookmarks_nav.html' %} + +
    + {% for post_reply in post_replies.items %} + {% include 'post/_post_reply_teaser.html' %} + {% else -%} +

    {{ _('No comments have been bookmarked. Use the three dots on each comment to find the bookmark function.') }}

    + {% endfor -%} +
    + + +
    +
    +{% endblock %} diff --git a/app/user/routes.py b/app/user/routes.py index 8b6d86b7..c607b7f7 100644 --- a/app/user/routes.py +++ b/app/user/routes.py @@ -15,7 +15,7 @@ from app.constants import SUBSCRIPTION_MEMBER, SUBSCRIPTION_PENDING, NOTIF_USER, from app.email import send_verification_email from app.models import Post, Community, CommunityMember, User, PostReply, PostVote, Notification, utcnow, File, Site, \ Instance, Report, UserBlock, CommunityBan, CommunityJoinRequest, CommunityBlock, Filter, Domain, DomainBlock, \ - InstanceBlock, NotificationSubscription, PostBookmark + InstanceBlock, NotificationSubscription, PostBookmark, PostReplyBookmark from app.user import bp from app.user.forms import ProfileForm, SettingsForm, DeleteAccountForm, ReportUserForm, FilterEditForm, \ RemoteFollowForm @@ -881,7 +881,29 @@ def user_bookmarks(): prev_url = url_for('user.user_bookmarks', page=posts.prev_num) if posts.has_prev and page != 1 else None return render_template('user/bookmarks.html', title=_('Bookmarks'), posts=posts, show_post_community=True, - low_bandwidth=low_bandwidth, + low_bandwidth=low_bandwidth, user=current_user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()), + menu_topics=menu_topics(), site=g.site, + next_url=next_url, prev_url=prev_url) + + +@bp.route('/bookmarks/comments') +@login_required +def user_bookmarks_comments(): + page = request.args.get('page', 1, type=int) + low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1' + + post_replies = PostReply.query.filter(PostReply.deleted == False).join(PostReplyBookmark, PostReplyBookmark.post_reply_id == PostReply.id).\ + filter(PostReplyBookmark.user_id == current_user.id).order_by(desc(PostReplyBookmark.created_at)) + + post_replies = post_replies.paginate(page=page, per_page=100 if current_user.is_authenticated and not low_bandwidth else 50, + error_out=False) + next_url = url_for('user.user_bookmarks_comments', page=post_replies.next_num) if post_replies.has_next else None + prev_url = url_for('user.user_bookmarks_comments', page=post_replies.prev_num) if post_replies.has_prev and page != 1 else None + + return render_template('user/bookmarks_comments.html', title=_('Comment bookmarks'), post_replies=post_replies, show_post_community=True, + low_bandwidth=low_bandwidth, user=current_user, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), menu_topics=menu_topics(), site=g.site,