Skip to content

Commit

Permalink
fix: some issues with publish new note
Browse files Browse the repository at this point in the history
  • Loading branch information
reyamir committed Nov 1, 2024
1 parent 6ad8ffd commit f3e875e
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 75 deletions.
16 changes: 8 additions & 8 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 40 additions & 21 deletions src-tauri/src/commands/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,29 @@ pub async fn get_event(id: String, state: State<'_, Nostr>) -> Result<RichEvent,

Ok(RichEvent { raw, parsed })
} else {
let filter = Filter::new().id(event_id);

let mut rich_event = RichEvent {
raw: "".to_string(),
parsed: None,
};

let filter = Filter::new().id(event_id).limit(1);
let mut events = Events::new(&[filter.clone()]);
let mut rx = client
.stream_events(vec![filter], Some(Duration::from_secs(5)))
.await
.map_err(|e| e.to_string())?;

while let Some(event) = rx.next().await {
events.insert(event);
}

if let Some(event) = events.first() {
let raw = event.as_json();
let parsed = if event.kind == Kind::TextNote {
Some(parse_event(&event.content).await)
} else {
None
};
rich_event = RichEvent { raw, parsed }
}

Ok(rich_event)
Ok(RichEvent { raw, parsed })
} else {
Err("Event not found.".into())
}
}
}

Expand Down Expand Up @@ -332,12 +332,7 @@ pub async fn publish(

#[tauri::command]
#[specta::specta]
pub async fn reply(
content: String,
to: String,
root: Option<String>,
state: State<'_, Nostr>,
) -> Result<String, String> {
pub async fn reply(content: String, to: String, state: State<'_, Nostr>) -> Result<String, String> {
let client = &state.client;

// Create event tags from content
Expand All @@ -349,6 +344,7 @@ pub async fn reply(

// Get reply event
let reply_id = EventId::parse(&to).map_err(|err| err.to_string())?;

let reply_to = match client.database().event_by_id(&reply_id).await {
Ok(event) => {
if let Some(event) = event {
Expand All @@ -360,12 +356,35 @@ pub async fn reply(
Err(e) => return Err(e.to_string()),
};

// Detect root event from reply
let root_ids: Vec<&EventId> = reply_to
.tags
.filter_standardized(TagKind::e())
.filter_map(|t| match t {
TagStandard::Event {
event_id, marker, ..
} => {
if let Some(mkr) = marker {
match mkr {
Marker::Root => Some(event_id),
Marker::Reply => Some(event_id),
_ => None,
}
} else {
Some(event_id)
}
}
_ => None,
})
.collect();

// Get root event if exist
let root = match root {
Some(id) => {
let root_id = EventId::parse(&id).map_err(|err| err.to_string())?;
(client.database().event_by_id(&root_id).await).unwrap_or_default()
}
let root = match root_ids.first() {
Some(&id) => client
.database()
.event_by_id(id)
.await
.map_err(|err| err.to_string())?,
None => None,
};

Expand Down
12 changes: 4 additions & 8 deletions src-tauri/src/commands/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use border::WebviewWindowExt as BorderWebviewWindowExt;
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::{path::PathBuf, str::FromStr};
use std::path::PathBuf;
#[cfg(target_os = "macos")]
use tauri::TitleBarStyle;
use tauri::{
Expand Down Expand Up @@ -68,7 +68,7 @@ pub async fn create_column(
.on_page_load(|webview, payload| match payload.event() {
PageLoadEvent::Started => {
if let Ok(id) = get_last_segment(payload.url()) {
if let Ok(public_key) = PublicKey::from_str(&id) {
if let Ok(public_key) = PublicKey::parse(&id) {
let is_newsfeed = payload.url().to_string().contains("newsfeed");

tauri::async_runtime::spawn(async move {
Expand Down Expand Up @@ -104,7 +104,7 @@ pub async fn create_column(
}
}
});
} else if let Ok(event_id) = EventId::from_str(&id) {
} else if let Ok(event_id) = EventId::parse(&id) {
tauri::async_runtime::spawn(async move {
let state = webview.state::<Nostr>();
let client = &state.client;
Expand All @@ -117,11 +117,7 @@ pub async fn create_column(
.since(Timestamp::now());

if let Err(e) = client
.subscribe_with_id(
subscription_id,
vec![filter.clone().since(Timestamp::now())],
None,
)
.subscribe_with_id(subscription_id, vec![filter], None)
.await
{
println!("Subscription error: {}", e);
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ fn main() {
if let Err(e) = handle_clone.emit("metadata", event.as_json()) {
println!("Emit error: {}", e)
}
} else if event.kind != Kind::RelayList {
} else if event.kind == Kind::TextNote {
let payload = RichEvent {
raw: event.as_json(),
parsed: if event.kind == Kind::TextNote {
Expand Down
4 changes: 2 additions & 2 deletions src/commands.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,9 @@ async publish(content: string, warning: string | null, difficulty: number | null
else return { status: "error", error: e as any };
}
},
async reply(content: string, to: string, root: string | null) : Promise<Result<string, string>> {
async reply(content: string, to: string) : Promise<Result<string, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("reply", { content, to, root }) };
return { status: "ok", data: await TAURI_INVOKE("reply", { content, to }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
Expand Down
9 changes: 8 additions & 1 deletion src/components/note/buttons/open.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ export function NoteOpenThread() {
<Tooltip.Trigger asChild>
<button
type="button"
onClick={() => LumeWindow.openEvent(event)}
onClick={() =>
LumeWindow.openColumn({
name: "Thread",
label: event.id.slice(0, 6),
account: event.pubkey,
url: `/columns/events/${event.id}`,
})
}
className="group inline-flex h-7 w-14 bg-neutral-100 dark:bg-white/10 rounded-full items-center justify-center text-sm font-medium text-neutral-800 dark:text-neutral-200 hover:text-blue-500 hover:bg-neutral-200 dark:hover:bg-white/20"
>
<ListPlus className="shrink-0 size-4" />
Expand Down
9 changes: 8 additions & 1 deletion src/components/note/mentions/note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ export const MentionNote = memo(function MentionNote({
<div className="invisible group-hover:visible flex items-center justify-end">
<button
type="button"
onClick={() => LumeWindow.openEvent(event)}
onClick={() =>
LumeWindow.openColumn({
name: "Thread",
label: eventId.slice(0, 6),
account: event.pubkey,
url: `/columns/events/${eventId}`,
})
}
className="mr-3 text-sm font-medium text-blue-500 hover:text-blue-600"
>
Show all
Expand Down
2 changes: 0 additions & 2 deletions src/components/repost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export const RepostNote = memo(function RepostNote({
}) {
const { isLoading, isError, data } = useEvent(event.repostId, event.content);

console.log("Repost: ", event);

return (
<Note.Root className={cn("", className)}>
{isLoading ? (
Expand Down
26 changes: 13 additions & 13 deletions src/routes/columns/_layout/events.$id.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Screen() {
>
<ScrollArea.Viewport
ref={ref}
className="relative h-full bg-white dark:bg-neutral-800 rounded-t-xl shadow shadow-neutral-300/50 dark:shadow-none border-[.5px] border-neutral-300 dark:border-neutral-700"
className="pb-3 relative h-full bg-white dark:bg-neutral-800 rounded-t-xl shadow shadow-neutral-300/50 dark:shadow-none border-[.5px] border-neutral-300 dark:border-neutral-700"
>
<Virtualizer scrollRef={ref as unknown as RefObject<HTMLElement>}>
<RootEvent />
Expand All @@ -57,16 +57,16 @@ function RootEvent() {

if (isLoading) {
return (
<div className="flex items-center gap-2 text-sm">
<Spinner />
Loading...
<div className="h-20 flex items-center justify-center gap-2 border-b-[.5px] border-neutral-300 dark:border-neutral-700">
<Spinner className="size-4" />
<p className="text-sm">Loading...</p>
</div>
);
}

if (isError || !event) {
return (
<div className="flex items-center gap-2 text-sm text-red-500">
<div className="flex items-center gap-2 text-sm text-red-500 border-b-[.5px] border-neutral-300 dark:border-neutral-700">
{error?.message || "Event not found within your current relay set"}
</div>
);
Expand All @@ -80,7 +80,7 @@ function RootEvent() {
<Note.Menu />
</div>
<Note.ContentLarge className="px-3" />
<div className="flex items-center gap-2 px-3 mt-6 h-12 rounded-b-xl bg-neutral-50 dark:bg-white/5">
<div className="select-text flex items-center gap-2 px-3 mt-6 h-12 bg-neutral-50 dark:bg-white/5">
<Note.Reply label />
<Note.Repost label />
<Note.Zap label />
Expand All @@ -94,7 +94,7 @@ function ReplyList() {
const { id } = Route.useParams();
const { queryClient } = Route.useRouteContext();
const { data, isLoading } = useQuery({
queryKey: ["reply", id],
queryKey: ["replies", id],
queryFn: async () => {
const res = await commands.getReplies(id);

Expand Down Expand Up @@ -183,8 +183,10 @@ function ReplyList() {
"event",
async (data) => {
const event = LumeEvent.from(data.payload.raw, data.payload.parsed);
console.log(event);

await queryClient.setQueryData(
["reply", id],
["replies", id],
(prevEvents: LumeEvent[]) => {
if (!prevEvents) return [event];
return [event, ...prevEvents];
Expand All @@ -204,11 +206,9 @@ function ReplyList() {
All replies
</div>
{isLoading ? (
<div className="flex items-center justify-center w-full mb-3 h-12 bg-black/5 dark:bg-white/5 rounded-xl">
<div className="flex items-center justify-center gap-2">
<Spinner className="size-4" />
<span className="text-sm font-medium">Getting replies...</span>
</div>
<div className="flex items-center justify-center gap-2">
<Spinner className="size-4" />
<span className="text-sm font-medium">Getting replies...</span>
</div>
) : (
<div className="flex flex-col gap-3">
Expand Down
Loading

0 comments on commit f3e875e

Please sign in to comment.