mirror of
https://github.com/aljazceru/mcp-code.git
synced 2025-12-17 12:45:28 +01:00
initial commit
This commit is contained in:
51
update-follow-list.ts
Normal file
51
update-follow-list.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import {
|
||||
type NDKEvent,
|
||||
type NDKUser,
|
||||
profileFromEvent,
|
||||
} from "@nostr-dev-kit/ndk";
|
||||
import { ndk } from "./ndk.js";
|
||||
import { knownUsers, saveKnownUsers, saveUserProfile } from "./users.js";
|
||||
import { addFollows } from "./wot.js";
|
||||
|
||||
export async function updateFollowList(user: NDKUser) {
|
||||
const follows = await user.followSet();
|
||||
const followsList = Array.from(follows.keys());
|
||||
|
||||
// Store follows in the database
|
||||
addFollows(user.pubkey, followsList);
|
||||
|
||||
const unknownUsers = new Set<string>();
|
||||
|
||||
for (const follow of followsList) {
|
||||
if (!knownUsers[follow]) {
|
||||
unknownUsers.add(follow);
|
||||
}
|
||||
}
|
||||
|
||||
const profilesSub = ndk.subscribe([
|
||||
{ kinds: [0, 3], authors: Array.from(unknownUsers) },
|
||||
]);
|
||||
|
||||
profilesSub.on("event", (event: NDKEvent) => {
|
||||
if (event.kind === 0) handleProfileEvent(event);
|
||||
else if (event.kind === 3) handleFollowEvent(event);
|
||||
});
|
||||
|
||||
profilesSub.on("eose", () => {
|
||||
saveKnownUsers();
|
||||
});
|
||||
}
|
||||
|
||||
function handleProfileEvent(event: NDKEvent) {
|
||||
const profile = profileFromEvent(event);
|
||||
// Save directly to database instead of just updating the cache
|
||||
saveUserProfile(event.pubkey, profile, event.content);
|
||||
}
|
||||
|
||||
function handleFollowEvent(event: NDKEvent) {
|
||||
const follows = event.tags
|
||||
.filter((tag) => tag[0] === "p")
|
||||
.map((tag) => tag[1])
|
||||
.filter(Boolean) as string[];
|
||||
addFollows(event.pubkey, follows);
|
||||
}
|
||||
Reference in New Issue
Block a user