Improve summary

This commit is contained in:
Thomas Sileo 2022-08-24 20:12:10 +02:00
parent 3b767eae11
commit ae28cf2294
5 changed files with 35 additions and 30 deletions

View file

@ -12,6 +12,7 @@ from app import config
from app.config import AP_CONTENT_TYPE # noqa: F401 from app.config import AP_CONTENT_TYPE # noqa: F401
from app.httpsig import auth from app.httpsig import auth
from app.key import get_pubkey_as_pem from app.key import get_pubkey_as_pem
from app.source import hashtagify
from app.utils.url import check_url from app.utils.url import check_url
if TYPE_CHECKING: if TYPE_CHECKING:
@ -81,6 +82,8 @@ class VisibilityEnum(str, enum.Enum):
}[key] }[key]
_LOCAL_ACTOR_SUMMARY, _LOCAL_ACTOR_TAGS = hashtagify(config.CONFIG.summary)
ME = { ME = {
"@context": AS_EXTENDED_CTX, "@context": AS_EXTENDED_CTX,
"type": "Person", "type": "Person",
@ -92,7 +95,7 @@ ME = {
"outbox": config.BASE_URL + "/outbox", "outbox": config.BASE_URL + "/outbox",
"preferredUsername": config.USERNAME, "preferredUsername": config.USERNAME,
"name": config.CONFIG.name, "name": config.CONFIG.name,
"summary": config.CONFIG.summary, "summary": markdown(_LOCAL_ACTOR_SUMMARY, extensions=["mdx_linkify"]),
"endpoints": { "endpoints": {
# For compat with servers expecting a sharedInbox... # For compat with servers expecting a sharedInbox...
"sharedInbox": config.BASE_URL "sharedInbox": config.BASE_URL
@ -120,6 +123,7 @@ ME = {
"owner": config.ID, "owner": config.ID,
"publicKeyPem": get_pubkey_as_pem(config.KEY_PATH), "publicKeyPem": get_pubkey_as_pem(config.KEY_PATH),
}, },
"tag": _LOCAL_ACTOR_TAGS,
} }

View file

@ -215,10 +215,13 @@ async def httpsig_checker(
logger.exception(f'Failed to fetch HTTP sig key {hsig["keyId"]}') logger.exception(f'Failed to fetch HTTP sig key {hsig["keyId"]}')
return HTTPSigInfo(has_valid_signature=False) 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( httpsig_info = HTTPSigInfo(
has_valid_signature=_verify_h( has_valid_signature=has_valid_signature,
signed_string, base64.b64decode(hsig["signature"]), k.pubkey
),
signed_by_ap_actor_id=k.owner, signed_by_ap_actor_id=k.owner,
server=server, server=server,
) )

View file

@ -1,16 +1,17 @@
import re import re
import typing
from markdown import markdown from markdown import markdown
from sqlalchemy import select from sqlalchemy import select
from app import models
from app import webfinger from app import webfinger
from app.actor import Actor
from app.actor import fetch_actor
from app.config import BASE_URL from app.config import BASE_URL
from app.database import AsyncSession from app.database import AsyncSession
from app.utils import emoji from app.utils import emoji
if typing.TYPE_CHECKING:
from app.actor import Actor
def _set_a_attrs(attrs, new=False): def _set_a_attrs(attrs, new=False):
attrs[(None, "target")] = "_blank" 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\-.]+") _MENTION_REGEX = re.compile(r"@[\d\w_.+-]+@[\d\w-]+\.[\d\w\-.]+")
async def _hashtagify( def hashtagify(content: str) -> tuple[str, list[dict[str, str]]]:
db_session: AsyncSession, content: str
) -> tuple[str, list[dict[str, str]]]:
tags = [] tags = []
hashtags = re.findall(_HASHTAG_REGEX, content) hashtags = re.findall(_HASHTAG_REGEX, content)
hashtags = sorted(set(hashtags), reverse=True) # unique tags, longest first hashtags = sorted(set(hashtags), reverse=True) # unique tags, longest first
@ -41,7 +40,10 @@ async def _hashtagify(
async def _mentionify( async def _mentionify(
db_session: AsyncSession, db_session: AsyncSession,
content: str, 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 = [] tags = []
mentioned_actors = [] mentioned_actors = []
for mention in re.findall(_MENTION_REGEX, content): for mention in re.findall(_MENTION_REGEX, content):
@ -69,19 +71,19 @@ async def _mentionify(
async def markdownify( async def markdownify(
db_session: AsyncSession, db_session: AsyncSession,
content: str, content: str,
mentionify: bool = True, enable_mentionify: bool = True,
hashtagify: bool = True, enable_hashtagify: bool = True,
) -> tuple[str, list[dict[str, str]], list[Actor]]: ) -> tuple[str, list[dict[str, str]], list["Actor"]]:
""" """
>>> content, tags = markdownify("Hello") >>> content, tags = markdownify("Hello")
""" """
tags = [] tags = []
mentioned_actors: list[Actor] = [] mentioned_actors: list["Actor"] = []
if hashtagify: if enable_hashtagify:
content, hashtag_tags = await _hashtagify(db_session, content) content, hashtag_tags = hashtagify(content)
tags.extend(hashtag_tags) tags.extend(hashtag_tags)
if mentionify: if enable_mentionify:
content, mention_tags, mentioned_actors = await _mentionify(db_session, content) content, mention_tags, mentioned_actors = await _mentionify(db_session, content)
tags.extend(mention_tags) tags.extend(mention_tags)

View file

@ -6,7 +6,6 @@ from typing import Any
import bcrypt import bcrypt
import tomli_w import tomli_w
from markdown import markdown # type: ignore
from app.key import generate_key from app.key import generate_key
@ -44,7 +43,7 @@ def setup_config_file(
dat["username"] = username dat["username"] = username
dat["admin_password"] = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() dat["admin_password"] = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
dat["name"] = name dat["name"] = name
dat["summary"] = markdown(summary) dat["summary"] = summary
dat["https"] = True dat["https"] = True
proto = "https" proto = "https"
dat["icon_url"] = f'{proto}://{dat["domain"]}/static/nopic.png' dat["icon_url"] = f'{proto}://{dat["domain"]}/static/nopic.png'

View file

@ -6,7 +6,6 @@ from typing import Any
import bcrypt import bcrypt
import tomli_w import tomli_w
from markdown import markdown # type: ignore
from prompt_toolkit import prompt from prompt_toolkit import prompt
from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.key_binding import KeyBindings
@ -58,15 +57,13 @@ def main() -> None:
prompt("admin password: ", is_password=True).encode(), bcrypt.gensalt() prompt("admin password: ", is_password=True).encode(), bcrypt.gensalt()
).decode() ).decode()
dat["name"] = prompt("name (e.g. John Doe): ", default=dat["username"]) dat["name"] = prompt("name (e.g. John Doe): ", default=dat["username"])
dat["summary"] = markdown( dat["summary"] = prompt(
prompt( (
( "summary (short description, in markdown, "
"summary (short description, in markdown, " "press [CTRL] + [SPACE] to submit):\n"
"press [CTRL] + [SPACE] to submit):\n" ),
), key_bindings=_kb,
key_bindings=_kb, multiline=True,
multiline=True,
)
) )
dat["https"] = True dat["https"] = True
proto = "https" proto = "https"