aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/nostr.ts57
-rw-r--r--lib/strfry.ts16
-rw-r--r--lib/utils.ts22
3 files changed, 44 insertions, 51 deletions
diff --git a/lib/nostr.ts b/lib/nostr.ts
index f3befa03..5ef8af3d 100644
--- a/lib/nostr.ts
+++ b/lib/nostr.ts
@@ -1,6 +1,7 @@
import config from "$config";
import { g, s } from "$lib/db";
import { l } from "$lib/logging";
+import { count, scan } from "$lib/strfry";
import { fail } from "$lib/utils";
import { finalizeEvent, getPublicKey, nip19 } from "nostr-tools";
import { AbstractSimplePool } from "nostr-tools/abstract-pool";
@@ -18,7 +19,7 @@ export const pool = new AbstractSimplePool({ verifyEvent: alwaysTrue });
const opts = { maxWait: 2000 };
export const anon = (pubkey) => ({
- name: pubkey.substr(0, 6),
+ username: pubkey.substr(0, 6),
pubkey,
anon: true,
follows: [],
@@ -101,15 +102,16 @@ export async function handleZap(invoice) {
export const getRelays = async (pubkey): Promise<any> => {
const { relays } = config;
const filter = { authors: [pubkey], kinds: [10002] };
- const event = await pool.get([config.nostr], filter, opts);
+ const result = await scan(filter);
let read = relays;
let write = relays;
- if (event) {
+ if (result.length) {
+ const { tags } = result[0];
read = [];
write = [];
- for (const r of event.tags) {
+ for (const r of tags) {
if (r[0] !== "r") continue;
if (!r[2] || r[2] === "write") write.push(r[1]);
if (!r[2] || r[2] === "read") read.push(r[1]);
@@ -157,44 +159,15 @@ export const getProfile = async (pubkey, relays = [config.nostr]) => {
};
export const getCount = async (pubkey) => {
- let follows = await g(`${pubkey}:follows:n`);
- let followers = await g(`${pubkey}:followers:n`);
-
- if (follows === null) {
- const filter: any = { kinds: [3], authors: [pubkey] };
- let ev = await pool.get([config.nostr], filter, opts);
-
- if (!ev) {
- let { relays } = config;
- ({ write: relays } = await getRelays(pubkey));
- ev = await pool.get(relays, filter, opts);
- send(ev);
- }
-
- if (ev) {
- follows = ev.tags
- .map((t) => t[0] === "p" && t[1])
- .filter((p) => p && p.length === 64).length;
- }
-
- follows ||= 0;
- await s(`${pubkey}:follows:n`, follows);
- }
-
- if (followers === null) {
- const filter: any = { cache: ["user_infos", { pubkeys: [pubkey] }] };
- const infos = await pool.querySync([config.cache], filter, opts);
-
- const f = infos.find((e) => e.kind === 10000133);
- let counts = {};
- if (f?.content) {
- counts = JSON.parse(f.content);
- followers = counts[pubkey];
- }
+ try {
+ const result = await scan({ authors: [pubkey], kinds: [3] });
+ const follows = result.length
+ ? result[0].tags.filter((t) => t[0] === "p").length
+ : 0;
- followers ||= 0;
- await s(`${pubkey}:followers:n`, followers);
+ const [followers] = await count({ "#p": [pubkey], kinds: [3] });
+ return { follows, followers };
+ } catch (e) {
+ console.log(e);
}
-
- return { follows, followers };
};
diff --git a/lib/strfry.ts b/lib/strfry.ts
index 9caa539e..231e3533 100644
--- a/lib/strfry.ts
+++ b/lib/strfry.ts
@@ -1,5 +1,5 @@
-import { createServer, connect } from "net";
import { randomUUID } from "crypto";
+import { connect, createServer } from "net";
const exec = async (cmd) =>
new Promise((resolve, reject) => {
@@ -13,7 +13,6 @@ const exec = async (cmd) =>
const results = [];
socket.on("data", (data) => {
resultBuffer += data.toString();
-
const parts = resultBuffer.split("\n");
for (let i = 0; i < parts.length - 1; i++) {
@@ -44,16 +43,17 @@ const exec = async (cmd) =>
controlClient.end();
});
- controlClient.on("error", (e) =>
- reject(`Control socket error: ${e.message}`),
- );
+ controlClient.on("error", (e) => {
+ reject(`Control socket error: ${e.message}`);
+ });
});
- resultServer.on("error", (e) =>
- reject(`Result server error: ${e.message}`),
- );
+ resultServer.on("error", (e) => {
+ reject(`Result server error: ${e.message}`);
+ });
});
+export const count = (f) => exec(`scan --count '${JSON.stringify(f)}'\n`);
export const scan = (f) => exec(`scan '${JSON.stringify(f)}'\n`);
export const sync = (r, f) =>
exec(`sync ${r} --dir down --filter '${JSON.stringify(f)}'\n`);
diff --git a/lib/utils.ts b/lib/utils.ts
index 7dc6b9ef..9cb6a690 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -2,6 +2,7 @@ import config from "$config";
import { g, s } from "$lib/db";
import locales from "$lib/locales/index";
import migrate from "$lib/migrate";
+import whitelist from "$lib/whitelist";
import { bytesToHex, randomBytes } from "@noble/hashes/utils";
import { getPublicKey } from "nostr";
@@ -68,7 +69,7 @@ export const fields = [
"banner",
];
-export const getUser = async (username) => {
+export const getUser = async (username, fields = whitelist) => {
let update;
if (username === "undefined") fail("invalid user");
const user = await migrate(username);
@@ -206,3 +207,22 @@ export const fmt = (sats) =>
)}`;
export const link = (id) => `${URL}/payment/${id}`;
+
+export const publicFields = [
+ "about",
+ "anon",
+ "banner",
+ "banner",
+ "currency",
+ "display",
+ "id",
+ "hidepay",
+ "lud16",
+ "memoPrompt",
+ "npub",
+ "picture",
+ "prompt",
+ "pubkey",
+ "username",
+ "website",
+];