- Use new logger

This commit is contained in:
Tristan B. Velloza Kildaire 2023-03-02 16:41:29 +02:00
parent 9fa2a9b1d7
commit 22c2276a70
7 changed files with 159 additions and 66 deletions

View File

@ -4,9 +4,8 @@
],
"copyright": "Copyright © 2023, Tristan B. Velloza Kildaire",
"dependencies": {
"dlog": "~>0.0.6",
"gogga": "~>1.0.0",
"vibe-d": "~>0.9.5"
"gogga": "2.1.6",
"vibe-d": "0.9.6-beta.1"
},
"description": "A D nostr client library",
"license": "LGPL v3.0",

View File

@ -4,13 +4,7 @@ import std.stdio;
import vibe.d;
import std.json;
import dnostr.logging;
/**
* FIXME: Fix the below so I need not import gogga too
*/
mixin LoggerSetup!();
import gogga;
import dnostr.logging;
import dnostr.messages;
@ -57,11 +51,11 @@ void main()
NostrRelay damusRelay = new NostrRelay("https://relay.damus.io/");
NostrClient client = new NostrClient([relay1]);
NostrClient client = new NostrClient([relay1, relay2]);
client.goOnline();
NostrEvent nostrPost = new NostrEvent("TODO BRUH PUBKEY");
NostrEvent nostrPost = new NostrEvent("TODO BRUH PUBKEY", "This is my post's body!");
client.event(nostrPost);
@ -79,7 +73,7 @@ public string calculateID(JSONValue jsonIn)
serializedJSON ~= JSONValue(0);
logger.print("Comptued ID of post is: "~id~"\n", DebugType.INFO);
logger.info("Computed ID of post is: ", id);
return id;

View File

@ -1,25 +1,17 @@
module dnostr.client;
import dnostr.logging;
/**
* FIXME: Fix the below so I need not import gogga too
*/
mixin LoggerSetup!();
import gogga;
import dnostr.logging;
import dnostr.relays : NostrRelay;
import core.sync.mutex : Mutex;
import dnostr.messages;
public class NostrClient
{
private NostrRelay[] relays;
private Mutex relaysLock;
private string myPublicKey = "MY public key (TODO)";
this(NostrRelay[] relays)
{
relaysLock = new Mutex();
this.relays = relays;
}
@ -28,21 +20,16 @@ public class NostrClient
// TODO: mutex-based lock and add to `relays`
}
/**
* Go online with all relays
*/
public final void goOnline()
{
// Ensure all NostrRelays are online
/* Lock the relay list */
relaysLock.lock();
/* Start each relay */
foreach(NostrRelay relay; relays)
{
relay.call();
}
/* Unlock the relay list */
relaysLock.unlock();
}
public final void goOffline()
@ -52,19 +39,14 @@ public class NostrClient
public void event(NostrEvent post)
{
/* Lock the relay list */
relaysLock.lock();
/* Announce the event to each relay */
foreach(NostrRelay relay; relays)
{
// TODO: Checking is relay even tpost worked, oif not, queue to disk or something for a later post (if enabled)
import std.stdio;
relay.event(post);
}
/* Unlock the relay list */
relaysLock.unlock();
logger.dbg("Done?");
}
}

View File

@ -1,6 +1,7 @@
module dnostr.listener;
import dnostr.messages;
import dnostr.logging;
/**
* NostrListener

View File

@ -1,12 +1,14 @@
module dnostr.logging;
mixin template LoggerSetup()
{
import gogga;
import gogga;
// TODO: Investigate if we need the below (I copied it from Birchwood)
__gshared GoggaLogger logger;
__gshared static this()
// NOTE: If we include threads then use `__gshared` so we have
// ... one logger for the whole threadgroup and not a per-TLS
// ... (per-thread) logger (as we do below)
private mixin template LoggerSetup()
{
GoggaLogger logger;
static this()
{
logger = new GoggaLogger();
@ -14,5 +16,10 @@ mixin template LoggerSetup()
{
logger.enableDebug();
}
logger.mode(GoggaMode.RUSTACEAN);
}
}
}
/* Where you import, setup logging */
mixin LoggerSetup!();

View File

@ -1,5 +1,9 @@
module dnostr.messages;
import std.conv : to;
import std.string : cmp, toLower;
import dnostr.logging;
import std.json;
public abstract class NostrMessage
@ -8,35 +12,154 @@ public abstract class NostrMessage
public abstract string encode();
}
public class Tag
{
}
// TODO: Move the generic stuff to the above
public class NostrEvent : NostrMessage
{
private string publicKey;
private string content;
private Tag[] tags; // TODO: See what to do with this
private int kind; // TODO: See what to do with this
this(string publicKey)
this(string publicKey, string content, Tag[] tags = null)
{
this.publicKey = publicKey;
this.content = content;
this.tags = tags;
}
public final string generateID()
private final void generateID(ref JSONValue inJSON)
{
JSONValue jsonSerialized;
JSONValue[] jsonSerialized;
// TODO: Generate the ID
// TODO: Update JSON
// TODO: List thing
// Add `0` as the ID (as per NIP-01)
jsonSerialized ~= JSONValue(0);
return "generatedID (TODO)";
JSONValue[string] kvPair = inJSON.object();
logger.dbg("kvPair: ", kvPair);
/**
* [
* 0,
* <pubkey, as a (lowercase) hex string>,
* <created_at, as a number>,
* <kind, as a number>,
* <tags, as an array of arrays of non-null strings>,
* <content, as a string>
* ]
*/
// As per NIP-01 put 0 here
jsonSerialized ~= JSONValue(0);
// Place public key
jsonSerialized ~= inJSON["pubkey"];
// Place creation time
jsonSerialized ~= inJSON["created_at"];
// Place kind
jsonSerialized ~= inJSON["kind"];
// Place the tags (if any)
if("tags" in kvPair)
{
jsonSerialized ~= inJSON["tags"];
}
// Place the content
jsonSerialized ~= inJSON["content"];
JSONValue finalSerialized = JSONValue(jsonSerialized);
logger.dbg("The JSON to BE serialized is:\n"~finalSerialized.toString());
// Perform the sha on the string of the structure
string finalSerializedString = finalSerialized.toString();
import std.digest.sha : sha256Of;
ubyte[] hashBytes = sha256Of!(string)(finalSerializedString);
import std.digest : toHexString;
string hashString = toHexString(hashBytes).toLower();
logger.dbg("The generated hash for serialization is \""~hashString~"\"");
// Now set the "id" field
inJSON["id"] = hashString;
}
private JSONValue generateJSON()
{
// Generated JSON
JSONValue generated;
// Acquire the current UNIX time in seconds
// and store this
import std.datetime.systime : SysTime, Clock;
SysTime creationTime = Clock.currTime();
generated["created_at"] = creationTime.toUnixTime();
// Set the public key
generated["pubkey"] = publicKey;
// Set the content
generated["content"] = content;
// Store the tags (if any)
if(tags.length)
{
JSONValue[] tagList;
// Store the list of tags into "tags"
generated["tags"] = tagList;
}
// Create an "id" field
generated["id"] = "";
// Create the kind field
generated["kind"] = kind;
return generated;
}
public override string encode()
{
// TODO: Add generateID call and switch out ID
// Generate the JSON
JSONValue generatedJSON = generateJSON();
return "muh post (TODO)";
// TODO: Generated the ID and set it for is
generateID(generatedJSON);
// TODO: Update the below string to be the thing above
string finalStr = generatedJSON.toString();
return finalStr;
}
public override JSONValue[] serialize()
{
JSONValue[] items;
/* Message types */
items ~= JSONValue("EVENT");

View File

@ -3,13 +3,7 @@ module dnostr.relays;
import std.json;
import vibe.vibe : WebSocket, connectWebSocket, WebSocketException;
import vibe.vibe : HTTPClientSettings, URL;
import dnostr.logging;
/**
* FIXME: Fix the below so I need not import gogga too
*/
mixin LoggerSetup!();
import gogga;
import dnostr.logging;
import core.thread : dur, Duration;
import core.thread.fiber : Fiber;
@ -17,14 +11,6 @@ import std.conv : to;
import core.sync.mutex : Mutex;
import dnostr.messages;
// TODO: Investigate if we need the belowe (I copied it from Birchwood)
__gshared GoggaLogger logger;
__gshared static this()
{
logger = new GoggaLogger();
}
public class NostrRelay : Fiber
{
/**
@ -76,7 +62,7 @@ public class NostrRelay : Fiber
}
catch(WebSocketException e)
{
logger.print("There was an error creating a web socket\n", DebugType.ERROR);
logger.error("There was an error creating a web socket");
}
}
@ -98,7 +84,7 @@ public class NostrRelay : Fiber
}
catch(JSONException e)
{
logger.print("Error parsing JSON received from the relay\n", DebugType.ERROR);
logger.error("Error parsing JSON received from the relay");
}
}
@ -131,5 +117,6 @@ public class NostrRelay : Fiber
ensureConnection();
// TODO: Implement me
ws.send(g.encode());
}
}