From b72957fc4b14ea2c8e7fd15fc4655afc6c8de620 Mon Sep 17 00:00:00 2001 From: kieran Date: Thu, 5 Sep 2024 10:10:25 +0100 Subject: [PATCH] Use rust nostr-services for events/profiles --- NostrServices/Services/RedisStore.cs | 61 +++++++++++++++++----------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/NostrServices/Services/RedisStore.cs b/NostrServices/Services/RedisStore.cs index 1f0f362..ff3fe73 100644 --- a/NostrServices/Services/RedisStore.cs +++ b/NostrServices/Services/RedisStore.cs @@ -1,5 +1,6 @@ using NBitcoin; using Nostr.Client.Identifiers; +using Nostr.Client.Json; using Nostr.Client.Messages; using Nostr.Client.Utils; using NostrServices.Client; @@ -13,56 +14,67 @@ public class RedisStore private static readonly TimeSpan DefaultExpire = TimeSpan.FromDays(90); private readonly IDatabase _database; private readonly ConnectionMultiplexer _connection; + private readonly HttpClient _client; - public RedisStore(ConnectionMultiplexer connection) + public RedisStore(ConnectionMultiplexer connection, HttpClient client) { _connection = connection; + _client = client; _database = connection.GetDatabase(); } public async Task StoreEvent(CompactEvent ev, TimeSpan? expiry = null) { - if (await _database.SetAsync(EventKey(ev.ToIdentifier()), ev, expiry ?? DefaultExpire)) - { - return await _database.SetAddAsync(UserEventsKey(ev.PubKey.ToHex()), ev.Id); - } - - return false; + return true; } public async Task GetEvent(NostrIdentifier id) { - var ek = EventKey(id); - var ev = await _database.GetAsync(ek); - if (ev != default) + try { - await _database.KeyExpireAsync(ek, DefaultExpire); + var ev = await _client.GetAsync($"https://nostr-rs.api.v0l.io/event/{id.ToBech32()}"); + if (ev.IsSuccessStatusCode) + { + var obj = NostrJson.Deserialize(await ev.Content.ReadAsStringAsync()); + if (obj != default) + { + return CompactEvent.FromNostrEvent(obj); + } + } + } + catch + { + // ignored } - return ev; + return default; } public async Task StoreProfile(CompactProfile meta, TimeSpan? expiry = null) { - var oldProfile = await GetProfile(meta.PubKey.ToHex()); - if ((oldProfile?.Created ?? new DateTime()) < meta.Created) - { - return await _database.SetAsync(ProfileKey(meta.PubKey.ToHex()), meta, expiry ?? DefaultExpire); - } - - return false; + return true; } public async Task GetProfile(string id) { - var pk = ProfileKey(id); - var profile = await _database.GetAsync(pk); - if (profile != default) + try { - await _database.KeyExpireAsync(pk, DefaultExpire); + var ev = await _client.GetAsync($"https://nostr-rs.api.v0l.io/event/0/{id}"); + if (ev.IsSuccessStatusCode) + { + var evo = NostrJson.Deserialize(await ev.Content.ReadAsStringAsync()); + if (evo != default) + { + return CompactProfile.FromNostrEvent(evo); + } + } + } + catch + { + // ignored } - return profile; + return default; } public async Task> GetKnownRelays() @@ -149,6 +161,7 @@ public class RedisStore { break; } + var id = ((string)gr.Member!).Split('\x1'); var u = new Uri(id[0]); var info = await GetRelay(u);