sitemap of local posts

This commit is contained in:
rimu 2024-01-24 17:02:48 +13:00
parent f6a0dfdc4f
commit 75f2c8d2ef
3 changed files with 28 additions and 1 deletions

View file

@ -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')

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for post in posts %}
<url>
<loc>https://{{ current_app.config['SERVER_NAME'] }}/post/{{ post.id }}</loc>
<lastmod>{{ ap_datetime(post.edited_at) if post.edited_at else ap_datetime(post.posted_at) }}</lastmod>
</url>
{% endfor %}
</urlset>

View file

@ -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