Use rust nostr-services for events/profiles
This commit is contained in:
parent
4e89ba6775
commit
b72957fc4b
@ -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<bool> 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<CompactEvent?> GetEvent(NostrIdentifier id)
|
||||
{
|
||||
var ek = EventKey(id);
|
||||
var ev = await _database.GetAsync<CompactEvent>(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<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)
|
||||
{
|
||||
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<CompactProfile?> GetProfile(string id)
|
||||
{
|
||||
var pk = ProfileKey(id);
|
||||
var profile = await _database.GetAsync<CompactProfile>(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<NostrEvent>(await ev.Content.ReadAsStringAsync());
|
||||
if (evo != default)
|
||||
{
|
||||
return CompactProfile.FromNostrEvent(evo);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
return profile;
|
||||
return default;
|
||||
}
|
||||
|
||||
public async Task<HashSet<Uri>> 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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user