Announce users the local peers knows to remote peers when they connect. Fix RPC system to correctly serialize and multiple params. Add detailed logging for rpc calls.

This commit is contained in:
2025-05-18 20:26:54 -07:00
parent 28050f1a54
commit 3905368bbb
4 changed files with 174 additions and 34 deletions

View File

@@ -1,5 +1,60 @@
import { openDatabase, getData, addData, addDataArray, clearData, deleteData, mergeDataArray, getAllData, checkPostIds, getAllIds, getPostsByIds } from "db";
import { log, logID } from "log";
export class Sync {
static async getFollowing(userID: string): Promise<string[]> {
userID: string = "";
userIDsToSync: Set<string> = new Set();
constructor() {
}
setUserID(userID: string) {
this.userIDsToSync = new Set(this.getFollowing(userID));
}
shouldSyncUserID(userID: string) {
return true;
}
// shouldSyncUserID(userID: string) {
// if (app.isHeadless) {
// return true;
// }
// return this.UserIDsTothis.has(userID);
// }
async getKnownUsers() {
let knownUsers = [...(await indexedDB.databases())].map(db => db.name?.replace('user_', '')).filter(userID => userID !== undefined);
knownUsers = knownUsers
.filter(userID => this.shouldSyncUserID(userID))
.filter(userID => !this.userBlockList.has(userID))
.filter(async userID => (await getAllIds(userID)).length > 0); // TODO:EASYOPT getting all the IDs is unecessary, replace it with a test to get a single ID.
return knownUsers;
}
userPeers: Map<string, string[]> = new Map();
userBlockList = new Set([
'5d63f0b2-a842-41bf-bf06-e0e4f6369271',
'5f1b85c4-b14c-454c-8df1-2cacc93f8a77',
// 'bba3ad24-9181-4e22-90c8-c265c80873ea'
])
postBlockList = new Set([
'1c71f53c-c467-48e4-bc8c-39005b37c0d5',
'64203497-f77b-40d6-9e76-34d17372e72a',
'243130d8-4a41-471e-8898-5075f1bd7aec',
'e01eff89-5100-4b35-af4c-1c1bcb007dd0',
'194696a2-d850-4bb0-98f7-47416b3d1662',
'f6b21eb1-a0ff-435b-8efc-6a3dd70c0dca',
'dd1d92aa-aa24-4166-a925-94ba072a9048'
]);
getFollowing(userID: string): string[] {
// Rob
if (userID === 'b38b623c-c3fa-4351-9cab-50233c99fa4e') {
@@ -32,4 +87,29 @@ export class Sync {
return ['a0e42390-08b5-4b07-bc2b-787f8e5f1297']; // Follow BMO by default :)
}
async getPostIdsForUser(userID: string) {
let postIds = await getAllIds(userID) ?? [];
postIds = postIds.filter((postID: string) => !this.postBlockList.has(postID));
if (postIds.length === 0) {
console.log.apply(null, log(`Net: I know about user ${logID(userID)} but I have 0 posts`));;
return null;
}
return postIds;
}
// async getPostIdsForUserHandler(data: any) {
// let message = data.message;
// let postIds = await getAllIds(message.user_id) ?? [];
// postIds = postIds.filter((postID: string) => !this.postBlockList.has(postID));
// if (postIds.length === 0) {
// console.log.apply(null, log(`Net: I know about user ${logID(message.user_id)} but I have 0 posts, so I'm not sending any to to peer ${logID(data.from)}`));;
// return;
// }
// console.log.apply(null, log(`Net: Sending ${postIds.length} post Ids for user ${logID(message.user_id)} to peer ${logID(data.from)}`));
// let responseMessage = { type: "peer_message", from: app.peerID, to: data.from, from_username: app.username, from_peername: app.peername, message: { type: "get_post_ids_for_user_response", post_ids: postIds, user_id: message.user_id } }
// this.send(responseMessage);
// }
}