feat(feed/chat): VK-style share post to chats + list breathing room
Feed list padding
FlatList had no inner padding so the first post bumped against the
tab strip and the last post against the NavBar. Added paddingTop: 8
/ paddingBottom: 24 on contentContainerStyle in both /feed and
/feed/tag/[tag] — first card now has a clear top gap, last card
doesn't get hidden behind the FAB or NavBar.
Share-to-chat flow
Replaces the placeholder share button (which showed an Alert with
the post URL) with a real "forward to chats" flow modeled on VK's
shared-wall-post embed.
New modules
lib/forwardPost.ts — encodePostRef / tryParsePostRef +
forwardPostToContacts(). Serialises a
feed post into a tiny JSON payload that
rides the same encrypted envelope as any
chat message; decode side distinguishes
"post_ref" payloads from regular text by
trying JSON.parse on decrypted text.
Mirrors the sent message into the sender's
local history so they see "you shared
this" in the chat they forwarded to.
components/feed/ShareSheet.tsx
— bottom-sheet picker. Multi-select
contacts via tick-box, search by
username / alias / address prefix.
"Send (N)" dispatches N parallel
encrypted envelopes. Contacts with no
X25519 key are filtered out (can't
encrypt for them).
components/chat/PostRefCard.tsx
— compact embedded-post card for chat
bubbles. Ribbon "ПОСТ" label +
author + 3-line excerpt + "с фото"
indicator. Tap → /(app)/feed/{id} full
post detail. Palette switches between
blue-bubble-friendly and peer-bubble-
friendly depending on bubble side.
Message pipeline
lib/types.ts — Message.postRef optional field added.
text stays "" when the message is a
post-ref (nothing to render as plain text).
hooks/useMessages.ts + hooks/useGlobalInbox.ts
— post decryption of every inbound envelope
runs through tryParsePostRef; matching
messages get the postRef attached instead
of the raw JSON in .text.
components/chat/MessageBubble.tsx
— renders PostRefCard inside the bubble when
msg.postRef is set. Other bubble features
(reply quote, attachment preview, text)
still work around it.
PostCard
- share icon now opens <ShareSheet>; the full-URL placeholder is
gone. ShareSheet is embedded at the PostCard level so each card
owns its own sheet state (avoids modal-stacking issues).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
145
client-app/lib/forwardPost.ts
Normal file
145
client-app/lib/forwardPost.ts
Normal file
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* Forward a feed post into a direct chat as a "post reference" message.
|
||||
*
|
||||
* What the receiver sees
|
||||
* ----------------------
|
||||
* A special chat bubble rendering a compact card:
|
||||
* [avatar] @author "post excerpt…" [📷 if has image]
|
||||
*
|
||||
* Tapping the card opens the full post detail. Design is modelled on
|
||||
* VK/Twitter's "shared post" embed: visually distinct from a plain
|
||||
* message so the user sees at a glance that this came from the feed,
|
||||
* not from the sender directly.
|
||||
*
|
||||
* Transport
|
||||
* ---------
|
||||
* Same encrypted envelope as a normal chat message. The payload is
|
||||
* plaintext JSON with a discriminator:
|
||||
*
|
||||
* { "kind": "post_ref", "post_id": "...", "author": "...",
|
||||
* "excerpt": "first 120 chars of body", "has_image": true }
|
||||
*
|
||||
* The receiver's useMessages / useGlobalInbox hooks detect the JSON
|
||||
* shape after decryption and assign it to Message.postRef for rendering.
|
||||
* Plain-text messages stay wrapped in the same envelope format — the
|
||||
* only difference is whether the decrypted body parses as our JSON
|
||||
* schema.
|
||||
*/
|
||||
|
||||
import { encryptMessage } from './crypto';
|
||||
import { sendEnvelope } from './api';
|
||||
import { appendMessage } from './storage';
|
||||
import { useStore } from './store';
|
||||
import { randomId } from './utils';
|
||||
import type { Contact, Message } from './types';
|
||||
import type { FeedPostItem } from './feed';
|
||||
|
||||
const POST_REF_MARKER = 'dchain-post-ref';
|
||||
const EXCERPT_MAX = 120;
|
||||
|
||||
export interface PostRefPayload {
|
||||
kind: typeof POST_REF_MARKER;
|
||||
post_id: string;
|
||||
author: string;
|
||||
excerpt: string;
|
||||
has_image: boolean;
|
||||
}
|
||||
|
||||
/** Serialise a post ref for the wire. */
|
||||
export function encodePostRef(post: FeedPostItem): string {
|
||||
const payload: PostRefPayload = {
|
||||
kind: POST_REF_MARKER,
|
||||
post_id: post.post_id,
|
||||
author: post.author,
|
||||
excerpt: truncate(post.content, EXCERPT_MAX),
|
||||
has_image: !!post.has_attachment,
|
||||
};
|
||||
return JSON.stringify(payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to parse an incoming plaintext message as a post reference.
|
||||
* Returns null if the payload isn't the expected shape — the caller
|
||||
* then treats it as a normal text message.
|
||||
*/
|
||||
export function tryParsePostRef(plaintext: string): PostRefPayload | null {
|
||||
const trimmed = plaintext.trim();
|
||||
if (!trimmed.startsWith('{')) return null;
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed) as Partial<PostRefPayload>;
|
||||
if (parsed.kind !== POST_REF_MARKER) return null;
|
||||
if (!parsed.post_id || !parsed.author) return null;
|
||||
return {
|
||||
kind: POST_REF_MARKER,
|
||||
post_id: String(parsed.post_id),
|
||||
author: String(parsed.author),
|
||||
excerpt: String(parsed.excerpt ?? ''),
|
||||
has_image: !!parsed.has_image,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward `post` to each of the given contacts as a post-ref message.
|
||||
* Creates a fresh envelope per recipient (can't fan-out a single
|
||||
* ciphertext — each recipient's x25519 key seals differently) and
|
||||
* drops a mirrored Message into our local chat history so the user
|
||||
* sees the share in their own view too.
|
||||
*
|
||||
* Contacts without an x25519 public key are skipped with a warning
|
||||
* instead of failing the whole batch.
|
||||
*/
|
||||
export async function forwardPostToContacts(params: {
|
||||
post: FeedPostItem;
|
||||
contacts: Contact[];
|
||||
keyFile: { pub_key: string; priv_key: string; x25519_pub: string; x25519_priv: string };
|
||||
}): Promise<{ ok: number; failed: number }> {
|
||||
const { post, contacts, keyFile } = params;
|
||||
const appendMsg = useStore.getState().appendMessage;
|
||||
const body = encodePostRef(post);
|
||||
|
||||
let ok = 0, failed = 0;
|
||||
for (const c of contacts) {
|
||||
if (!c.x25519Pub) { failed++; continue; }
|
||||
try {
|
||||
const { nonce, ciphertext } = encryptMessage(
|
||||
body, keyFile.x25519_priv, c.x25519Pub,
|
||||
);
|
||||
await sendEnvelope({
|
||||
senderPub: keyFile.x25519_pub,
|
||||
recipientPub: c.x25519Pub,
|
||||
senderEd25519Pub: keyFile.pub_key,
|
||||
nonce, ciphertext,
|
||||
});
|
||||
|
||||
// Mirror into local history so the sender sees "you shared this".
|
||||
const mirrored: Message = {
|
||||
id: randomId(),
|
||||
from: keyFile.x25519_pub,
|
||||
text: '', // postRef carries all the content
|
||||
timestamp: Math.floor(Date.now() / 1000),
|
||||
mine: true,
|
||||
postRef: {
|
||||
postID: post.post_id,
|
||||
author: post.author,
|
||||
excerpt: truncate(post.content, EXCERPT_MAX),
|
||||
hasImage: !!post.has_attachment,
|
||||
},
|
||||
};
|
||||
appendMsg(c.address, mirrored);
|
||||
await appendMessage(c.address, mirrored);
|
||||
ok++;
|
||||
} catch {
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
return { ok, failed };
|
||||
}
|
||||
|
||||
function truncate(s: string, n: number): string {
|
||||
if (!s) return '';
|
||||
if (s.length <= n) return s;
|
||||
return s.slice(0, n).trimEnd() + '…';
|
||||
}
|
||||
Reference in New Issue
Block a user