Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more functions #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ interface SubInfo {
last_hit: number;
}

interface UserInfo {
name?: string;
display_name?: string;
image?: string;
picture?: string;
banner?: string;
bio?: string;
nip05?: string;
lud06?: string;
lud16?: string;
about?: string;
website?: string;
zapService?: string;
}

// very thin wrapper using SimplePool + ArcadeIdentity
export class NostrPool {
ident: ArcadeIdentity;
Expand All @@ -30,7 +45,10 @@ export class NostrPool {
this.filters = new Map<string, SubInfo>();
}

async list(filters: Filter<number>[], db_only = false): Promise<NostrEvent[]> {
async list(
filters: Filter<number>[],
db_only = false
): Promise<NostrEvent[]> {
if (this.db) {
const since = await this.db.latest(filters);
if (db_only) {
Expand Down Expand Up @@ -246,6 +264,32 @@ export class NostrPool {
});
}

async getProfile(pubkey: string, db_only = false): Promise<UserInfo> {
const ev = await this.list([{ kinds: [0], authors: [pubkey] }], db_only);
console.log(ev);
if (ev.length > 0) {
const payload = JSON.parse(ev[0].content);
return { ...payload };
} else {
throw new Error(`User not found`);
}
}

async getContacts(db_only = false): Promise<string[]> {
const ev = await this.list(
[{ kinds: [3], authors: [this.ident.pubKey] }],
db_only
);
if (ev.length > 0) {
const contacts = [];
for (const item of ev[0].tags) {
contacts.push(item[1]);
}
return contacts;
} else {
throw new Error(`Relay not return anything`);
}
}

/**
* This function adds a callback function to an array of event callbacks.
Expand Down
25 changes: 25 additions & 0 deletions test/pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,29 @@ describe('NostrPool', () => {
expect(ret.length).toBe(4);
pool1.close();
});

it('get user profile and contact', async () => {
const db = connectDb();
const pool1 = new NostrPool(ident, db);
if (!pool1.db) throw Error;
await pool1.setRelays(relays);

await pool1.send({
content: JSON.stringify({ name: 'yo' }),
kind: 0,
tags: [],
});

await pool1.send({
content: '',
kind: 3,
tags: [['p', ident.pubKey]],
});

const profile = await pool1.getProfile(ident.pubKey);
expect(profile.name).toBeTruthy;

const contacts = await pool1.getContacts();
expect(contacts).toBeTruthy;
});
});