Skip to content

Commit 1aa87bb

Browse files
tellahonpub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w
andauthored
feat(composer): compose spoiler, annotation, and tooltip controls (#1491)
Signed-off-by: Taylor Ho <[email protected]> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@sprout-oss.stage.blox.sqprod.co>
1 parent 36ac524 commit 1aa87bb

17 files changed

Lines changed: 1373 additions & 256 deletions

‎desktop/playwright.config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ export default defineConfig({
4040
"**/agent-readiness-screenshots.spec.ts",
4141
"**/file-attachment.spec.ts",
4242
"**/image-attachment-gallery.spec.ts",
43+
"**/composer-image-draw.spec.ts",
4344
"**/video-attachment.spec.ts",
4445
"**/spoiler.spec.ts",
46+
"**/composer-tooltip-dismiss.spec.ts",
4547
"**/mentions.spec.ts",
4648
"**/relay-reconnect.spec.ts",
4749
"**/relay-reconnect-affordance.spec.ts",

‎desktop/src-tauri/src/commands/media_download.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ pub async fn download_file(
129129
save_bytes_with_dialog(&app, &filename, "All Files", &extensions, &bytes).await
130130
}
131131

132+
/// Fetch relay media bytes for the composer image editor.
133+
///
134+
/// The editor composites the image onto a canvas and needs pixel access.
135+
/// Handing the webview raw bytes over IPC (which it wraps in a same-origin
136+
/// `blob:` URL) keeps the canvas un-tainted without involving CORS — and
137+
/// therefore without any media-proxy header or origin-gate changes.
138+
///
139+
/// Same SSRF validation, size cap, and content policy as the download
140+
/// commands above.
141+
#[tauri::command]
142+
pub async fn fetch_media_bytes(url: String, state: State<'_, AppState>) -> Result<Vec<u8>, String> {
143+
let relay_base = relay_api_base_url_with_override(&state);
144+
validate_download_url(&url, &relay_base)?;
145+
146+
let bytes = fetch_blob_bytes(&url, &state).await?;
147+
detect_and_validate_mime(&bytes)?;
148+
Ok(bytes)
149+
}
150+
132151
/// Fetch blob bytes from a (pre-validated) relay media URL through the app's
133152
/// HTTP client, enforcing the download size cap. The caller is responsible for
134153
/// validating the URL origin and for any content-type checks on the result.

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ pub fn run() {
507507
upload_media_bytes,
508508
download_image,
509509
download_file,
510+
fetch_media_bytes,
510511
list_relay_members,
511512
get_my_relay_membership,
512513
add_relay_member,

‎desktop/src/features/forum/ui/ForumComposerMediaStatus.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
import * as React from "react";
2+
13
import type { useMediaUpload } from "@/features/messages/lib/useMediaUpload";
24
import { ComposerAttachments } from "@/features/messages/ui/ComposerAttachments";
35

46
type ComposerMedia = Pick<
57
ReturnType<typeof useMediaUpload>,
68
| "isUploading"
79
| "cancelUpload"
10+
| "originalUrlByUrl"
811
| "pendingImeta"
912
| "removeAttachment"
13+
| "revertAttachment"
1014
| "setUploadState"
15+
| "uploadEditedAttachment"
1116
| "uploadState"
1217
| "uploadingCount"
1318
| "uploadingPreviews"
@@ -20,6 +25,13 @@ type ForumComposerMediaStatusProps = {
2025
export function ForumComposerMediaStatus({
2126
media,
2227
}: ForumComposerMediaStatusProps) {
28+
const handleEditSave = React.useCallback(
29+
async (url: string, bytes: Uint8Array) => {
30+
await media.uploadEditedAttachment(url, bytes);
31+
},
32+
[media.uploadEditedAttachment],
33+
);
34+
2335
return (
2436
<>
2537
{media.uploadState.status === "error" ? (
@@ -41,7 +53,10 @@ export function ForumComposerMediaStatus({
4153
attachments={media.pendingImeta}
4254
isUploading={media.isUploading}
4355
onCancelUpload={media.cancelUpload}
56+
onEditSave={handleEditSave}
4457
onRemove={media.removeAttachment}
58+
onRevert={media.revertAttachment}
59+
originalUrlByUrl={media.originalUrlByUrl}
4560
uploadingCount={media.uploadingCount}
4661
uploadingPreviews={media.uploadingPreviews}
4762
/>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as React from "react";
2+
3+
import type { MediaUploadController } from "./useMediaUpload";
4+
5+
type UseAttachmentEditingArgs = {
6+
revertAttachment: MediaUploadController["revertAttachment"];
7+
/** Spoiler-set updater; membership follows the attachment across URL swaps. */
8+
setSpoileredAttachmentUrls: React.Dispatch<React.SetStateAction<Set<string>>>;
9+
uploadEditedAttachment: MediaUploadController["uploadEditedAttachment"];
10+
};
11+
12+
/**
13+
* Composer-side glue for the attachment drawing editor: uploads annotated
14+
* bytes as a replacement / reverts to the pre-edit original, migrating
15+
* spoiler membership from the replaced URL to its replacement so an edited
16+
* spoilered image stays spoilered.
17+
*/
18+
export function useAttachmentEditing({
19+
revertAttachment,
20+
setSpoileredAttachmentUrls,
21+
uploadEditedAttachment,
22+
}: UseAttachmentEditingArgs) {
23+
const migrateSpoileredUrl = React.useCallback(
24+
(fromUrl: string, toUrl: string) => {
25+
setSpoileredAttachmentUrls((current) => {
26+
if (!current.has(fromUrl)) return current;
27+
const next = new Set(current);
28+
next.delete(fromUrl);
29+
next.add(toUrl);
30+
return next;
31+
});
32+
},
33+
[setSpoileredAttachmentUrls],
34+
);
35+
36+
const handleAttachmentEditSave = React.useCallback(
37+
async (url: string, bytes: Uint8Array) => {
38+
const descriptor = await uploadEditedAttachment(url, bytes);
39+
if (descriptor) migrateSpoileredUrl(url, descriptor.url);
40+
},
41+
[migrateSpoileredUrl, uploadEditedAttachment],
42+
);
43+
44+
const handleAttachmentRevert = React.useCallback(
45+
(url: string) => {
46+
const original = revertAttachment(url);
47+
if (original) migrateSpoileredUrl(url, original.url);
48+
},
49+
[migrateSpoileredUrl, revertAttachment],
50+
);
51+
52+
return { handleAttachmentEditSave, handleAttachmentRevert };
53+
}

‎desktop/src/features/messages/lib/useMediaUpload.ts‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,44 @@ export function useMediaUpload() {
205205
const pendingImetaRef = React.useRef(pendingImeta);
206206
pendingImetaRef.current = pendingImeta;
207207

208+
/**
209+
* Pre-edit originals of annotated attachments, keyed by the annotated
210+
* attachment's URL. Powers "revert to original" in the composer lightbox.
211+
* In-memory only — cleared implicitly when the attachment leaves the
212+
* composer (send, remove, draft switch).
213+
*/
214+
const [originalsByUrl, setOriginalsByUrl] = React.useState<
215+
Map<string, BlobDescriptor>
216+
>(() => new Map());
217+
const originalsByUrlRef = React.useRef(originalsByUrl);
218+
originalsByUrlRef.current = originalsByUrl;
219+
220+
/** Annotated URL → original URL (derived; handy for stable list keys). */
221+
const originalUrlByUrl = React.useMemo(() => {
222+
const map = new Map<string, string>();
223+
for (const [url, original] of originalsByUrl) map.set(url, original.url);
224+
return map;
225+
}, [originalsByUrl]);
226+
227+
// Prune originals whose annotated attachment is no longer pending —
228+
// covers remove, cancel, send-clear, and draft restore in one place.
229+
React.useEffect(() => {
230+
setOriginalsByUrl((prev) => {
231+
if (prev.size === 0) return prev;
232+
const liveUrls = new Set(pendingImeta.map((d) => d.url));
233+
let changed = false;
234+
const next = new Map<string, BlobDescriptor>();
235+
for (const [url, original] of prev) {
236+
if (liveUrls.has(url)) {
237+
next.set(url, original);
238+
} else {
239+
changed = true;
240+
}
241+
}
242+
return changed ? next : prev;
243+
});
244+
}, [pendingImeta]);
245+
208246
/** Monotonic slot counter — ensures each batch gets unique indices even
209247
* before React flushes the state update. */
210248
const nextSlotRef = React.useRef(0);
@@ -511,6 +549,80 @@ export function useMediaUpload() {
511549
[isUploadCanceled, onUploaded, onUploadError, reserveUploadingPreview],
512550
);
513551

552+
/**
553+
* Upload an annotated replacement for an existing image attachment and
554+
* swap it into the same slot (attachment order is preserved). The pre-edit
555+
* descriptor is remembered in `originalsByUrl` so the edit can be reverted;
556+
* chained edits keep the earliest original as the single revert point.
557+
*
558+
* Returns the new descriptor, or null if `oldUrl` is no longer pending.
559+
* Rejects on upload failure (after surfacing the standard error banner) so
560+
* callers can keep their editing UI open.
561+
*/
562+
const uploadEditedAttachment = React.useCallback(
563+
async (
564+
oldUrl: string,
565+
bytes: Uint8Array,
566+
): Promise<BlobDescriptor | null> => {
567+
const oldDescriptor = pendingImetaRef.current.find(
568+
(d) => d.url === oldUrl,
569+
);
570+
if (!oldDescriptor) return null;
571+
572+
// The annotated output is always PNG — swap the extension accordingly.
573+
const stem = (oldDescriptor.filename ?? "image").replace(/\.[^.]+$/, "");
574+
const filename = `${stem}.png`;
575+
576+
const previewId = reserveUploadingPreview();
577+
setUploadingCount((c) => c + 1);
578+
try {
579+
const descriptor = await uploadMediaBytes(
580+
[...bytes],
581+
filename,
582+
uploadProgressId(previewId),
583+
);
584+
if (isUploadCanceled(previewId)) return null;
585+
finishUpload(previewId);
586+
setImetaSlots((prev) =>
587+
prev.map((d) => (d?.url === oldUrl ? descriptor : d)),
588+
);
589+
setOriginalsByUrl((prev) => {
590+
const next = new Map(prev);
591+
// Re-editing an annotated image keeps the earliest original.
592+
const original = prev.get(oldUrl) ?? oldDescriptor;
593+
next.delete(oldUrl);
594+
next.set(descriptor.url, original);
595+
return next;
596+
});
597+
return descriptor;
598+
} catch (err) {
599+
onUploadError(err, previewId);
600+
throw err;
601+
}
602+
},
603+
[finishUpload, isUploadCanceled, onUploadError, reserveUploadingPreview],
604+
);
605+
606+
/**
607+
* Swap an annotated attachment back to its pre-edit original (same slot)
608+
* and forget the stored original. Returns the restored descriptor, or null
609+
* if the URL has no recorded original.
610+
*/
611+
const revertAttachment = React.useCallback(
612+
(url: string): BlobDescriptor | null => {
613+
const original = originalsByUrlRef.current.get(url);
614+
if (!original) return null;
615+
setImetaSlots((prev) => prev.map((d) => (d?.url === url ? original : d)));
616+
setOriginalsByUrl((prev) => {
617+
const next = new Map(prev);
618+
next.delete(url);
619+
return next;
620+
});
621+
return original;
622+
},
623+
[],
624+
);
625+
514626
const removeAttachment = React.useCallback((url: string) => {
515627
setImetaSlots((prev) => prev.map((d) => (d?.url === url ? null : d)));
516628
}, []);
@@ -541,11 +653,14 @@ export function useMediaUpload() {
541653
handlePaste,
542654
isDragOver,
543655
isUploading,
656+
originalUrlByUrl,
544657
pendingImeta,
545658
pendingImetaRef,
546659
removeAttachment,
660+
revertAttachment,
547661
setPendingImeta,
548662
setUploadState,
663+
uploadEditedAttachment,
549664
uploadFile,
550665
uploadingCount,
551666
uploadingPreviews,
@@ -561,9 +676,12 @@ export function useMediaUpload() {
561676
handlePaste,
562677
isDragOver,
563678
isUploading,
679+
originalUrlByUrl,
564680
pendingImeta,
565681
removeAttachment,
682+
revertAttachment,
566683
setPendingImeta,
684+
uploadEditedAttachment,
567685
uploadFile,
568686
uploadingCount,
569687
uploadingPreviews,

0 commit comments

Comments
 (0)