From 75f2c8d2ef87f4b6d73c595b3184932697b1566b Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:02:48 +1300 Subject: [PATCH] sitemap of local posts --- app/main/routes.py | 17 +++++++++++++++++ app/templates/sitemap.xml | 9 +++++++++ pyfedi.py | 3 ++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 app/templates/sitemap.xml diff --git a/app/main/routes.py b/app/main/routes.py index 08cb26d7..7efe691d 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -216,6 +216,23 @@ def robots(): return resp +@bp.route('/sitemap.xml') +@cache.cached(timeout=6000) +def sitemap(): + posts = Post.query.filter(Post.from_bot == False) + posts = posts.join(Community, Community.id == Post.community_id) + posts = posts.filter(Community.show_all == True, Community.ap_id == None) # sitemap.xml only includes local posts + if not g.site.enable_nsfw: + posts = posts.filter(Community.nsfw == False) + if not g.site.enable_nsfl: + posts = posts.filter(Community.nsfl == False) + posts = posts.order_by(desc(Post.posted_at)) + + resp = make_response(render_template('sitemap.xml', posts=posts, current_app=current_app)) + resp.mimetype = 'text/xml' + return resp + + @bp.route('/keyboard_shortcuts') def keyboard_shortcuts(): return render_template('keyboard_shortcuts.html') diff --git a/app/templates/sitemap.xml b/app/templates/sitemap.xml new file mode 100644 index 00000000..d883c2bf --- /dev/null +++ b/app/templates/sitemap.xml @@ -0,0 +1,9 @@ + + +{% for post in posts %} + + https://{{ current_app.config['SERVER_NAME'] }}/post/{{ post.id }} + {{ ap_datetime(post.edited_at) if post.edited_at else ap_datetime(post.posted_at) }} + +{% endfor %} + \ No newline at end of file diff --git a/pyfedi.py b/pyfedi.py index 163241f5..52d5c565 100644 --- a/pyfedi.py +++ b/pyfedi.py @@ -8,7 +8,7 @@ from flask import session, g, json, request from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_ARTICLE from app.models import Site from app.utils import getmtime, gibberish, shorten_string, shorten_url, digits, user_access, community_membership, \ - can_create, can_upvote, can_downvote, shorten_number + can_create, can_upvote, can_downvote, shorten_number, ap_datetime app = create_app() cli.register(app) @@ -35,6 +35,7 @@ with app.app_context(): app.jinja_env.globals['community_membership'] = community_membership app.jinja_env.globals['json_loads'] = json.loads app.jinja_env.globals['user_access'] = user_access + app.jinja_env.globals['ap_datetime'] = ap_datetime app.jinja_env.globals['can_create'] = can_create app.jinja_env.globals['can_upvote'] = can_upvote app.jinja_env.globals['can_downvote'] = can_downvote