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() note_id = allocate_outbox_id()
published = now().replace(microsecond=0).isoformat().replace("+00:00", "Z") published = now().replace(microsecond=0).isoformat().replace("+00:00", "Z")
context = f"{ID}/contexts/" + uuid.uuid4().hex context = f"{ID}/contexts/" + uuid.uuid4().hex
content, tags = markdownify(db, source) content, tags, mentioned_actors = markdownify(db, source)
attachments = [] attachments = []
if in_reply_to: if in_reply_to:
@ -253,23 +253,20 @@ def send_create(
for (upload, filename) in uploads: for (upload, filename) in uploads:
attachments.append(upload_to_attachment(upload, filename)) attachments.append(upload_to_attachment(upload, filename))
mentioned_actors = [
mention["href"] for mention in tags if mention["type"] == "Mention"
]
to = [] to = []
cc = [] cc = []
mentioned_actor_ap_ids = [actor.ap_id for actor in mentioned_actors]
if visibility == ap.VisibilityEnum.PUBLIC: if visibility == ap.VisibilityEnum.PUBLIC:
to = [ap.AS_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: elif visibility == ap.VisibilityEnum.UNLISTED:
to = [f"{BASE_URL}/followers"] 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: elif visibility == ap.VisibilityEnum.FOLLOWERS_ONLY:
to = [f"{BASE_URL}/followers"] to = [f"{BASE_URL}/followers"]
cc = mentioned_actors cc = mentioned_actor_ap_ids
elif visibility == ap.VisibilityEnum.DIRECT: elif visibility == ap.VisibilityEnum.DIRECT:
to = mentioned_actors to = mentioned_actor_ap_ids
cc = [] cc = []
else: else:
raise ValueError(f"Unhandled visibility {visibility}") 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.extend(ap.as_list(ap_object[field]))
recipients = set() recipients = set()
logger.info(f"{_recipients}")
for r in _recipients: for r in _recipients:
if r in [ap.AS_PUBLIC, ID]: if r in [ap.AS_PUBLIC, ID]:
continue continue
@ -740,7 +738,7 @@ def save_to_inbox(db: Session, raw_object: ap.RawObject) -> None:
) )
db.add(notif) db.add(notif)
else: else:
raise ValueError("Should never happpen") raise ValueError("Should never happen")
else: else:
logger.warning(f"Received an unknown {inbox_object.ap_type} object") logger.warning(f"Received an unknown {inbox_object.ap_type} object")

View file

@ -409,8 +409,14 @@ def featured(
def _check_outbox_object_acl( 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: ) -> None:
if templates.is_current_user_admin(request):
return None
if ap_object.visibility in [ if ap_object.visibility in [
ap.VisibilityEnum.PUBLIC, ap.VisibilityEnum.PUBLIC,
ap.VisibilityEnum.UNLISTED, ap.VisibilityEnum.UNLISTED,
@ -451,7 +457,7 @@ def outbox_by_public_id(
if not maybe_object: if not maybe_object:
raise HTTPException(status_code=404) 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): if is_activitypub_requested(request):
return ActivityPubResponse(maybe_object.ap_object) return ActivityPubResponse(maybe_object.ap_object)
@ -472,6 +478,7 @@ def outbox_by_public_id(
@app.get("/o/{public_id}/activity") @app.get("/o/{public_id}/activity")
def outbox_activity_by_public_id( def outbox_activity_by_public_id(
public_id: str, public_id: str,
request: Request,
db: Session = Depends(get_db), db: Session = Depends(get_db),
httpsig_info: httpsig.HTTPSigInfo = Depends(httpsig.httpsig_checker), httpsig_info: httpsig.HTTPSigInfo = Depends(httpsig.httpsig_checker),
) -> ActivityPubResponse: ) -> ActivityPubResponse:
@ -486,7 +493,7 @@ def outbox_activity_by_public_id(
if not maybe_object: if not maybe_object:
raise HTTPException(status_code=404) 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)) 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 models
from app import webfinger from app import webfinger
from app.actor import Actor
from app.actor import fetch_actor from app.actor import fetch_actor
from app.config import BASE_URL from app.config import BASE_URL
@ -34,9 +35,11 @@ def _hashtagify(db: Session, content: str) -> tuple[str, list[dict[str, str]]]:
def _mentionify( def _mentionify(
db: Session, content: str, hide_domain: bool = False db: Session,
) -> tuple[str, list[dict[str, str]]]: content: str,
) -> tuple[str, list[dict[str, str]], list[Actor]]:
tags = [] tags = []
mentioned_actors = []
for mention in re.findall(_MENTION_REGEX, content): for mention in re.findall(_MENTION_REGEX, content):
_, username, domain = mention.split("@") _, username, domain = mention.split("@")
actor = ( actor = (
@ -49,15 +52,12 @@ def _mentionify(
continue continue
actor = fetch_actor(db, actor_url) actor = fetch_actor(db, actor_url)
mentioned_actors.append(actor)
tags.append(dict(type="Mention", href=actor.url, name=mention)) tags.append(dict(type="Mention", href=actor.url, name=mention))
d = f"@{domain}" link = f'<span class="h-card"><a href="{actor.url}" class="u-url mention">@{username}</a></span>' # noqa: E501
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
content = content.replace(mention, link) content = content.replace(mention, link)
return content, tags return content, tags, mentioned_actors
def markdownify( def markdownify(
@ -65,17 +65,18 @@ def markdownify(
content: str, content: str,
mentionify: bool = True, mentionify: bool = True,
hashtagify: bool = True, hashtagify: bool = True,
) -> tuple[str, list[dict[str, str]]]: ) -> tuple[str, list[dict[str, str]], list[Actor]]:
""" """
>>> content, tags = markdownify("Hello") >>> content, tags = markdownify("Hello")
""" """
tags = [] tags = []
mentioned_actors: list[Actor] = []
if hashtagify: if hashtagify:
content, hashtag_tags = _hashtagify(db, content) content, hashtag_tags = _hashtagify(db, content)
tags.extend(hashtag_tags) tags.extend(hashtag_tags)
if mentionify: if mentionify:
content, mention_tags = _mentionify(db, content) content, mention_tags, mentioned_actors = _mentionify(db, content)
tags.extend(mention_tags) tags.extend(mention_tags)
content = markdown(content, extensions=["mdx_linkify"]) content = markdown(content, extensions=["mdx_linkify"])
return content, tags return content, tags, mentioned_actors