Fix profile loader

This commit is contained in:
Kieran 2022-12-27 12:33:52 +00:00
parent ff9de60b6b
commit f42e183bc8
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
5 changed files with 44 additions and 22 deletions

View File

@ -56,7 +56,7 @@ export default class Connection {
*/ */
AddSubscription(sub) { AddSubscription(sub) {
let req = ["REQ", sub.Id, sub.ToObject()]; let req = ["REQ", sub.Id, sub.ToObject()];
if(sub.OrSubs.length > 0) { if (sub.OrSubs.length > 0) {
req = [ req = [
...req, ...req,
...sub.OrSubs.map(o => o.ToObject()) ...sub.OrSubs.map(o => o.ToObject())
@ -71,9 +71,13 @@ export default class Connection {
* @param {any} subId Subscription id to remove * @param {any} subId Subscription id to remove
*/ */
RemoveSubscription(subId) { RemoveSubscription(subId) {
let req = ["CLOSE", subId]; if (this.Subscriptions[subId]) {
this._SendJson(req); let req = ["CLOSE", subId];
delete this.Subscriptions[subId]; this._SendJson(req);
delete this.Subscriptions[subId];
return true;
}
return false;
} }
_SendJson(obj) { _SendJson(obj) {

View File

@ -60,7 +60,8 @@ export class NostrSystem {
}; };
sub.OnEnd = (c) => { sub.OnEnd = (c) => {
c.RemoveSubscription(sub.Id); c.RemoveSubscription(sub.Id);
if(--counter === 0) { console.debug(counter);
if (counter-- <= 0) {
resolve(events); resolve(events);
} }
}; };
@ -68,6 +69,15 @@ export class NostrSystem {
s.AddSubscription(sub); s.AddSubscription(sub);
counter++; counter++;
} }
// force timeout returning current results
setTimeout(() => {
for (let s of Object.values(this.Sockets)) {
s.RemoveSubscription(sub.Id);
counter++;
}
resolve(events);
}, 10_000);
}); });
} }
} }

View File

@ -14,22 +14,26 @@ export default function useUsersStore() {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
function isUserCached(id) { function isUserCached(id) {
let expire = new Date().getTime() - 60_000; // 60s expire let expire = new Date().getTime() - (1_000 * 60 * 5) ; // 60s expire
let u = users[id]; let u = users[id];
return u && (u.loaded || 0) < expire; return u && u.loaded > expire;
} }
async function getUsers() { async function getUsers() {
let needProfiles = pKeys.filter(a => !isUserCached(a)); let needProfiles = pKeys.filter(a => !isUserCached(a));
if(needProfiles.length === 0) {
return;
}
console.debug("Need profiles: ", needProfiles);
let sub = new Subscriptions(); let sub = new Subscriptions();
sub.Authors = new Set(needProfiles); sub.Authors = new Set(needProfiles);
sub.Kinds.add(EventKind.SetMetadata); sub.Kinds.add(EventKind.SetMetadata);
let events = await system.RequestSubscription(sub); let events = await system.RequestSubscription(sub);
console.debug("Got events: ", events);
let loaded = new Date().getTime(); let loaded = new Date().getTime();
let profiles = events.map(a => { let profiles = events.filter(a => a.kind === EventKind.SetMetadata).map(a => {
let metaEvent = Event.FromObject(a); let metaEvent = Event.FromObject(a);
let data = JSON.parse(metaEvent.Content); let data = JSON.parse(metaEvent.Content);
return { return {
@ -40,10 +44,14 @@ export default function useUsersStore() {
}; };
}); });
let missing = needProfiles.filter(a => !events.some(b => b.pubkey === a)); let missing = needProfiles.filter(a => !events.some(b => b.pubkey === a));
let missingProfiles = missing.map(a => new{ let missingProfiles = missing.map(a => {
pubkey: a, return {
loaded pubkey: a,
loaded
}
}); });
console.debug("Got profiles: ", profiles);
console.debug("Missing profiles: ", missing);
dispatch(setUserData([ dispatch(setUserData([
...profiles, ...profiles,
...missingProfiles ...missingProfiles

View File

@ -3,13 +3,8 @@ import { createSlice } from '@reduxjs/toolkit'
const PrivateKeyItem = "secret"; const PrivateKeyItem = "secret";
const RelayList = "relays"; const RelayList = "relays";
const DefaultRelays = JSON.stringify([ const DefaultRelays = JSON.stringify([
"wss://nostr.v0l.io",
"wss://nostr-pub.wellorder.net", "wss://nostr-pub.wellorder.net",
"wss://nostr.zebedee.cloud",
"wss://relay.damus.io", "wss://relay.damus.io",
"wss://nostr.rocks",
"wss://nostr.rocks",
"wss://nostr.fmt.wiz.biz"
]); ]);
const LoginSlice = createSlice({ const LoginSlice = createSlice({

View File

@ -37,16 +37,21 @@ const UsersSlice = createSlice({
if (!Array.isArray(ud)) { if (!Array.isArray(ud)) {
ud = [ud]; ud = [ud];
} }
console.debug("Set user profiles: ", ud);
for (let x of ud) { for (let x of ud) {
let existing = state.users[ud.pubkey]; let existing = state.users[x.pubkey];
if (existing) { if (existing) {
ud = { x = {
...existing, ...existing,
...ud ...x
}; };
} }
state.users[ud.pubkey] = ud; state.users[x.pubkey] = x;
window.localStorage.setItem(`user:${ud.pubkey}`, JSON.stringify(ud)); window.localStorage.setItem(`user:${x.pubkey}`, JSON.stringify(x));
let newUsersObj = {};
Object.assign(newUsersObj, state.users);
state.users = newUsersObj;
} }
} }
} }