Fix audience/addressing support

This commit is contained in:
Thomas Sileo 2022-06-26 19:00:29 +02:00
parent 6d756fb8c4
commit 33154b7e01
3 changed files with 30 additions and 24 deletions

View File

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

View File

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

View File

@ -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'<span class="h-card"><a href="{actor.url}" class="u-url mention">@<span>{username}</span>{d}</a></span>' # noqa: E501
link = f'<span class="h-card"><a href="{actor.url}" class="u-url mention">@{username}</a></span>' # 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