From 33154b7e0144e1f48f18635fb90be0688adaf160 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 26 Jun 2022 19:00:29 +0200 Subject: [PATCH] Fix audience/addressing support --- app/boxes.py | 18 ++++++++---------- app/main.py | 13 ++++++++++--- app/source.py | 23 ++++++++++++----------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/app/boxes.py b/app/boxes.py index afdb6e5..b958087 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -234,7 +234,7 @@ def send_create( note_id = allocate_outbox_id() published = now().replace(microsecond=0).isoformat().replace("+00:00", "Z") context = f"{ID}/contexts/" + uuid.uuid4().hex - content, tags = markdownify(db, source) + content, tags, mentioned_actors = markdownify(db, source) attachments = [] if in_reply_to: @@ -253,23 +253,20 @@ def send_create( for (upload, filename) in uploads: attachments.append(upload_to_attachment(upload, filename)) - mentioned_actors = [ - mention["href"] for mention in tags if mention["type"] == "Mention" - ] - to = [] cc = [] + mentioned_actor_ap_ids = [actor.ap_id for actor in mentioned_actors] if visibility == ap.VisibilityEnum.PUBLIC: to = [ap.AS_PUBLIC] - cc = [f"{BASE_URL}/followers"] + mentioned_actors + cc = [f"{BASE_URL}/followers"] + mentioned_actor_ap_ids elif visibility == ap.VisibilityEnum.UNLISTED: to = [f"{BASE_URL}/followers"] - cc = [ap.AS_PUBLIC] + mentioned_actors + cc = [ap.AS_PUBLIC] + mentioned_actor_ap_ids elif visibility == ap.VisibilityEnum.FOLLOWERS_ONLY: to = [f"{BASE_URL}/followers"] - cc = mentioned_actors + cc = mentioned_actor_ap_ids elif visibility == ap.VisibilityEnum.DIRECT: - to = mentioned_actors + to = mentioned_actor_ap_ids cc = [] else: raise ValueError(f"Unhandled visibility {visibility}") @@ -326,6 +323,7 @@ def _compute_recipients(db: Session, ap_object: ap.RawObject) -> set[str]: _recipients.extend(ap.as_list(ap_object[field])) recipients = set() + logger.info(f"{_recipients}") for r in _recipients: if r in [ap.AS_PUBLIC, ID]: continue @@ -740,7 +738,7 @@ def save_to_inbox(db: Session, raw_object: ap.RawObject) -> None: ) db.add(notif) else: - raise ValueError("Should never happpen") + raise ValueError("Should never happen") else: logger.warning(f"Received an unknown {inbox_object.ap_type} object") diff --git a/app/main.py b/app/main.py index 995a2c1..8b14eff 100644 --- a/app/main.py +++ b/app/main.py @@ -409,8 +409,14 @@ def featured( def _check_outbox_object_acl( - db: Session, ap_object: models.OutboxObject, httpsig_info: httpsig.HTTPSigInfo + request: Request, + db: Session, + ap_object: models.OutboxObject, + httpsig_info: httpsig.HTTPSigInfo, ) -> None: + if templates.is_current_user_admin(request): + return None + if ap_object.visibility in [ ap.VisibilityEnum.PUBLIC, ap.VisibilityEnum.UNLISTED, @@ -451,7 +457,7 @@ def outbox_by_public_id( if not maybe_object: raise HTTPException(status_code=404) - _check_outbox_object_acl(db, maybe_object, httpsig_info) + _check_outbox_object_acl(request, db, maybe_object, httpsig_info) if is_activitypub_requested(request): return ActivityPubResponse(maybe_object.ap_object) @@ -472,6 +478,7 @@ def outbox_by_public_id( @app.get("/o/{public_id}/activity") def outbox_activity_by_public_id( public_id: str, + request: Request, db: Session = Depends(get_db), httpsig_info: httpsig.HTTPSigInfo = Depends(httpsig.httpsig_checker), ) -> ActivityPubResponse: @@ -486,7 +493,7 @@ def outbox_activity_by_public_id( if not maybe_object: raise HTTPException(status_code=404) - _check_outbox_object_acl(db, maybe_object, httpsig_info) + _check_outbox_object_acl(request, db, maybe_object, httpsig_info) return ActivityPubResponse(ap.wrap_object(maybe_object.ap_object)) diff --git a/app/source.py b/app/source.py index d71ff41..eacf67a 100644 --- a/app/source.py +++ b/app/source.py @@ -5,6 +5,7 @@ from sqlalchemy.orm import Session 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 @@ -34,9 +35,11 @@ def _hashtagify(db: Session, content: str) -> tuple[str, list[dict[str, str]]]: def _mentionify( - db: Session, content: str, hide_domain: bool = False -) -> tuple[str, list[dict[str, str]]]: + db: Session, + content: str, +) -> tuple[str, list[dict[str, str]], list[Actor]]: tags = [] + mentioned_actors = [] for mention in re.findall(_MENTION_REGEX, content): _, username, domain = mention.split("@") actor = ( @@ -49,15 +52,12 @@ def _mentionify( continue actor = fetch_actor(db, actor_url) + mentioned_actors.append(actor) tags.append(dict(type="Mention", href=actor.url, name=mention)) - d = f"@{domain}" - if hide_domain: - d = "" - - link = f'@{username}{d}' # noqa: E501 + link = f'@{username}' # noqa: E501 content = content.replace(mention, link) - return content, tags + return content, tags, mentioned_actors def markdownify( @@ -65,17 +65,18 @@ def markdownify( content: str, mentionify: bool = True, hashtagify: bool = True, -) -> tuple[str, list[dict[str, str]]]: +) -> tuple[str, list[dict[str, str]], list[Actor]]: """ >>> content, tags = markdownify("Hello") """ tags = [] + mentioned_actors: list[Actor] = [] if hashtagify: content, hashtag_tags = _hashtagify(db, content) tags.extend(hashtag_tags) if mentionify: - content, mention_tags = _mentionify(db, content) + content, mention_tags, mentioned_actors = _mentionify(db, content) tags.extend(mention_tags) content = markdown(content, extensions=["mdx_linkify"]) - return content, tags + return content, tags, mentioned_actors