working on showing single posts for users we dont know about
This commit is contained in:
45
src/App.ts
45
src/App.ts
@@ -1,7 +1,7 @@
|
||||
import { generateID } from "IDUtils";
|
||||
import { PeerManager, PeerEventTypes } from "PeerManager";
|
||||
import { Sync } from "Sync";
|
||||
import { openDatabase, getData, addData, addDataArray, clearData, deleteData, mergeDataArray, getAllData, checkPostIds, getAllIds, getPostsByIds } from "db";
|
||||
import { openDatabase, getData, addData, addDataArray, clearData, deleteData, mergeDataArray, getAllData, checkPostIds, getAllIds, getPostsByIds, getPostForUser } from "db";
|
||||
import { arrayBufferToBase64, compressString } from "dataUtils";
|
||||
import { log, logID, renderLog, setLogVisibility } from "log"
|
||||
|
||||
@@ -189,7 +189,7 @@ export class App {
|
||||
let neededPostCount = neededPostIDs.length;
|
||||
this.statusBar.updatePeerStatus(peerID, `need(${logID(userID)} | ${neededPostCount})`, { havePostCount: 0, neededPostCount: neededPostCount });
|
||||
|
||||
let neededPosts = await this.peerManager?.rpc.getPostsForUser(peerID, this.peerID, userID, neededPostIDs);
|
||||
await this.peerManager?.rpc.getPostsForUser(peerID, this.peerID, userID, neededPostIDs);
|
||||
}
|
||||
else {
|
||||
console.log.apply(null, log(`[app] Don't need any posts for user ${logID(userID)} from peer ${logID(peerID)}`));
|
||||
@@ -236,7 +236,7 @@ export class App {
|
||||
for (let userID of userIDs) {
|
||||
// console.log.apply(null, log(`[app] announceUsers, got user:${userID} from peer ${sendingPeerID}`));
|
||||
this.sync.addUserPeer(userID, sendingPeerID);
|
||||
if (!(this.sync.shouldSyncUserID(userID) || (this.router.route === App.Route.USER && userID === this.router.userID))) {
|
||||
if (!(this.sync.shouldSyncUserID(userID) || (this.router.route & (App.Route.USER | App.Route.POST) && userID === this.router.userID))) {
|
||||
console.log.apply(null, log(`[app] announceUser_rpc_response skipping user[${logID(userID)}] from[${logID(sendingPeerID)}]`));
|
||||
continue;
|
||||
}
|
||||
@@ -244,7 +244,17 @@ export class App {
|
||||
console.log.apply(null, log(`[app] calling getPostIDsForUser for user [${logID(userID)}] on peer [${logID(sendingPeerID)}]`));
|
||||
this.statusBar.updatePeerStatus(sendingPeerID, `getPostIDs(${logID(userID)})⬆️`);
|
||||
|
||||
let postIDs = await this.peerManager?.rpc.getPostIDsForUser(sendingPeerID, userID);
|
||||
|
||||
let postIDs = null;
|
||||
if (this.router.route === App.Route.POST && this.router.userID == userID) {
|
||||
postIDs = [this.router.postID];
|
||||
} else {
|
||||
postIDs = await this.peerManager?.rpc.getPostIDsForUser(sendingPeerID, userID);
|
||||
}
|
||||
|
||||
if (!postIDs) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.statusBar.updatePeerStatus(sendingPeerID, `syncing(${logID(userID)} ${postIDs.length})`);
|
||||
|
||||
@@ -306,6 +316,8 @@ export class App {
|
||||
if (postIDs) {
|
||||
return postIDs;
|
||||
}
|
||||
|
||||
return [];
|
||||
});
|
||||
|
||||
this.peerManager.registerRPC('getPostsForUser', async (requestingPeerID: string, userID: string, postIDs: string[]) => {
|
||||
@@ -1078,17 +1090,20 @@ export class App {
|
||||
this.timerStart();
|
||||
let posts: StoragePost[] = [];
|
||||
|
||||
// if (postID) {
|
||||
// posts = await gePostForUser(userID, postID);
|
||||
// }
|
||||
if (postID) {
|
||||
posts = await getPostForUser(userID, postID);
|
||||
} else {
|
||||
posts = await getData(userID, new Date(2022, 8), new Date());
|
||||
}
|
||||
|
||||
posts = await getData(userID, new Date(2022, 8), new Date());
|
||||
|
||||
if (posts.length > 0) {
|
||||
if (posts?.length) {
|
||||
console.log.apply(null, log(`Loaded ${posts.length} posts in ${this.timerDelta().toFixed(2)}ms`));;
|
||||
return posts;
|
||||
}
|
||||
|
||||
console.log.apply(null, log(`No posts found for userID:${userID}, postID:${postID}`));;
|
||||
|
||||
// posts = await createTestData2(userID);
|
||||
|
||||
// log("Adding test data...");
|
||||
@@ -1602,11 +1617,11 @@ export class App {
|
||||
|
||||
export namespace App {
|
||||
export enum Route {
|
||||
USER,
|
||||
POST,
|
||||
MEDIA,
|
||||
GROUP,
|
||||
HOME,
|
||||
CONNECT,
|
||||
USER = 1,
|
||||
POST = 2,
|
||||
MEDIA = 4,
|
||||
GROUP = 8,
|
||||
HOME = 16,
|
||||
CONNECT = 32,
|
||||
}
|
||||
};
|
||||
@@ -538,7 +538,7 @@ class PeerConnection {
|
||||
private chunkSize = (16 * 1024) - 100;
|
||||
messageSuperlog: boolean = false;
|
||||
sendQueueSuperLog: boolean = false;
|
||||
rpcSuperlog: boolean = false;
|
||||
rpcSuperlog: boolean = true;
|
||||
pendingRPCs: Map<
|
||||
string,
|
||||
{ resolve: Function; reject: Function; functionName: string }
|
||||
@@ -556,9 +556,9 @@ class PeerConnection {
|
||||
// { urls: "turn:ddln.app", username: "a", credential: "b" },
|
||||
{ urls: "stun:stun.l.google.com" }, // keeping this for now as my STUN server is not returning ipv6
|
||||
{ urls: "stun:stun1.l.google.com" },
|
||||
{ urls: "stun:stun2.l.google.com" },
|
||||
{ urls: "stun:stun3.l.google.com" },
|
||||
{ urls: "stun:stun4.l.google.com" },
|
||||
//{ urls: "stun:stun2.l.google.com" },
|
||||
//{ urls: "stun:stun3.l.google.com" },
|
||||
//{ urls: "stun:stun4.l.google.com" },
|
||||
],
|
||||
};
|
||||
// longMessageQueue: string[] = [];
|
||||
|
||||
@@ -36,7 +36,7 @@ export async function compressString(input: string) {
|
||||
const compressedArray = await new Response(compressionStream.readable).arrayBuffer();
|
||||
|
||||
// Convert the compressed data to a Uint8Array
|
||||
return new Uint8Array(compressedArray);
|
||||
return compressedArray;
|
||||
}
|
||||
|
||||
// Base58 character set
|
||||
|
||||
14
src/db.ts
14
src/db.ts
@@ -342,7 +342,21 @@ export async function mergeDataArray(userID: string, array: any[]): Promise<void
|
||||
}
|
||||
|
||||
export async function getPostForUser(userID: string, postID: string): Promise<any | undefined> {
|
||||
const { store } = await getDBTransactionStore(userID);
|
||||
const index = store.index("postIDIndex");
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const getPostRequest = index.get(postID);
|
||||
|
||||
getPostRequest.onsuccess = () => {
|
||||
resolve(getPostRequest.result);
|
||||
};
|
||||
|
||||
getPostRequest.onerror = () => {
|
||||
console.error('Transaction failed:', getPostRequest.error?.message);
|
||||
reject(getPostRequest.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user