Skip to content

Commit

Permalink
small cleanup for text note posting
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Dec 10, 2024
1 parent 58cafd6 commit 73aa6ad
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/components/post-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import dayjs from "dayjs";
import { useForm } from "react-hook-form";
import { kinds, UnsignedEvent } from "nostr-tools";
import { useThrottle } from "react-use";
import { useObservable } from "applesauce-react/hooks";
import { useEventFactory, useObservable } from "applesauce-react/hooks";

import { ChevronDownIcon, ChevronUpIcon, UploadImageIcon } from "../icons";
import PublishAction from "../../classes/nostr-publish-action";
Expand Down Expand Up @@ -64,6 +64,7 @@ import localSettings from "../../services/local-settings";
import useLocalStorageDisclosure from "../../hooks/use-localstorage-disclosure";
import InsertGifButton from "../gif/insert-gif-button";
import InsertImageButton from "./insert-image-button";
import { unixNow } from "applesauce-core/helpers";

type FormValues = {
subject: string;
Expand Down Expand Up @@ -101,6 +102,7 @@ export default function PostModal({
const emojis = useContextEmojis();
const moreOptions = useDisclosure();

const factory = useEventFactory();
const [draft, setDraft] = useState<UnsignedEvent>();
const { getValues, setValue, watch, register, handleSubmit, formState, reset } = useForm<FormValues>({
defaultValues: {
Expand Down Expand Up @@ -131,10 +133,10 @@ export default function PostModal({
const { content, nsfw, nsfwReason, community, split, subject } = values;

let draft = finalizeNote({
content: correctContentMentions(content),
kind: kinds.ShortTextNote,
content,
tags: [],
created_at: dayjs().unix(),
created_at: unixNow(),
});

if (nsfw) draft.tags.push(nsfwReason ? ["content-warning", nsfwReason] : ["content-warning"]);
Expand All @@ -152,7 +154,7 @@ export default function PostModal({
setDraft(unsigned);
return unsigned;
},
[getValues, emojis, finalizeDraft, setDraft],
[getValues, emojis, finalizeDraft, setDraft, factory],
);

// throttle update the draft every 500ms
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/nostr/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function ensureNotifyPubkeys(draft: EventTemplate, pubkeys: string[]) {
}

export function correctContentMentions(content: string) {
return content.replace(/(\s|^)(?:@)?(npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})/gi, "$1nostr:$2");
return content.replaceAll(/(\s|^)(?:@)?(npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})/gi, "$1nostr:$2");
}

export function getPubkeysMentionedInContent(content: string, direct = false) {
Expand Down Expand Up @@ -219,7 +219,6 @@ export function addPubkeyRelayHints(draft: EventTemplate) {
export function finalizeNote(draft: EventTemplate) {
let updated: EventTemplate = { ...draft, tags: Array.from(draft.tags) };
updated.content = correctContentMentions(updated.content);
updated = createHashtagTags(updated);
updated = addPubkeyRelayHints(updated);
updated = ensureTagContentMentions(updated);
return updated;
Expand Down
5 changes: 3 additions & 2 deletions src/services/event-factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BehaviorSubject, Observable } from "rxjs";
import { EventFactory } from "applesauce-factory";
import { Account } from "../classes/accounts/account";
import { getEventRelayHints } from "./event-relay-hint";
import { getEventRelayHint, getPubkeyRelayHint } from "./event-relay-hint";
import { NIP_89_CLIENT_APP } from "../const";
import accountService from "./account";

Expand All @@ -19,7 +19,8 @@ class EventFactoryService {
this.subject.next(
new EventFactory({
signer: current.signer,
getRelayHint: (event) => getEventRelayHints(event, 1)[0],
getRelayHint: getEventRelayHint,
getPubkeyRelayHint: getPubkeyRelayHint,
client: NIP_89_CLIENT_APP,
}),
);
Expand Down
5 changes: 5 additions & 0 deletions src/services/event-relay-hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export function getEventRelayHint(event: NostrEvent): string | undefined {
return getEventRelayHints(event, 1)[0];
}

/** Returns a relay hint for a single pubkey */
export function getPubkeyRelayHint(pubkey: string): string | undefined {
return getAuthorHints(pubkey)[0];
}

/** Returns a nevent or naddr for an event */
export function getSharableEventAddress(event: NostrEvent, relays?: Iterable<string>) {
relays = relays || getEventRelayHints(event, 2);
Expand Down

0 comments on commit 73aa6ad

Please sign in to comment.