Improved CW/sensitive support

main
Thomas Sileo 2022-06-28 21:10:22 +02:00
parent d4c80dedeb
commit 626a165411
6 changed files with 97 additions and 36 deletions

View File

@ -199,12 +199,22 @@ def admin_inbox(
else None
)
actors_metadata = get_actors_metadata(
db,
[
inbox_object.actor
for inbox_object in inbox
if inbox_object.ap_type == "Follow"
],
)
return templates.render_template(
db,
request,
"admin_inbox.html",
{
"inbox": inbox,
"actors_metadata": actors_metadata,
"next_cursor": next_cursor,
},
)
@ -475,6 +485,8 @@ def admin_actions_new(
content: str = Form(),
redirect_url: str = Form(),
in_reply_to: str | None = Form(None),
content_warning: str | None = Form(None),
is_sensitive: bool = Form(False),
visibility: str = Form(),
csrf_check: None = Depends(verify_csrf_token),
db: Session = Depends(get_db),
@ -491,6 +503,8 @@ def admin_actions_new(
uploads=uploads,
in_reply_to=in_reply_to or None,
visibility=ap.VisibilityEnum[visibility],
content_warning=content_warning or None,
is_sensitive=True if content_warning else is_sensitive,
)
return RedirectResponse(
request.url_for("outbox_by_public_id", public_id=public_id),

View File

@ -131,6 +131,10 @@ class Object:
return content
@property
def summary(self) -> str | None:
return self.ap_object.get("summary")
@property
def permalink_id(self) -> str:
return (

View File

@ -231,6 +231,8 @@ def send_create(
uploads: list[tuple[models.Upload, str]],
in_reply_to: str | None,
visibility: ap.VisibilityEnum,
content_warning: str | None = None,
is_sensitive: bool = False,
) -> str:
note_id = allocate_outbox_id()
published = now().replace(microsecond=0).isoformat().replace("+00:00", "Z")
@ -285,9 +287,9 @@ def send_create(
"conversation": context,
"url": outbox_object_id(note_id),
"tag": tags,
"summary": None,
"summary": content_warning,
"inReplyTo": in_reply_to,
"sensitive": False,
"sensitive": is_sensitive,
"attachment": attachments,
}
outbox_object = save_outbox_object(db, note_id, note, source=source)
@ -550,10 +552,9 @@ def _handle_create_activity(
def save_to_inbox(db: Session, raw_object: ap.RawObject) -> None:
try:
actor = fetch_actor(db, raw_object["actor"])
actor = fetch_actor(db, ap.get_id(raw_object["actor"]))
except httpx.HTTPStatusError:
logger.exception("Failed to fetch actor")
# XXX: Delete 410 when we never seen the actor
return
ap_published_at = now()
@ -561,6 +562,16 @@ def save_to_inbox(db: Session, raw_object: ap.RawObject) -> None:
ap_published_at = isoparse(raw_object["published"])
ra = RemoteObject(ap.unwrap_activity(raw_object), actor=actor)
if (
db.query(models.InboxObject)
.filter(models.InboxObject.ap_id == ra.ap_id)
.count()
> 0
):
logger.info(f"Received duplicate {ra.ap_type} activity: {ra.ap_id}")
return
relates_to_inbox_object: models.InboxObject | None = None
relates_to_outbox_object: models.OutboxObject | None = None
if ra.activity_object_ap_id:

View File

@ -10,7 +10,11 @@
{% elif inbox_object.ap_type in ["Article", "Note", "Video"] %}
{{ utils.display_object(inbox_object) }}
{% elif inbox_object.ap_type == "Follow" %}
{{ utils.display_object(inbox_object) }}
<div class="actor-action">
{{ inbox_object.actor.display_name }} followed you
<span>{{ inbox_object.ap_published_at | timeago }}</span>
</div>
{{ utils.display_actor(inbox_object.actor, actors_metadata) }}
{% else %}
Implement {{ inbox_object.ap_type }}
{% endif %}

View File

@ -25,6 +25,12 @@
{% endfor %}
<textarea name="content" rows="10" cols="50" autofocus="autofocus" designMode="on" style="font-size:1.2em;width:95%;">{{ content }}</textarea>
<p>
<input type="text" name="content_warning" placeholder="content warning (will mark the post as sensitive)" style="width:300px;">
</p>
<p>
<input type="checkbox" name="is_sensitive" id="is_sensitive"> <label for="is_sensitive">Mark attachment(s) as sentive</label>
</p>
<input type="hidden" name="in_reply_to" value="{{ request.query_params.in_reply_to }}">
<p>
<input name="files" type="file" multiple>

View File

@ -88,6 +88,17 @@
</form>
{% endmacro %}
{% macro show_more_button(permalink_id) %}
<form action="" method="GET">
<input type="hidden" name="show_more" value="{{ permalink_id }}">
{% for k, v in request.query_params.items() %}
<input type="hidden" name="{{k}}" value="{{v}}">
{% endfor %}
<button type="submit">show more</button>
</form>
{% endmacro %}
{% macro admin_reply_button(ap_object_id) %}
<form action="/admin/new" method="GET">
<input type="hidden" name="in_reply_to" value="{{ ap_object_id }}">
@ -157,15 +168,43 @@
{% endmacro %}
{% macro display_attachments(object) %}
{% if object.attachments and object.sensitive and not request.query_params.show_sensitive == object.permalink_id %}
{{ sensitive_button(object.permalink_id )}}
{% endif %}
{% if object.attachments and (not object.sensitive or (object.sensitive and request.query_params["show_sensitive"] == object.permalink_id)) %}
{% for attachment in object.attachments %}
{% if attachment.type == "Image" or (attachment | has_media_type("image")) %}
<img src="{{ attachment.resized_url or attachment.proxied_url }}"{% if attachment.name %} alt="{{ attachment.name }}"{% endif %} class="attachment">
{% elif attachment.type == "Video" or (attachment | has_media_type("video")) %}
<video controls preload="metadata" src="{{ attachment.url | media_proxy_url }}"{% if attachment.name %} title="{{ attachment.name }}"{% endif %} class="attachmeent"></video>
{% elif attachment.type == "Audio" or (attachment | has_media_type("audio")) %}
<audio controls preload="metadata" src="{{ attachment.url | media_proxy_url }}"{% if attachment.name%} title="{{ attachment.name }}"{% endif %} style="width:480px;" class="attachment"></audio>
{% else %}
<a href="{{ attachment.url | media_proxy_url }}"{% if attachment.name %} title="{{ attachment.name }}"{% endif %} class="attachment">{{ attachment.url }}</a>
{% endif %}
{% endfor %}
{% endif %}
{% endmacro %}
{% macro display_object_expanded(object) %}
<div class="activity-expanded h-entry">
{{ display_actor(object.actor, {}) }}
{% if object.summary %}
<p class="p-summary">{{ object.summary | clean_html(object) | safe }}</p>
{% endif %}
{% if object.sensitive and object.summary and not request.query_params.show_more == object.permalink_id %}
{{ show_more_button(object.permalink_id) }}
{% endif %}
{% if not object.sensitive or (object.sensitive and object.summary and request.query_params.show_more == object.permalink_id) %}
<div class="e-content">
{{ object.content | clean_html(object) | safe }}
</div>
{% endif %}
<a href="{{ object.url }}" class="u-url u-uid"><time class="dt-published" datetime="{{ object.ap_published_at }}">{{ object.ap_published_at | format_date }}</time></a>
{{ object.visibility.value }}
@ -177,6 +216,7 @@
{{ object.announces_count }} shares
{% endif %}
{{ display_attachments(object) }}
</div>
@ -194,32 +234,24 @@
{{ object.visibility.value }}
<a href="{{ object.url }}" class="u-url u-uid"><time class="dt-published" datetime="{{ object.ap_published_at }}">{{ object.ap_published_at | timeago }}</time></a>
</span>
{% if object.summary %}
<p class="p-summary">{{ object.summary | clean_html(object) | safe }}</p>
{% endif %}
{% if object.sensitive and object.summary and not request.query_params.show_more == object.permalink_id %}
{{ show_more_button(object.permalink_id) }}
{% endif %}
{% if not object.sensitive or (object.sensitive and object.summary and request.query_params.show_more == object.permalink_id) %}
<div class="activity-main e-content">
{{ object.content | clean_html(object) | safe }}
</div>
</div>
</div>
{% if object.attachments and object.sensitive and not request.query_params["show_sensitive"] == object.permalink_id %}
<div class="activity-attachment">
{{ sensitive_button(object.permalink_id )}}
</div>
{% endif %}
</div>
</div>
{% if object.attachments and (not object.sensitive or (object.sensitive and request.query_params["show_sensitive"] == object.permalink_id)) %}
<div class="activity-attachment">
{% for attachment in object.attachments %}
{% if attachment.type == "Image" or (attachment | has_media_type("image")) %}
<img src="{{ attachment.resized_url or attachment.proxied_url }}"{% if attachment.name %} alt="{{ attachment.name }}"{% endif %} class="attachment">
{% elif attachment.type == "Video" or (attachment | has_media_type("video")) %}
<video controls preload="metadata" src="{{ attachment.url | media_proxy_url }}"{% if attachment.name %} title="{{ attachment.name }}"{% endif %} class="attachmeent"></video>
{% elif attachment.type == "Audio" or (attachment | has_media_type("audio")) %}
<audio controls preload="metadata" src="{{ attachment.url | media_proxy_url }}"{% if attachment.name%} title="{{ attachment.name }}"{% endif %} style="width:480px;" class="attachment"></audio>
{% else %}
<a href="{{ attachment.url | media_proxy_url }}"{% if attachment.name %} title="{{ attachment.name }}"{% endif %} class="attachment">{{ attachment.url }}</a>
{% endif %}
{% endfor %}
{{ display_attachments(object) }}
</div>
{% endif %}
<div class="activity-bar">
{% if object.is_from_outbox %}
<div class="bar-item">
@ -285,15 +317,5 @@
</div>
</div>
{% elif object.ap_type == "Follow" %}
{% if object.is_from_inbox %}
<div class="actor-action">
{{ object.actor.display_name }} followed you
<span>{{ object.ap_published_at | timeago }}</span>
</div>
{{ display_actor(object.actor, {}) }}
{% endif %}
{% endif %}
{% endmacro %}