From 49db71e05d3e856d87d4be055ffb02ee8733adfd Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Tue, 12 Mar 2024 20:58:47 +1300 Subject: [PATCH] opt out of search --- app/activitypub/util.py | 15 +++++++++++++-- app/community/util.py | 1 + app/models.py | 2 +- app/post/routes.py | 1 + app/search/routes.py | 25 +++++++++++++++++-------- app/templates/base.html | 3 +++ app/templates/post/post.html | 4 ++-- app/user/forms.py | 2 +- app/user/routes.py | 6 ++++++ 9 files changed, 45 insertions(+), 14 deletions(-) diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 683c35f7..425f5125 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -339,10 +339,19 @@ def refresh_user_profile_task(user_id): if actor_data.status_code == 200: activity_json = actor_data.json() actor_data.close() + + # update indexible state on their posts, if necessary + new_indexable = activity_json['indexable'] if 'indexable' in activity_json else True + if new_indexable != user.indexable: + db.session.execute(text('UPDATE "post" set indexable = :indexable WHERE user_id = :user_id'), + {'user_id': user.id, + 'indexable': new_indexable}) + user.user_name = activity_json['preferredUsername'] user.about_html = parse_summary(activity_json) user.ap_fetched_at = utcnow() - user.public_key=activity_json['publicKey']['publicKeyPem'] + user.public_key = activity_json['publicKey']['publicKeyPem'] + user.indexable = new_indexable avatar_changed = cover_changed = False if 'icon' in activity_json: @@ -594,6 +603,7 @@ def post_json_to_model(post_json, user, community) -> Post: last_active=post_json['published'], instance_id=user.instance_id ) + post.indexable = user.indexable if 'source' in post_json and \ post_json['source']['mediaType'] == 'text/markdown': post.body = post_json['source']['content'] @@ -1212,7 +1222,8 @@ def create_post(activity_log: ActivityPubLog, community: Community, request_json type=constants.POST_TYPE_ARTICLE, up_votes=1, score=instance_weight(user.ap_domain), - instance_id=user.instance_id + instance_id=user.instance_id, + indexable=user.indexable ) # Get post content. Lemmy and Kbin put this in different places. if 'source' in request_json['object'] and isinstance(request_json['object']['source'], dict) and request_json['object']['source']['mediaType'] == 'text/markdown': # Lemmy diff --git a/app/community/util.py b/app/community/util.py index 75132ded..f86e5811 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -164,6 +164,7 @@ def url_to_thumbnail_file(filename) -> File: def save_post(form, post: Post): + post.indexable = current_user.indexable post.nsfw = form.nsfw.data post.nsfl = form.nsfl.data post.notify_author = form.notify_author.data diff --git a/app/models.py b/app/models.py index c42f8a59..69449bd5 100644 --- a/app/models.py +++ b/app/models.py @@ -776,7 +776,7 @@ class Post(db.Model): nsfl = db.Column(db.Boolean, default=False, index=True) sticky = db.Column(db.Boolean, default=False) notify_author = db.Column(db.Boolean, default=True) - indexable = db.Column(db.Boolean, default=False) + indexable = db.Column(db.Boolean, default=True) from_bot = db.Column(db.Boolean, default=False, index=True) created_at = db.Column(db.DateTime, index=True, default=utcnow) # this is when the content arrived here posted_at = db.Column(db.DateTime, index=True, default=utcnow) # this is when the original server created it diff --git a/app/post/routes.py b/app/post/routes.py index 0c5ed613..67472b84 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -216,6 +216,7 @@ def show_post(post_id: int): canonical=post.ap_id, form=form, replies=replies, THREAD_CUTOFF_DEPTH=constants.THREAD_CUTOFF_DEPTH, description=description, og_image=og_image, POST_TYPE_IMAGE=constants.POST_TYPE_IMAGE, POST_TYPE_LINK=constants.POST_TYPE_LINK, POST_TYPE_ARTICLE=constants.POST_TYPE_ARTICLE, + noindex=not post.author.indexable, etag=f"{post.id}{sort}_{hash(post.last_active)}", markdown_editor=current_user.is_authenticated and current_user.markdown_editor, low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER, moderating_communities=moderating_communities(current_user.get_id()), diff --git a/app/search/routes.py b/app/search/routes.py index e4a96cf8..8edbcef7 100644 --- a/app/search/routes.py +++ b/app/search/routes.py @@ -5,11 +5,10 @@ from sqlalchemy import or_ from app.models import Post from app.search import bp -from app.utils import moderating_communities, joined_communities, render_template, blocked_domains +from app.utils import moderating_communities, joined_communities, render_template, blocked_domains, blocked_instances @bp.route('/search', methods=['GET', 'POST']) -@login_required def run_search(): if request.args.get('q') is not None: q = request.args.get('q') @@ -17,16 +16,26 @@ def run_search(): low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1' posts = Post.query.search(q) - if current_user.ignore_bots: + if current_user.is_authenticated: + if current_user.ignore_bots: + posts = posts.filter(Post.from_bot == False) + if current_user.show_nsfl is False: + posts = posts.filter(Post.nsfl == False) + if current_user.show_nsfw is False: + posts = posts.filter(Post.nsfw == False) + domains_ids = blocked_domains(current_user.id) + if domains_ids: + posts = posts.filter(or_(Post.domain_id.not_in(domains_ids), Post.domain_id == None)) + instance_ids = blocked_instances(current_user.id) + if instance_ids: + posts = posts.filter(or_(Post.instance_id.not_in(instance_ids), Post.instance_id == None)) + else: posts = posts.filter(Post.from_bot == False) - if current_user.show_nsfl is False: posts = posts.filter(Post.nsfl == False) - if current_user.show_nsfw is False: posts = posts.filter(Post.nsfw == False) - domains_ids = blocked_domains(current_user.id) - if domains_ids: - posts = posts.filter(or_(Post.domain_id.not_in(domains_ids), Post.domain_id == None)) + posts = posts.filter(Post.indexable == True) + posts = posts.paginate(page=page, per_page=100 if current_user.is_authenticated and not low_bandwidth else 50, error_out=False) diff --git a/app/templates/base.html b/app/templates/base.html index 92c8e266..7e76319b 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -70,6 +70,9 @@ {% if rss_feed %} {% endif %} + {% if noindex %} + + {% endif %}