Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Nov 19, 2024
1 parent 7777b33 commit dbd724a
Show file tree
Hide file tree
Showing 27 changed files with 80 additions and 122 deletions.
8 changes: 4 additions & 4 deletions src/components/app-handler-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import {
Text,
} from "@chakra-ui/react";
import { NostrEvent, kinds, nip19 } from "nostr-tools";
import { encodeDecodeResult } from "applesauce-core/helpers";
import { encodeDecodeResult, getProfileContent, ProfileContent } from "applesauce-core/helpers";

import { ExternalLinkIcon } from "../icons";
import useTimelineLoader from "../../hooks/use-timeline-loader";
import useSingleEvent from "../../hooks/use-single-event";
import useReplaceableEvent from "../../hooks/use-replaceable-event";
import { useReadRelays } from "../../hooks/use-client-relays";
import { Kind0ParsedContent, getDisplayName, parseMetadataContent } from "../../helpers/nostr/user-metadata";
import { getDisplayName } from "../../helpers/nostr/profile";
import { MetadataAvatar } from "../user/user-avatar";
import HoverLinkOverlay from "../hover-link-overlay";
import ArrowRight from "../icons/arrow-right";
Expand Down Expand Up @@ -62,7 +62,7 @@ function getKindFromDecoded(decoded: nip19.DecodeResult) {
}

function AppHandler({ app, decoded }: { app: NostrEvent; decoded: nip19.DecodeResult }) {
const metadata = useMemo(() => parseMetadataContent(app), [app]);
const metadata = useMemo(() => getProfileContent(app), [app]);
const link = useMemo(() => {
const tag = app.tags.find((t) => t[0] === "web" && t[2] === decoded.type) || app.tags.find((t) => t[0] === "web");
return tag ? tag[1].replace("<bech32>", encodeDecodeResult(decoded)) : undefined;
Expand Down Expand Up @@ -111,7 +111,7 @@ export default function AppHandlerModal({
const filteredApps = apps.filter((app) => {
if (search.length > 1) {
try {
const parsed = JSON.parse(app.content) as Kind0ParsedContent;
const parsed = JSON.parse(app.content) as ProfileContent;
if (getDisplayName(parsed, app.pubkey).toLowerCase().includes(search.toLowerCase())) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/common-menu-items/share-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MenuItem, useToast } from "@chakra-ui/react";
import { NostrEvent } from "../../types/nostr-event";
import { ShareIcon } from "../icons";
import useUserProfile from "../../hooks/use-user-profile";
import { getDisplayName } from "../../helpers/nostr/user-metadata";
import { getDisplayName } from "../../helpers/nostr/profile";
import useShareableEventAddress from "../../hooks/use-shareable-event-address";

export default function ShareLinkMenuItem({ event }: { event: NostrEvent }) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/compact-user-stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Link as RouterLink } from "react-router-dom";
import { nip19 } from "nostr-tools";

import UserAvatar from "./user/user-avatar";
import { getDisplayName } from "../helpers/nostr/user-metadata";
import { getDisplayName } from "../helpers/nostr/profile";
import useUserProfile from "../hooks/use-user-profile";

function UserTag({ pubkey, ...props }: { pubkey: string } & Omit<TagProps, "children">) {
Expand Down
5 changes: 2 additions & 3 deletions src/components/event-zap-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import {
import dayjs from "dayjs";
import { kinds } from "nostr-tools";
import { getValue } from "applesauce-core/observable";
import { getInboxes, getOutboxes } from "applesauce-core/helpers";
import { getInboxes, getOutboxes, safeRelayUrls } from "applesauce-core/helpers";

import { DraftNostrEvent, NostrEvent, isDTag } from "../../types/nostr-event";
import clientRelaysService from "../../services/client-relays";
import { getZapSplits } from "../../helpers/nostr/zaps";
import { unique } from "../../helpers/array";
import relayScoreboardService from "../../services/relay-scoreboard";
Expand Down Expand Up @@ -81,7 +80,7 @@ async function getPayRequestForPubkey(
content: comment ?? "",
tags: [
["p", pubkey],
["relays", ...unique([...userInbox, ...eventRelays, ...outbox, ...additional])],
["relays", ...unique(safeRelayUrls([...userInbox, ...eventRelays, ...outbox, ...additional]))],
["amount", String(amount)],
],
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/account-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CloseIcon } from "@chakra-ui/icons";
import { useNavigate, Link as RouterLink } from "react-router-dom";
import { Box, Button, ButtonGroup, Flex, IconButton, Text, useDisclosure } from "@chakra-ui/react";

import { getDisplayName } from "../../helpers/nostr/user-metadata";
import { getDisplayName } from "../../helpers/nostr/profile";
import useUserProfile from "../../hooks/use-user-profile";
import accountService from "../../services/account";
import { AddIcon, ChevronDownIcon, ChevronUpIcon } from "../icons";
Expand Down
2 changes: 1 addition & 1 deletion src/components/search-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { nip19 } from "nostr-tools";

import UserAvatar from "../user/user-avatar";
import useUserProfile from "../../hooks/use-user-profile";
import { getDisplayName } from "../../helpers/nostr/user-metadata";
import { getDisplayName } from "../../helpers/nostr/profile";
import { userSearchDirectory } from "../../services/username-search";

function UserOption({ pubkey }: { pubkey: string }) {
Expand Down
5 changes: 3 additions & 2 deletions src/components/user/user-avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { forwardRef, memo, useMemo } from "react";
import { Avatar, AvatarProps } from "@chakra-ui/react";
import { useAsync } from "react-use";
import styled from "@emotion/styled";
import { ProfileContent } from "applesauce-core/helpers";

import { getIdenticon } from "../../helpers/identicon";
import { safeUrl } from "../../helpers/parse";
import { Kind0ParsedContent, getDisplayName } from "../../helpers/nostr/user-metadata";
import { getDisplayName } from "../../helpers/nostr/profile";
import useAppSettings from "../../hooks/use-app-settings";
import useCurrentAccount from "../../hooks/use-current-account";
import { buildImageProxyURL } from "../../helpers/image";
Expand Down Expand Up @@ -79,7 +80,7 @@ const SquareAvatarWithBorder = styled(SquareAvatar)`
`;

export type MetadataAvatarProps = Omit<AvatarProps, "src"> & {
metadata?: Kind0ParsedContent;
metadata?: ProfileContent;
pubkey?: string;
noProxy?: boolean;
square?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/components/user/user-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Link, LinkProps } from "@chakra-ui/react";
import { Link as RouterLink } from "react-router-dom";
import { nip19 } from "nostr-tools";

import { getDisplayName } from "../../helpers/nostr/user-metadata";
import { getDisplayName } from "../../helpers/nostr/profile";
import useUserProfile from "../../hooks/use-user-profile";
import useAppSettings from "../../hooks/use-app-settings";
import useCurrentAccount from "../../hooks/use-current-account";
Expand Down
2 changes: 1 addition & 1 deletion src/components/user/user-name.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { memo } from "react";
import { Text, TextProps } from "@chakra-ui/react";

import { getDisplayName } from "../../helpers/nostr/user-metadata";
import { getDisplayName } from "../../helpers/nostr/profile";
import useUserProfile from "../../hooks/use-user-profile";
import useAppSettings from "../../hooks/use-app-settings";

Expand Down
7 changes: 3 additions & 4 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { safeRelayUrls } from "applesauce-core/helpers";
import { kinds } from "nostr-tools";
import { safeRelayUrl, safeRelayUrls } from "./helpers/relay";

export const DEFAULT_SEARCH_RELAYS = safeRelayUrls([
"wss://relay.nostr.band",
Expand All @@ -8,11 +8,10 @@ export const DEFAULT_SEARCH_RELAYS = safeRelayUrls([
"wss://filter.nostr.wine",
]);
export const WIKI_RELAYS = safeRelayUrls(["wss://relay.wikifreedia.xyz/"]);
export const COMMON_CONTACT_RELAY = safeRelayUrl("wss://purplepag.es") as string;
export const COMMON_CONTACT_RELAYS = [COMMON_CONTACT_RELAY];
export const COMMON_CONTACT_RELAYS = safeRelayUrls(["wss://purplepag.es/"]);

export const DEFAULT_SIGNAL_RELAYS = safeRelayUrls(["wss://nostrue.com/", "wss://relay.damus.io"]);
export const DEFAULT_NOSTR_CONNECT_RELAYS = safeRelayUrls(["wss://relay.nsec.app"]);
export const DEFAULT_NOSTR_CONNECT_RELAYS = safeRelayUrls(["wss://relay.nsec.app/"]);

export const DEFAULT_ICE_SERVERS: RTCIceServer[] = [
{
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/nostr/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { nip19 } from "nostr-tools";
import emojiRegex from "emoji-regex";
import { truncatedId } from "./event";
import { ProfileContent } from "applesauce-core/helpers";

export function getSearchNames(profile: ProfileContent) {
if (!profile) return [];

return [profile.displayName, profile.display_name, profile.name].filter(Boolean) as string[];
}

const matchEmoji = emojiRegex();
export function getDisplayName(metadata: ProfileContent | undefined, pubkey: string, removeEmojis = false) {
let displayName = metadata?.displayName || metadata?.display_name || metadata?.name;

if (displayName) {
if (removeEmojis) displayName = displayName.replaceAll(matchEmoji, "");
return displayName;
}

return truncatedId(nip19.npubEncode(pubkey));
}
63 changes: 0 additions & 63 deletions src/helpers/nostr/user-metadata.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/helpers/nostr/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export const FLARE_VIDEO_KIND = 34235;

export function getVideoTitle(video: NostrEvent) {
const title = video.tags.find((t) => t[0] === "title")?.[1];
if (!title) throw new Error("missing title");
if (!title) throw new Error("Missing title");
return title;
}
export function getVideoUrl(video: NostrEvent) {
const url = video.tags.find((t) => t[0] === "url")?.[1];
if (!url) throw new Error("missing url");
if (!url) throw new Error("Missing url");
return url;
}
export function getVideoSummary(video: NostrEvent) {
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/nostr/zaps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { bech32 } from "@scure/base";
import {} from "nostr-tools/nip57";
import { isETag, isPTag, NostrEvent } from "../../types/nostr-event";
import { utils } from "nostr-tools";
import { getZapPayment, ProfileContent } from "applesauce-core/helpers";
import { NostrEvent, utils } from "nostr-tools";
import { getZapPayment, isETag, isPTag, ProfileContent } from "applesauce-core/helpers";

// based on https://github.com/nbd-wtf/nostr-tools/blob/master/nip57.ts
export async function getZapEndpoint(metadata: ProfileContent): Promise<null | string> {
Expand Down
8 changes: 6 additions & 2 deletions src/providers/global/web-of-trust-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getPubkeysFromList } from "../../helpers/nostr/lists";
import useCurrentAccount from "../../hooks/use-current-account";
import { PubkeyGraph } from "../../classes/pubkey-graph";
import replaceableEventsService from "../../services/replaceable-events";
import { COMMON_CONTACT_RELAY } from "../../const";
import { COMMON_CONTACT_RELAYS } from "../../const";
import { eventStore } from "../../services/event-store";

export function loadSocialGraph(
Expand Down Expand Up @@ -40,7 +40,11 @@ export function loadSocialGraph(
if (contacts) {
handleEvent(contacts);
} else {
replaceableEventsService.requestEvent(relay ? [relay, COMMON_CONTACT_RELAY] : [COMMON_CONTACT_RELAY], kind, pubkey);
replaceableEventsService.requestEvent(
relay ? [relay, ...COMMON_CONTACT_RELAYS] : COMMON_CONTACT_RELAYS,
kind,
pubkey,
);

// wait for event to load
const sub = eventStore.replaceable(kind, pubkey).subscribe((e) => {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/route/mute-modal-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { PropsWithChildren, createContext, useCallback, useContext, useEffect, u
import dayjs from "dayjs";
import { useInterval } from "react-use";

import { getDisplayName } from "../../helpers/nostr/user-metadata";
import { getDisplayName } from "../../helpers/nostr/profile";
import useUserProfile from "../../hooks/use-user-profile";
import useCurrentAccount from "../../hooks/use-current-account";
import {
Expand Down
6 changes: 3 additions & 3 deletions src/services/user-event-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _throttle from "lodash.throttle";
import { combineLatest, distinct, filter } from "rxjs";
import { USER_BLOSSOM_SERVER_LIST_KIND } from "blossom-client-sdk";

import { COMMON_CONTACT_RELAY } from "../const";
import { COMMON_CONTACT_RELAYS } from "../const";
import { logger } from "../helpers/debug";
import accountService from "./account";
import clientRelaysService from "./client-relays";
Expand All @@ -24,7 +24,7 @@ function downloadEvents(account: Account) {
};

log("Loading outboxes");
requestReplaceable([...relays, COMMON_CONTACT_RELAY], kinds.RelayList);
requestReplaceable([...relays, ...COMMON_CONTACT_RELAYS], kinds.RelayList);

const mailboxesSub = queryStore.mailboxes(account.pubkey).subscribe((mailboxes) => {
log("Loading user information");
Expand All @@ -35,7 +35,7 @@ function downloadEvents(account: Account) {

log("Loading contacts list");
replaceableEventsService.requestEvent(
[...clientRelaysService.readRelays.value, COMMON_CONTACT_RELAY],
[...clientRelaysService.readRelays.value, ...COMMON_CONTACT_RELAYS],
kinds.Contacts,
account.pubkey,
undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/services/username-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { from } from "rxjs";
import { filter, bufferTime, concatMap, mergeWith, shareReplay, map, scan } from "rxjs/operators";
import { getProfileContent, isFromCache } from "applesauce-core/helpers";

import { getSearchNames } from "../helpers/nostr/user-metadata";
import { getSearchNames } from "../helpers/nostr/profile";
import db from "./db";
import { eventStore } from "./event-store";
import { logger } from "../helpers/debug";
Expand Down
2 changes: 1 addition & 1 deletion src/views/discovery/dvm-feed/components/dvm-name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Link as RouterLink } from "react-router-dom";
import { nip19 } from "nostr-tools";

import useUserProfile from "../../../../hooks/use-user-profile";
import { getDisplayName } from "../../../../helpers/nostr/user-metadata";
import { getDisplayName } from "../../../../helpers/nostr/profile";
import { AddressPointer } from "nostr-tools/nip19";
import useDVMMetadata from "../../../../hooks/use-dvm-metadata";

Expand Down
11 changes: 5 additions & 6 deletions src/views/profile/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ import {
Link,
Textarea,
} from "@chakra-ui/react";
import dayjs from "dayjs";
import { useForm } from "react-hook-form";
import { ProfileContent, unixNow } from "applesauce-core/helpers";

import { ExternalLinkIcon } from "../../components/icons";
import { isLNURL } from "../../helpers/lnurl";
import { Kind0ParsedContent } from "../../helpers/nostr/user-metadata";
import { useReadRelays } from "../../hooks/use-client-relays";
import useCurrentAccount from "../../hooks/use-current-account";
import useUserProfile from "../../hooks/use-user-profile";
import dnsIdentityService from "../../services/dns-identity";
import { DraftNostrEvent } from "../../types/nostr-event";
import lnurlMetadataService from "../../services/lnurl-metadata";
import VerticalPageLayout from "../../components/vertical-page-layout";
import { COMMON_CONTACT_RELAY } from "../../const";
import { COMMON_CONTACT_RELAYS } from "../../const";
import { usePublishEvent } from "../../providers/global/publish-provider";

const isEmail =
Expand Down Expand Up @@ -234,7 +233,7 @@ export const ProfileEditView = () => {
);

const handleSubmit = async (data: FormData) => {
const newMetadata: Kind0ParsedContent = {
const newMetadata: ProfileContent = {
name: data.username,
picture: data.picture,
banner: data.banner,
Expand All @@ -253,13 +252,13 @@ export const ProfileEditView = () => {
}

const draft: DraftNostrEvent = {
created_at: dayjs().unix(),
created_at: unixNow(),
kind: 0,
content: JSON.stringify({ ...metadata, ...newMetadata }),
tags: [],
};

await publish("Update Profile", draft, [COMMON_CONTACT_RELAY]);
await publish("Update Profile", draft, COMMON_CONTACT_RELAYS);
};

return <MetadataForm defaultValues={defaultValues} onSubmit={handleSubmit} />;
Expand Down
Loading

0 comments on commit dbd724a

Please sign in to comment.