basic seo

This commit is contained in:
rimu 2023-10-23 20:18:46 +13:00
parent 8737d3cbad
commit b630f76152
4 changed files with 31 additions and 5 deletions

View file

@ -14,7 +14,8 @@ from app.constants import SUBSCRIPTION_MEMBER, SUBSCRIPTION_OWNER, POST_TYPE_LIN
from app.models import User, Community, CommunityMember, CommunityJoinRequest, CommunityBan, Post, PostReply, \ from app.models import User, Community, CommunityMember, CommunityJoinRequest, CommunityBan, Post, PostReply, \
PostReplyVote, PostVote PostReplyVote, PostVote
from app.community import bp from app.community import bp
from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \
shorten_string, markdown_to_text
@bp.route('/add_local', methods=['GET', 'POST']) @bp.route('/add_local', methods=['GET', 'POST'])
@ -89,8 +90,12 @@ def show_community(community: Community):
else: else:
posts = community.posts posts = community.posts
description = shorten_string(community.description, 150) if community.description else None
og_image = community.image.source_url if community.image_id else None
return render_template('community/community.html', community=community, title=community.title, return render_template('community/community.html', community=community, title=community.title,
is_moderator=is_moderator, is_owner=is_owner, mods=mod_list, posts=posts) is_moderator=is_moderator, is_owner=is_owner, mods=mod_list, posts=posts, description=description,
og_image=og_image)
@bp.route('/<actor>/subscribe', methods=['GET']) @bp.route('/<actor>/subscribe', methods=['GET'])
@ -248,8 +253,13 @@ def show_post(post_id: int):
post_id=post_id)) # redirect to current page to avoid refresh resubmitting the form post_id=post_id)) # redirect to current page to avoid refresh resubmitting the form
else: else:
replies = post_replies(post.id, 'top') replies = post_replies(post.id, 'top')
og_image = post.image.source_url if post.image_id else None
description = shorten_string(markdown_to_text(post.body), 150) if post.body else None
return render_template('community/post.html', title=post.title, post=post, is_moderator=is_moderator, return render_template('community/post.html', title=post.title, post=post, is_moderator=is_moderator,
canonical=post.ap_id, form=form, replies=replies, THREAD_CUTOFF_DEPTH=constants.THREAD_CUTOFF_DEPTH) canonical=post.ap_id, form=form, replies=replies, THREAD_CUTOFF_DEPTH=constants.THREAD_CUTOFF_DEPTH,
description=description, og_image=og_image)
@bp.route('/post/<int:post_id>/<vote_direction>', methods=['GET', 'POST']) @bp.route('/post/<int:post_id>/<vote_direction>', methods=['GET', 'POST'])

View file

@ -36,6 +36,12 @@
{% if canonical %} {% if canonical %}
<link rel="canonical" href="{{ canonical }}" /> <link rel="canonical" href="{{ canonical }}" />
{% endif %} {% endif %}
{% if description %}
<meta name="description" content="{{ description }}" />
{% endif %}
{% if og_image %}
<meta property="og:image" content="{{ og_image }}" />
{% endif %}
{% endblock %} {% endblock %}
</head> </head>
<body class="d-flex flex-column" style="padding-top: 43px;"> <body class="d-flex flex-column" style="padding-top: 43px;">

View file

@ -6,7 +6,7 @@ from app import db
from app.models import Post, Community, CommunityMember, User, PostReply from app.models import Post, Community, CommunityMember, User, PostReply
from app.user import bp from app.user import bp
from app.user.forms import ProfileForm, SettingsForm from app.user.forms import ProfileForm, SettingsForm
from app.utils import get_setting, render_template, markdown_to_html, user_access from app.utils import get_setting, render_template, markdown_to_html, user_access, markdown_to_text, shorten_string
from sqlalchemy import desc, or_ from sqlalchemy import desc, or_
@ -20,9 +20,11 @@ def show_profile(user):
post_replies = PostReply.query.filter_by(user_id=user.id).order_by(desc(PostReply.posted_at)).all() post_replies = PostReply.query.filter_by(user_id=user.id).order_by(desc(PostReply.posted_at)).all()
canonical = user.ap_public_url if user.ap_public_url else None canonical = user.ap_public_url if user.ap_public_url else None
user.about_html = markdown_to_html(user.about) user.about_html = markdown_to_html(user.about)
description = shorten_string(markdown_to_text(user.about), 150) if user.about else None
return render_template('user/show_profile.html', user=user, posts=posts, post_replies=post_replies, return render_template('user/show_profile.html', user=user, posts=posts, post_replies=post_replies,
moderates=moderates.all(), canonical=canonical, title=_('Posts by %(user_name)s', moderates=moderates.all(), canonical=canonical, title=_('Posts by %(user_name)s',
user_name=user.user_name)) user_name=user.user_name),
description=description)
@bp.route('/u/<actor>/profile', methods=['GET', 'POST']) @bp.route('/u/<actor>/profile', methods=['GET', 'POST'])
@ -32,6 +34,8 @@ def edit_profile(actor):
user = User.query.filter_by(user_name=actor, deleted=False, banned=False, ap_id=None).first() user = User.query.filter_by(user_name=actor, deleted=False, banned=False, ap_id=None).first()
if user is None: if user is None:
abort(404) abort(404)
if current_user.id != user.id:
abort(401)
form = ProfileForm() form = ProfileForm()
if form.validate_on_submit(): if form.validate_on_submit():
current_user.email = form.email.data current_user.email = form.email.data
@ -57,6 +61,8 @@ def change_settings(actor):
user = User.query.filter_by(user_name=actor, deleted=False, banned=False, ap_id=None).first() user = User.query.filter_by(user_name=actor, deleted=False, banned=False, ap_id=None).first()
if user is None: if user is None:
abort(404) abort(404)
if current_user.id != user.id:
abort(401)
form = SettingsForm() form = SettingsForm()
if form.validate_on_submit(): if form.validate_on_submit():
current_user.newsletter = form.newsletter.data current_user.newsletter = form.newsletter.data

View file

@ -152,6 +152,10 @@ def markdown_to_html(markdown_text) -> str:
return '' return ''
def markdown_to_text(markdown_text) -> str:
return markdown_text.replace("# ", '')
def domain_from_url(url: str) -> Domain: def domain_from_url(url: str) -> Domain:
parsed_url = urlparse(url) parsed_url = urlparse(url)
domain = Domain.query.filter_by(name=parsed_url.hostname.lower()).first() domain = Domain.query.filter_by(name=parsed_url.hostname.lower()).first()