Skip to content

Commit

Permalink
Merge pull request #2065 from shubhamkmr04/shubham/refactor-nostr-con…
Browse files Browse the repository at this point in the history
…tacts

Refactor fetchNostrContacts
  • Loading branch information
kaloudis authored Mar 19, 2024
2 parents c25fd04 + 9b9d760 commit 1c37cae
Showing 1 changed file with 92 additions and 80 deletions.
172 changes: 92 additions & 80 deletions views/NostrContacts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,96 +106,108 @@ export default class NostrContacts extends React.Component<
}
}

DEFAULT_NOSTR_RELAYS.map(async (relayItem) => {
const relay = relayInit(relayItem);
relay.on('connect', () => {
console.log(`connected to ${relay.url}`);
});
relay.on('error', () => {
console.log(`failed to connect to ${relay.url}`);
});
const profilesEventsPromises = DEFAULT_NOSTR_RELAYS.map(
async (relayItem) => {
const relay = relayInit(relayItem);
relay.on('connect', () => {
console.log(`connected to ${relay.url}`);
});
relay.on('error', () => {
console.log(`failed to connect to ${relay.url}`);
});

await relay.connect();
let eventReceived = await relay.list([
{
authors: [pubkey],
kinds: [3]
}
]);

let latestContactEvent: any;

eventReceived.forEach((content) => {
if (
!latestContactEvent ||
content.created_at > latestContactEvent.created_at
) {
latestContactEvent = content;
}
});
await relay.connect();
let eventReceived = await relay.list([
{
authors: [pubkey],
kinds: [3]
}
]);

if (!latestContactEvent) return;
let latestContactEvent: any;

const tags: Array<string> = [];
latestContactEvent.tags.forEach((tag: string) => {
if (tag[0] === 'p') {
tags.push(tag[1]);
}
});
eventReceived.forEach((content) => {
if (
!latestContactEvent ||
content.created_at > latestContactEvent.created_at
) {
latestContactEvent = content;
}
});

const profilesEvents = await relay.list([
{
authors: tags,
kinds: [0]
}
]);
if (!latestContactEvent) return;

const newContactDataIndexByName: any = {};
const newContactDataIndexByPubkey: any = {};
const newContactsData: any[] = [];
const tags: Array<string> = [];
latestContactEvent.tags.forEach((tag: string) => {
if (tag[0] === 'p') {
tags.push(tag[1]);
}
});

profilesEvents.forEach((item) => {
try {
const content = JSON.parse(item.content);
if (
!newContactDataIndexByPubkey[item.pubkey] ||
item.created_at >
newContactDataIndexByPubkey[item.pubkey].timestamp
) {
newContactDataIndexByPubkey[item.pubkey] = {
content,
timestamp: item.created_at
};
return relay.list([
{
authors: tags,
kinds: [0]
}
} catch (error: any) {
// Handle the error, e.g., log it or skip this item
this.setState({ loading: false });
console.error(
`Error parsing JSON for item with ID ${item.id}: ${error.message}`
);
}
});
Object.keys(newContactDataIndexByPubkey).forEach((pubkey) => {
const content = newContactDataIndexByPubkey[pubkey].content;
if (!content?.npub) {
content.npub = nip19.npubEncode(pubkey);
}
newContactDataIndexByName[
content?.display_name?.toLowerCase() ||
content?.name?.toLowerCase()
] = content;
});
]);
}
);

Object.keys(newContactDataIndexByName)
.sort()
.forEach((name) => {
newContactsData.push(newContactDataIndexByName[name]);
Promise.all(profilesEventsPromises)
.then((profilesEventsArrays) => {
const profileEvents = profilesEventsArrays
.flat()
.filter((event) => event !== undefined);
const newContactDataIndexByName: any = {};
const newContactDataIndexByPubkey: any = {};
const newContactsData: any[] = [];

profileEvents.forEach((item: any) => {
try {
const content = JSON.parse(item.content);
if (
!newContactDataIndexByPubkey[item.pubkey] ||
item.created_at >
newContactDataIndexByPubkey[item.pubkey]
.timestamp
) {
newContactDataIndexByPubkey[item.pubkey] = {
content,
timestamp: item.created_at
};
}
} catch (error: any) {
// Handle the error, e.g., log it or skip this item
this.setState({ loading: false });
console.error(
`Error parsing JSON for item with ID ${item.id}: ${error.message}`
);
}
});
this.setState({
contactsData: newContactsData,
loading: false
Object.keys(newContactDataIndexByPubkey).forEach((pubkey) => {
const content = newContactDataIndexByPubkey[pubkey].content;
if (!content?.npub) {
content.npub = nip19.npubEncode(pubkey);
}
newContactDataIndexByName[
content?.display_name?.toLowerCase() ||
content?.name?.toLowerCase()
] = content;
});

Object.keys(newContactDataIndexByName)
.sort()
.forEach((name) => {
newContactsData.push(newContactDataIndexByName[name]);
});
this.setState({
contactsData: newContactsData,
loading: false
});
})
.catch((error) => {
console.error('Error fetching profiles events:', error);
});
});
}

toggleContactSelection = (contact: any) => {
Expand Down

0 comments on commit 1c37cae

Please sign in to comment.