mirror of
https://github.com/svemagie/indiekit-endpoint-activitypub.git
synced 2026-04-02 15:44:58 +02:00
Extract quoteUrl from Fedify Note objects (supports Mastodon, Misskey, Fedibird quote formats). Fetch quoted post data asynchronously on inbox receive and on-demand in post detail view. Render as rich embed card with author avatar, handle, content, and timestamp. Confab-Link: http://localhost:8080/sessions/e9d666ac-3c90-4298-9e92-9ac9d142bc06
265 lines
11 KiB
Plaintext
265 lines
11 KiB
Plaintext
{# Timeline item card partial - reusable across timeline and profile views #}
|
|
|
|
{# Skip empty cards (e.g. Lemmy/PieFed activity IDs with no actual content) #}
|
|
{% set hasCardContent = item.content and (item.content.html or item.content.text) %}
|
|
{% set hasCardTitle = item.name %}
|
|
{% set hasCardMedia = (item.photo and item.photo.length > 0) or (item.video and item.video.length > 0) or (item.audio and item.audio.length > 0) %}
|
|
{% if hasCardContent or hasCardTitle or hasCardMedia %}
|
|
|
|
<article class="ap-card{% if item.type %} ap-card--{{ item.type }}{% endif %}{% if item.inReplyTo %} ap-card--reply{% endif %}{% if item._moderated %} ap-card--moderated{% endif %}">
|
|
{# Moderation content warning wrapper #}
|
|
{% if item._moderated %}
|
|
{% if item._moderationReason == "muted_account" %}
|
|
{% set modLabel = __("activitypub.moderation.cwMutedAccount") %}
|
|
{% elif item._moderationReason == "muted_keyword" and item._moderationKeyword %}
|
|
{% set modLabel = __("activitypub.moderation.cwMutedKeyword") + ' "' + item._moderationKeyword + '"' %}
|
|
{% else %}
|
|
{% set modLabel = __("activitypub.moderation.cwFiltered") %}
|
|
{% endif %}
|
|
<div class="ap-card__moderation-cw" x-data="{ shown: false }">
|
|
<button @click="shown = !shown" class="ap-card__moderation-toggle">
|
|
<span x-show="!shown">🛡️ {{ modLabel }} — {{ __("activitypub.reader.showContent") }}</span>
|
|
<span x-show="shown" x-cloak>🛡️ {{ modLabel }} — {{ __("activitypub.reader.hideContent") }}</span>
|
|
</button>
|
|
<div x-show="shown" x-cloak>
|
|
{% endif %}
|
|
{# Boost header if this is a boosted post #}
|
|
{% if item.type == "boost" and item.boostedBy %}
|
|
<div class="ap-card__boost">
|
|
🔁 {% if item.boostedBy.url %}<a href="{{ mountPath }}/admin/reader/profile?url={{ item.boostedBy.url | urlencode }}">{{ item.boostedBy.name or "Someone" }}</a>{% else %}{{ item.boostedBy.name or "Someone" }}{% endif %} {{ __("activitypub.reader.boosted") }}
|
|
</div>
|
|
{% endif %}
|
|
|
|
{# Reply context if this is a reply #}
|
|
{% if item.inReplyTo %}
|
|
<div class="ap-card__reply-to">
|
|
↩ {{ __("activitypub.reader.replyingTo") }} <a href="{{ mountPath }}/admin/reader/post?url={{ item.inReplyTo | urlencode }}">{{ item.inReplyTo }}</a>
|
|
</div>
|
|
{% endif %}
|
|
|
|
{# Author header #}
|
|
<header class="ap-card__author">
|
|
{% if item.author.photo %}
|
|
<img src="{{ item.author.photo }}" alt="{{ item.author.name }}" class="ap-card__avatar" loading="lazy" crossorigin="anonymous"
|
|
onerror="this.style.display='none';this.nextElementSibling.style.display=''">
|
|
<span class="ap-card__avatar ap-card__avatar--default" style="display:none" aria-hidden="true">{{ item.author.name[0] | upper if item.author.name else "?" }}</span>
|
|
{% else %}
|
|
<span class="ap-card__avatar ap-card__avatar--default" aria-hidden="true">{{ item.author.name[0] | upper if item.author.name else "?" }}</span>
|
|
{% endif %}
|
|
<div class="ap-card__author-info">
|
|
<div class="ap-card__author-name">
|
|
{% if item.author.url %}
|
|
<a href="{{ mountPath }}/admin/reader/profile?url={{ item.author.url | urlencode }}">{{ item.author.name or "Unknown" }}</a>
|
|
{% else %}
|
|
<span>{{ item.author.name or "Unknown" }}</span>
|
|
{% endif %}
|
|
</div>
|
|
{% if item.author.handle %}
|
|
<div class="ap-card__author-handle">{{ item.author.handle }}</div>
|
|
{% endif %}
|
|
</div>
|
|
{% if item.published %}
|
|
<a href="{{ mountPath }}/admin/reader/post?url={{ (item.uid or item.url) | urlencode }}" class="ap-card__timestamp-link" title="{{ __('activitypub.reader.post.title') }}">
|
|
<time datetime="{{ item.published }}" class="ap-card__timestamp">
|
|
{{ item.published | date("PPp") }}
|
|
</time>
|
|
</a>
|
|
{% endif %}
|
|
</header>
|
|
|
|
{# Post title (articles only) #}
|
|
{% if item.name %}
|
|
<h2 class="ap-card__title">
|
|
<a href="{{ mountPath }}/admin/reader/post?url={{ item.uid | urlencode }}">{{ item.name }}</a>
|
|
</h2>
|
|
{% endif %}
|
|
|
|
{# Determine if content should be hidden behind CW #}
|
|
{% set hasCW = item.summary or item.sensitive %}
|
|
{% set cwLabel = item.summary if item.summary else __("activitypub.reader.sensitiveContent") %}
|
|
|
|
{% if hasCW %}
|
|
<div class="ap-card__cw" x-data="{ shown: false }">
|
|
<button @click="shown = !shown" class="ap-card__cw-toggle">
|
|
<span x-show="!shown">⚠️ {{ cwLabel }} — {{ __("activitypub.reader.showContent") }}</span>
|
|
<span x-show="shown" x-cloak>{{ __("activitypub.reader.hideContent") }}</span>
|
|
</button>
|
|
<div x-show="shown" x-cloak>
|
|
{% if item.content and item.content.html %}
|
|
<div class="ap-card__content">
|
|
{{ item.content.html | safe }}
|
|
</div>
|
|
{% endif %}
|
|
|
|
{# Quoted post embed #}
|
|
{% include "partials/ap-quote-embed.njk" %}
|
|
|
|
{# Link previews #}
|
|
{% include "partials/ap-link-preview.njk" %}
|
|
|
|
{# Media hidden behind CW #}
|
|
{% include "partials/ap-item-media.njk" %}
|
|
</div>
|
|
</div>
|
|
{% else %}
|
|
{# Regular content (no CW) #}
|
|
{% if item.content and item.content.html %}
|
|
<div class="ap-card__content">
|
|
{{ item.content.html | safe }}
|
|
</div>
|
|
{% endif %}
|
|
|
|
{# Quoted post embed #}
|
|
{% include "partials/ap-quote-embed.njk" %}
|
|
|
|
{# Link previews #}
|
|
{% include "partials/ap-link-preview.njk" %}
|
|
|
|
{# Media visible directly #}
|
|
{% include "partials/ap-item-media.njk" %}
|
|
{% endif %}
|
|
|
|
{# Mentions and hashtags #}
|
|
{% set hasMentions = item.mentions and item.mentions.length > 0 %}
|
|
{% set hasHashtags = item.category and item.category.length > 0 %}
|
|
{% if hasMentions or hasHashtags %}
|
|
<div class="ap-card__tags">
|
|
{# Mentions — render with @ prefix, link to profile view when URL available #}
|
|
{% if hasMentions %}
|
|
{% for mention in item.mentions %}
|
|
{% if mention.url %}
|
|
<a href="{{ mountPath }}/admin/reader/profile?url={{ mention.url | urlencode }}" class="ap-card__mention">@{{ mention.name }}</a>
|
|
{% else %}
|
|
<span class="ap-card__mention ap-card__mention--legacy">@{{ mention.name }}</span>
|
|
{% endif %}
|
|
{% endfor %}
|
|
{% endif %}
|
|
{# Hashtags — render with # prefix, link to tag timeline #}
|
|
{% if hasHashtags %}
|
|
{% for tag in item.category %}
|
|
<a href="{{ mountPath }}/admin/reader/tag?tag={{ tag | urlencode }}" class="ap-card__tag">#{{ tag }}</a>
|
|
{% endfor %}
|
|
{% endif %}
|
|
</div>
|
|
{% endif %}
|
|
|
|
{# Interaction buttons — Alpine.js for optimistic updates #}
|
|
{# Use canonical AP uid for interactions (Fedify lookupObject), display url for links #}
|
|
{% set itemUrl = item.url or item.originalUrl %}
|
|
{% set itemUid = item.uid or item.url or item.originalUrl %}
|
|
{% set isLiked = interactionMap[itemUid].like if interactionMap[itemUid] else false %}
|
|
{% set isBoosted = interactionMap[itemUid].boost if interactionMap[itemUid] else false %}
|
|
<footer class="ap-card__actions"
|
|
data-item-uid="{{ itemUid }}"
|
|
data-item-url="{{ itemUrl }}"
|
|
data-csrf-token="{{ csrfToken }}"
|
|
data-mount-path="{{ mountPath }}"
|
|
x-data="{
|
|
liked: {{ 'true' if isLiked else 'false' }},
|
|
boosted: {{ 'true' if isBoosted else 'false' }},
|
|
saved: false,
|
|
loading: false,
|
|
error: '',
|
|
async saveLater() {
|
|
if (this.saved) return;
|
|
const el = this.$root;
|
|
const itemUrl = el.dataset.itemUrl;
|
|
try {
|
|
const res = await fetch('/readlater/save', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
url: itemUrl,
|
|
title: el.closest('article')?.querySelector('p')?.textContent?.substring(0, 80) || itemUrl,
|
|
source: 'activitypub'
|
|
}),
|
|
credentials: 'same-origin'
|
|
});
|
|
if (res.ok) this.saved = true;
|
|
else this.error = 'Failed to save';
|
|
} catch (e) {
|
|
this.error = e.message;
|
|
}
|
|
if (this.error) setTimeout(() => this.error = '', 3000);
|
|
},
|
|
async interact(action) {
|
|
if (this.loading) return;
|
|
this.loading = true;
|
|
this.error = '';
|
|
const el = this.$root;
|
|
const itemUid = el.dataset.itemUid;
|
|
const csrfToken = el.dataset.csrfToken;
|
|
const basePath = el.dataset.mountPath;
|
|
const prev = { liked: this.liked, boosted: this.boosted };
|
|
if (action === 'like') this.liked = true;
|
|
else if (action === 'unlike') this.liked = false;
|
|
else if (action === 'boost') this.boosted = true;
|
|
else if (action === 'unboost') this.boosted = false;
|
|
try {
|
|
const res = await fetch(basePath + '/admin/reader/' + action, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-Token': csrfToken
|
|
},
|
|
body: JSON.stringify({ url: itemUid })
|
|
});
|
|
const data = await res.json();
|
|
if (!data.success) {
|
|
this.liked = prev.liked;
|
|
this.boosted = prev.boosted;
|
|
this.error = data.error || 'Failed';
|
|
}
|
|
} catch (e) {
|
|
this.liked = prev.liked;
|
|
this.boosted = prev.boosted;
|
|
this.error = e.message;
|
|
}
|
|
this.loading = false;
|
|
if (this.error) setTimeout(() => this.error = '', 3000);
|
|
}
|
|
}">
|
|
<a href="{{ mountPath }}/admin/reader/compose?replyTo={{ itemUid | urlencode }}"
|
|
class="ap-card__action ap-card__action--reply"
|
|
title="{{ __('activitypub.reader.actions.reply') }}">
|
|
↩ {{ __("activitypub.reader.actions.reply") }}
|
|
</a>
|
|
<button class="ap-card__action ap-card__action--boost"
|
|
:class="{ 'ap-card__action--active': boosted }"
|
|
:title="boosted ? '{{ __('activitypub.reader.actions.unboost') }}' : '{{ __('activitypub.reader.actions.boost') }}'"
|
|
:disabled="loading"
|
|
@click="interact(boosted ? 'unboost' : 'boost')">
|
|
🔁 <span x-text="boosted ? '{{ __('activitypub.reader.actions.boosted') }}' : '{{ __('activitypub.reader.actions.boost') }}'"></span>
|
|
</button>
|
|
<button class="ap-card__action ap-card__action--like"
|
|
:class="{ 'ap-card__action--active': liked }"
|
|
:title="liked ? '{{ __('activitypub.reader.actions.unlike') }}' : '{{ __('activitypub.reader.actions.like') }}'"
|
|
:disabled="loading"
|
|
@click="interact(liked ? 'unlike' : 'like')">
|
|
<span x-text="liked ? '❤️' : '♥'"></span>
|
|
<span x-text="liked ? '{{ __('activitypub.reader.actions.liked') }}' : '{{ __('activitypub.reader.actions.like') }}'"></span>
|
|
</button>
|
|
<a href="{{ itemUrl }}" class="ap-card__action ap-card__action--link" target="_blank" rel="noopener">
|
|
🔗 {{ __("activitypub.reader.actions.viewOriginal") }}
|
|
</a>
|
|
{% if application.readlaterEndpoint %}
|
|
<button class="ap-card__action ap-card__action--save"
|
|
:class="{ 'ap-card__action--active': saved }"
|
|
:disabled="saved"
|
|
@click="saveLater()"
|
|
:title="saved ? 'Saved' : 'Save for later'">
|
|
<span x-text="saved ? '🔖' : '📑'"></span>
|
|
<span x-text="saved ? 'Saved' : 'Save'"></span>
|
|
</button>
|
|
{% endif %}
|
|
<div x-show="error" x-text="error" class="ap-card__action-error" x-transition></div>
|
|
</footer>
|
|
{# Close moderation content warning wrapper #}
|
|
{% if item._moderated %}
|
|
</div>{# /x-show="shown" #}
|
|
</div>{# /ap-card__moderation-cw #}
|
|
{% endif %}
|
|
</article>
|
|
|
|
{% endif %}{# end hasCardContent/hasCardTitle/hasCardMedia guard #}
|