Use rust nostr-services for events/profiles

This commit is contained in:
kieran 2024-09-05 10:10:25 +01:00
parent 4e89ba6775
commit b72957fc4b
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941

View File

@ -1,5 +1,6 @@
using NBitcoin; using NBitcoin;
using Nostr.Client.Identifiers; using Nostr.Client.Identifiers;
using Nostr.Client.Json;
using Nostr.Client.Messages; using Nostr.Client.Messages;
using Nostr.Client.Utils; using Nostr.Client.Utils;
using NostrServices.Client; using NostrServices.Client;
@ -13,56 +14,67 @@ public class RedisStore
private static readonly TimeSpan DefaultExpire = TimeSpan.FromDays(90); private static readonly TimeSpan DefaultExpire = TimeSpan.FromDays(90);
private readonly IDatabase _database; private readonly IDatabase _database;
private readonly ConnectionMultiplexer _connection; private readonly ConnectionMultiplexer _connection;
private readonly HttpClient _client;
public RedisStore(ConnectionMultiplexer connection) public RedisStore(ConnectionMultiplexer connection, HttpClient client)
{ {
_connection = connection; _connection = connection;
_client = client;
_database = connection.GetDatabase(); _database = connection.GetDatabase();
} }
public async Task<bool> StoreEvent(CompactEvent ev, TimeSpan? expiry = null) public async Task<bool> StoreEvent(CompactEvent ev, TimeSpan? expiry = null)
{ {
if (await _database.SetAsync(EventKey(ev.ToIdentifier()), ev, expiry ?? DefaultExpire)) return true;
{
return await _database.SetAddAsync(UserEventsKey(ev.PubKey.ToHex()), ev.Id);
}
return false;
} }
public async Task<CompactEvent?> GetEvent(NostrIdentifier id) public async Task<CompactEvent?> GetEvent(NostrIdentifier id)
{ {
var ek = EventKey(id); try
var ev = await _database.GetAsync<CompactEvent>(ek);
if (ev != default)
{ {
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<NostrEvent>(await ev.Content.ReadAsStringAsync());
if (obj != default)
{
return CompactEvent.FromNostrEvent(obj);
}
}
}
catch
{
// ignored
} }
return ev; return default;
} }
public async Task<bool> StoreProfile(CompactProfile meta, TimeSpan? expiry = null) public async Task<bool> StoreProfile(CompactProfile meta, TimeSpan? expiry = null)
{ {
var oldProfile = await GetProfile(meta.PubKey.ToHex()); return true;
if ((oldProfile?.Created ?? new DateTime()) < meta.Created)
{
return await _database.SetAsync(ProfileKey(meta.PubKey.ToHex()), meta, expiry ?? DefaultExpire);
}
return false;
} }
public async Task<CompactProfile?> GetProfile(string id) public async Task<CompactProfile?> GetProfile(string id)
{ {
var pk = ProfileKey(id); try
var profile = await _database.GetAsync<CompactProfile>(pk);
if (profile != default)
{ {
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<NostrEvent>(await ev.Content.ReadAsStringAsync());
if (evo != default)
{
return CompactProfile.FromNostrEvent(evo);
}
}
}
catch
{
// ignored
} }
return profile; return default;
} }
public async Task<HashSet<Uri>> GetKnownRelays() public async Task<HashSet<Uri>> GetKnownRelays()
@ -149,6 +161,7 @@ public class RedisStore
{ {
break; break;
} }
var id = ((string)gr.Member!).Split('\x1'); var id = ((string)gr.Member!).Split('\x1');
var u = new Uri(id[0]); var u = new Uri(id[0]);
var info = await GetRelay(u); var info = await GetRelay(u);