Compare commits

...

2 Commits

Author SHA1 Message Date
b72957fc4b
Use rust nostr-services for events/profiles
All checks were successful
continuous-integration/drone/push Build is passing
2024-09-05 10:11:20 +01:00
4e89ba6775
Use new API for profiles/events 2024-09-05 10:11:20 +01:00
2 changed files with 39 additions and 26 deletions

View File

@ -18,12 +18,12 @@ public class NostrServicesClient
public async Task<CompactProfile?> Profile(string id)
{
return await Get<CompactProfile>(HttpMethod.Get, $"/api/v1/export/profile/{id}");
return await Get<CompactProfile>(HttpMethod.Get, $"https://nostr-rs.api.v0l.io/event/0/{id}");
}
public async Task<NostrEvent?> Event(string id)
{
return await Get<NostrEvent>(HttpMethod.Get, $"/api/v1/export/{id}");
return await Get<NostrEvent>(HttpMethod.Get, $"https://nostr-rs.api.v0l.io/event/{id}");
}
public async Task<LinkPreviewData?> LinkPreview(string url)

View File

@ -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);