Lists with not followers fixed!

This commit is contained in:
minimo-io 2024-04-17 20:51:49 -03:00
parent 2869edc5cd
commit d62fff97e1
5 changed files with 136 additions and 58 deletions

View File

@ -5,6 +5,8 @@ Let promote some reciprocity here! 😹
## ToDo ## ToDo
- Add % value for progress.
- Query for userdata, at least for no-followers
- Remove SvelteKit (just Vite + Svelte). - Remove SvelteKit (just Vite + Svelte).
- Polish the proof-of-concept code. - Polish the proof-of-concept code.
- Configure Vite for the miniapp to be loaded in the article's url as base. - Configure Vite for the miniapp to be loaded in the article's url as base.

View File

@ -0,0 +1,22 @@
export const relays = [
"wss://relay.hodl.ar",
"wss://relay.current.fyi",
"wss://nostr.wine",
"wss://nostr.plebchain.org",
"wss://purplepag.es",
"wss://nos.lol",
"wss://nostr.mom",
"wss://nostrelay.yeghro.site",
"wss://relay.damus.io",
"wss://relay.nostr.bg",
"wss://relay.snort.social",
"wss://relay.primal.net",
"wss://nostr.bitcoiner.social",
"wss://nostr.mutinywallet.com",
"wss://relay.current.fyi",
"wss://relay.plebstr.com",
// "wss://nostr-pub.wellorder.net",
// "wss://brb.io",
// "wss://eden.nostr.land",
// "wss://nostr.orangepill.dev",
];

View File

@ -0,0 +1,9 @@
export async function fetchUserProfile(npub, ndk) {
const user = ndk.getUser({ npub });
await user.fetchProfile();
return user;
}
export async function fetchNotes(hexkey, ndk) {
const kind1filter = { kinds: [3], authors: [hexkey] };
return ndk.fetchEvent(kind1filter);
}

View File

@ -1,6 +1,8 @@
<script> <script>
// Import the package // Import the package
import NDK from "@nostr-dev-kit/ndk"; import NDK from "@nostr-dev-kit/ndk";
import { fetchUserProfile } from "$lib/fetchs";
import { relays } from "$lib/data/relays";
let npubToQuery = "npub1wujhdsytm3w6g0mpsqh8v7ezx83jcm64dlkwuqgm5v8lv0pds55ssudkw0"; let npubToQuery = "npub1wujhdsytm3w6g0mpsqh8v7ezx83jcm64dlkwuqgm5v8lv0pds55ssudkw0";
let userName; let userName;
@ -12,52 +14,18 @@
let unknownFollowBack = 0; let unknownFollowBack = 0;
let totalCountOfContactsChecked = 0; let totalCountOfContactsChecked = 0;
let originalFollow = [];
let notFollowersBack = []; let notFollowersBack = [];
async function fetchUserProfile(npub, ndk) { function updateNpub() {}
const user = ndk.getUser({ npub });
await user.fetchProfile();
// console.log("User profile:", user.profile);
// console.log("User profile:", user);
return user;
}
async function fetchNotes(hexkey, ndk) {
const kind1filter = { kinds: [3], authors: [hexkey] };
return ndk.fetchEvent(kind1filter);
// const kind1filter = { author: [hexkey] };
// return ndk.fetchEvent(kind1filter);
}
async function bootstrap() { async function bootstrap() {
try { try {
const ndk = new NDK({ const ndk = new NDK({
explicitRelayUrls: [ explicitRelayUrls: relays,
"wss://relay.hodl.ar",
"wss://relay.current.fyi",
"wss://nostr.wine",
"wss://nostr.plebchain.org",
"wss://purplepag.es",
"wss://nos.lol",
"wss://nostr.mom",
"wss://nostrelay.yeghro.site",
"wss://relay.damus.io",
"wss://relay.nostr.bg",
"wss://relay.snort.social",
"wss://relay.primal.net",
"wss://nostr.bitcoiner.social",
"wss://nostr.mutinywallet.com",
"wss://relay.current.fyi",
"wss://nostr-pub.wellorder.net",
// "wss://brb.io",
// "wss://eden.nostr.land",
// "wss://nostr.orangepill.dev",
],
}); });
await ndk.connect(); await ndk.connect();
// const user = await fetchUserProfile("npub1l8hja34gyeqrhc8plpcl3sql476n2rkgrzexazsfytwjkmy9kndqhx6pk9", ndk);
const user = await fetchUserProfile(npubToQuery, ndk); const user = await fetchUserProfile(npubToQuery, ndk);
userName = user.profile.name; userName = user.profile.name;
@ -70,16 +38,20 @@
followsCount = follows.size; followsCount = follows.size;
let lastFollowerBack; let lastFollowerBack;
let i = 0;
follows.forEach(async (follower) => { follows.forEach(async (follower) => {
await new Promise((resolve) => setTimeout(resolve, 1000)); //for (const follower of follows) {
// await new Promise((resolve) => setTimeout(resolve, 0));
const followerFollowList = await follower.follows(); const followerFollowList = await follower.follows();
originalFollow.push({ npub: follower.npub, followsBack: "-" }); // add to follower list
originalFollow = originalFollow;
if (followerFollowList.size) { if (followerFollowList.size) {
// console.log(followerFollowList); // console.log(followerFollowList);
// check if the user is in the queried user follow list // check if the user is in the queried user follow list
let doesFollowBack = false; let doesFollowBack = false;
for (const contact of followerFollowList) { for (let contact of followerFollowList) {
lastFollowerBack = contact.npub; lastFollowerBack = contact.npub;
if (contact.npub == npubToQuery) { if (contact.npub == npubToQuery) {
doesFollowBack = true; doesFollowBack = true;
@ -87,6 +59,8 @@
} }
} }
originalFollow[i].followBack = doesFollowBack ? "1" : "0";
// decision making time // decision making time
if (doesFollowBack) { if (doesFollowBack) {
followBackCount++; followBackCount++;
@ -95,7 +69,7 @@
} else { } else {
notFollowBackCount++; notFollowBackCount++;
notFollowersBack.push(lastFollowerBack); notFollowersBack.push(follower.npub);
notFollowersBack = notFollowersBack; notFollowersBack = notFollowersBack;
totalCountOfContactsChecked++; totalCountOfContactsChecked++;
} }
@ -103,14 +77,13 @@
unknownFollowBack++; unknownFollowBack++;
totalCountOfContactsChecked++; totalCountOfContactsChecked++;
} }
i++;
}); });
// follows.forEach(async (follower) => {
// });
} }
// console.log("Followers:");
// let followers = await fetchNotes(user.hexpubkey, ndk);
// console.log(followers.tags.length);
// const longNotes = await fetchLongNotes(hexkey, ndk);
} catch (error) { } catch (error) {
console.error("Error fetching data:", error); console.error("Error fetching data:", error);
} }
@ -128,16 +101,36 @@
<div class="user-box"> <div class="user-box">
<img src={userThumb} width="50" style="border-radius:100%;" alt="user-thumb" /> <img src={userThumb} width="50" style="border-radius:100%;" alt="user-thumb" />
User: {userName} |  Follows: {followsCount} User: {userName} |  Follows: {followsCount}
<br />
{npubToQuery}
<br /><br /> <br /><br />
Unknown: {unknownFollowBack} | Follow_Back: {followBackCount} | <strong>Not_Follow_Back</strong>: Unknown: {unknownFollowBack} | Follow_Back: {followBackCount} | <strong>Not_Follow_Back</strong>:
<span title="Actually Counted">{notFollowBackCount}</span> <span title="Actually Counted">{notFollowBackCount}</span>
/ <span title="Actualy counted">{notFollowersBack.length}</span> / <span title="Actualy counted">{notFollowersBack.length}</span>
<br /> <br />
Total Scanned = {totalCountOfContactsChecked} of {followBackCount + notFollowBackCount + unknownFollowBack} <p>
<br /><br /> Progress = {totalCountOfContactsChecked} of {followBackCount + notFollowBackCount + unknownFollowBack}
</p>
<hr />
<br />
Results ({originalFollow.length})
<ul>
{#each originalFollow as item, i}
<li>
#{i + 1} - {@html item.followBack == "0" ? "<span>🔴</span>" : "<span>🟢</span>"}
<a target="_blank noreferrer noopener" href="https://primal.net/p/{item.npub}">
{item.npub}
</a>
<!-- {JSON.stringify(item)} -->
</li>
{/each}
</ul>
<!-- <br />
<strong>They don't follow you ({notFollowersBack.length}):</strong> <strong>They don't follow you ({notFollowersBack.length}):</strong>
<br /><br /> <br />
{#each notFollowersBack as item, i}
<ul>
{#each notFollowersBack as item, i (item)}
<li> <li>
#{i + 1} - <a href="https://nostr.band/{item}" target="_blank noreferrer noopener">Nostr.Band</a> #{i + 1} - <a href="https://nostr.band/{item}" target="_blank noreferrer noopener">Nostr.Band</a>
/ <a href="https://primal.net/p/{item}" target="_blank noreferrer noopener">Primal</a> / <a href="https://primal.net/p/{item}" target="_blank noreferrer noopener">Primal</a>
@ -145,6 +138,7 @@
{item} {item}
</li> </li>
{/each} {/each}
</ul> -->
</div> </div>
{:else} {:else}
<div class="loader">Loading data...</div> <div class="loader">Loading data...</div>

View File

@ -0,0 +1,51 @@
<script>
import NDK from "@nostr-dev-kit/ndk";
import { fetchUserProfile } from "$lib/fetchs.js";
import { relays } from "$lib/data/relays";
let npubToQuery = "npub1fyjqppvxj9z8km5c9hmyfvvnrqj8qxcz82av3dse57v3lftvhz3q98p6ma";
let userName;
let userThumb;
let followsCount;
let listOfFollows = [];
async function init() {
try {
const ndk = new NDK({
explicitRelayUrls: relays,
});
await ndk.connect();
const user = await fetchUserProfile(npubToQuery, ndk);
userName = user.profile.name;
userThumb = user.profile.image;
if (userName) {
const follows = await user.follows();
followsCount = follows.size;
if (follows.size) {
follows.forEach((contact) => {
listOfFollows.push(contact.npub);
listOfFollows = listOfFollows;
});
}
}
} catch (error) {
console.error("Error fetching data:", error);
}
}
init();
</script>
<h3>This npub follows:</h3>
<img src={userThumb} width="50" style="border-radius:100%;" alt="user-thumb" />
User: {userName} |  Follows: {followsCount}
<br />
nPub: {npubToQuery}
<br /><br />
<ul>
{#each listOfFollows as item}
<li>{item}</li>
{/each}
</ul>