From ae28cf229466b2ce9e32690ff93b07336a0b5cfe Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Wed, 24 Aug 2022 20:12:10 +0200 Subject: [PATCH] Improve summary --- app/activitypub.py | 6 +++++- app/httpsig.py | 9 ++++++--- app/source.py | 30 ++++++++++++++++-------------- app/utils/yunohost.py | 3 +-- scripts/config_wizard.py | 17 +++++++---------- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/app/activitypub.py b/app/activitypub.py index f879647..eb72b2f 100644 --- a/app/activitypub.py +++ b/app/activitypub.py @@ -12,6 +12,7 @@ from app import config from app.config import AP_CONTENT_TYPE # noqa: F401 from app.httpsig import auth from app.key import get_pubkey_as_pem +from app.source import hashtagify from app.utils.url import check_url if TYPE_CHECKING: @@ -81,6 +82,8 @@ class VisibilityEnum(str, enum.Enum): }[key] +_LOCAL_ACTOR_SUMMARY, _LOCAL_ACTOR_TAGS = hashtagify(config.CONFIG.summary) + ME = { "@context": AS_EXTENDED_CTX, "type": "Person", @@ -92,7 +95,7 @@ ME = { "outbox": config.BASE_URL + "/outbox", "preferredUsername": config.USERNAME, "name": config.CONFIG.name, - "summary": config.CONFIG.summary, + "summary": markdown(_LOCAL_ACTOR_SUMMARY, extensions=["mdx_linkify"]), "endpoints": { # For compat with servers expecting a sharedInbox... "sharedInbox": config.BASE_URL @@ -120,6 +123,7 @@ ME = { "owner": config.ID, "publicKeyPem": get_pubkey_as_pem(config.KEY_PATH), }, + "tag": _LOCAL_ACTOR_TAGS, } diff --git a/app/httpsig.py b/app/httpsig.py index 24515f6..495e0cb 100644 --- a/app/httpsig.py +++ b/app/httpsig.py @@ -215,10 +215,13 @@ async def httpsig_checker( logger.exception(f'Failed to fetch HTTP sig key {hsig["keyId"]}') return HTTPSigInfo(has_valid_signature=False) + has_valid_signature = _verify_h( + signed_string, base64.b64decode(hsig["signature"]), k.pubkey + ) + # FIXME: fetch/update the user if the signature is wrong + httpsig_info = HTTPSigInfo( - has_valid_signature=_verify_h( - signed_string, base64.b64decode(hsig["signature"]), k.pubkey - ), + has_valid_signature=has_valid_signature, signed_by_ap_actor_id=k.owner, server=server, ) diff --git a/app/source.py b/app/source.py index b555ceb..9a55c16 100644 --- a/app/source.py +++ b/app/source.py @@ -1,16 +1,17 @@ import re +import typing from markdown import markdown from sqlalchemy import select -from app import models from app import webfinger -from app.actor import Actor -from app.actor import fetch_actor from app.config import BASE_URL from app.database import AsyncSession from app.utils import emoji +if typing.TYPE_CHECKING: + from app.actor import Actor + def _set_a_attrs(attrs, new=False): attrs[(None, "target")] = "_blank" @@ -24,9 +25,7 @@ _HASHTAG_REGEX = re.compile(r"(#[\d\w]+)") _MENTION_REGEX = re.compile(r"@[\d\w_.+-]+@[\d\w-]+\.[\d\w\-.]+") -async def _hashtagify( - db_session: AsyncSession, content: str -) -> tuple[str, list[dict[str, str]]]: +def hashtagify(content: str) -> tuple[str, list[dict[str, str]]]: tags = [] hashtags = re.findall(_HASHTAG_REGEX, content) hashtags = sorted(set(hashtags), reverse=True) # unique tags, longest first @@ -41,7 +40,10 @@ async def _hashtagify( async def _mentionify( db_session: AsyncSession, content: str, -) -> tuple[str, list[dict[str, str]], list[Actor]]: +) -> tuple[str, list[dict[str, str]], list["Actor"]]: + from app import models + from app.actor import fetch_actor + tags = [] mentioned_actors = [] for mention in re.findall(_MENTION_REGEX, content): @@ -69,19 +71,19 @@ async def _mentionify( async def markdownify( db_session: AsyncSession, content: str, - mentionify: bool = True, - hashtagify: bool = True, -) -> tuple[str, list[dict[str, str]], list[Actor]]: + enable_mentionify: bool = True, + enable_hashtagify: bool = True, +) -> tuple[str, list[dict[str, str]], list["Actor"]]: """ >>> content, tags = markdownify("Hello") """ tags = [] - mentioned_actors: list[Actor] = [] - if hashtagify: - content, hashtag_tags = await _hashtagify(db_session, content) + mentioned_actors: list["Actor"] = [] + if enable_hashtagify: + content, hashtag_tags = hashtagify(content) tags.extend(hashtag_tags) - if mentionify: + if enable_mentionify: content, mention_tags, mentioned_actors = await _mentionify(db_session, content) tags.extend(mention_tags) diff --git a/app/utils/yunohost.py b/app/utils/yunohost.py index 9dc4f01..8b03e85 100644 --- a/app/utils/yunohost.py +++ b/app/utils/yunohost.py @@ -6,7 +6,6 @@ from typing import Any import bcrypt import tomli_w -from markdown import markdown # type: ignore from app.key import generate_key @@ -44,7 +43,7 @@ def setup_config_file( dat["username"] = username dat["admin_password"] = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() dat["name"] = name - dat["summary"] = markdown(summary) + dat["summary"] = summary dat["https"] = True proto = "https" dat["icon_url"] = f'{proto}://{dat["domain"]}/static/nopic.png' diff --git a/scripts/config_wizard.py b/scripts/config_wizard.py index eadcb9e..22119e6 100644 --- a/scripts/config_wizard.py +++ b/scripts/config_wizard.py @@ -6,7 +6,6 @@ from typing import Any import bcrypt import tomli_w -from markdown import markdown # type: ignore from prompt_toolkit import prompt from prompt_toolkit.key_binding import KeyBindings @@ -58,15 +57,13 @@ def main() -> None: prompt("admin password: ", is_password=True).encode(), bcrypt.gensalt() ).decode() dat["name"] = prompt("name (e.g. John Doe): ", default=dat["username"]) - dat["summary"] = markdown( - prompt( - ( - "summary (short description, in markdown, " - "press [CTRL] + [SPACE] to submit):\n" - ), - key_bindings=_kb, - multiline=True, - ) + dat["summary"] = prompt( + ( + "summary (short description, in markdown, " + "press [CTRL] + [SPACE] to submit):\n" + ), + key_bindings=_kb, + multiline=True, ) dat["https"] = True proto = "https"