mirror of
https://github.com/vishalxl/nostr_console.git
synced 2025-04-28 03:46:29 +02:00
removed fetch events option. can reply to posts, and create new post from cmd
This commit is contained in:
parent
b457016a3f
commit
9738244d44
@ -6,7 +6,13 @@ import 'package:args/args.dart';
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'dart:convert'; // for the utf8.encode method
|
||||
|
||||
var userPublickey = "3235036bd0957dfb27ccda02d452d7c763be40c91a1ac082ba6983b25238388c"; // vishalxl
|
||||
// well known disposable test private key
|
||||
const String testPrivateKey = "9d00d99c8dfad84534d3b395280ca3b3e81be5361d69dc0abf8e0fdf5a9d52f9";
|
||||
const String testPublicKey = "e8caa2028a7090ffa85f1afee67451b309ba2f9dee655ec8f7e0a02c29388180";
|
||||
String userPrivateKey = testPrivateKey;
|
||||
String userPublicKey = testPublicKey;
|
||||
|
||||
//var userPublickey = "3235036bd0957dfb27ccda02d452d7c763be40c91a1ac082ba6983b25238388c"; // vishalxl
|
||||
//var userPublickey = "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"; // jb55
|
||||
//var userPublickey = "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"; // fiatjaf
|
||||
//var userPublickey = "ed1d0e1f743a7d19aa2dfb0162df73bacdbc699f67cc55bb91a98c35f7deac69"; // melvin
|
||||
@ -14,7 +20,7 @@ var userPublickey = "3235036bd0957dfb27ccda02d452d7c763be40c91a1ac082ba6983b2
|
||||
|
||||
// program arguments
|
||||
const String requestArg = "request";
|
||||
const String userArg = "user";
|
||||
const String prikeyArg = "prikey";
|
||||
const String lastdaysArg = "days";
|
||||
const String relayArg = "relay";
|
||||
|
||||
@ -22,13 +28,10 @@ const String relayArg = "relay";
|
||||
// this can be changed with 'days' command line argument
|
||||
int numLastDays = 1;
|
||||
|
||||
// well known disposable test private key
|
||||
const String testPrivateKey = "9d00d99c8dfad84534d3b395280ca3b3e81be5361d69dc0abf8e0fdf5a9d52f9";
|
||||
const String testPublicKey = "e8caa2028a7090ffa85f1afee67451b309ba2f9dee655ec8f7e0a02c29388180";
|
||||
|
||||
int showMenu(List<String> menuOptions) {
|
||||
while(true) {
|
||||
print("in showmenu while");
|
||||
//print("in showmenu while");
|
||||
for(int i = 0; i < menuOptions.length;i++) {
|
||||
print(" ${i+1}. ${menuOptions[i]}");
|
||||
}
|
||||
@ -40,6 +43,7 @@ int showMenu(List<String> menuOptions) {
|
||||
if( !int.parse(userOption).isNaN) {
|
||||
int valueOption = int.parse(userOption);
|
||||
if( valueOption < 1 || valueOption > menuOptions.length) {
|
||||
print("Invalid option. Kindly try again.\n");
|
||||
continue;
|
||||
} else {
|
||||
|
||||
@ -51,8 +55,8 @@ int showMenu(List<String> menuOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
String getShaId(String pubkey, int createdAt, String content) {
|
||||
String buf = '[0,"$pubkey",$createdAt,1,[],"$content"]';
|
||||
String getShaId(String pubkey, int createdAt, String strTags, String content) {
|
||||
String buf = '[0,"$pubkey",$createdAt,1,[$strTags],"$content"]';
|
||||
var bufInBytes = utf8.encode(buf);
|
||||
var value = sha256.convert(bufInBytes);
|
||||
String id = value.toString();
|
||||
@ -64,18 +68,12 @@ Future<void> terminalMenuUi(Tree node, var contactList) async {
|
||||
|
||||
bool userContinue = true;
|
||||
while(userContinue) {
|
||||
//relays.printStatus();
|
||||
|
||||
print('\n\nPick an option by typing the correspoinding\nnumber and then pressing <enter>:');
|
||||
int option = showMenu(['Display events', // 1
|
||||
'Get Latest events', // 2
|
||||
'Post/Reply', // 3
|
||||
'Exit']); // 4
|
||||
|
||||
|
||||
'Post/Reply', // 2
|
||||
'Exit']); // 3
|
||||
print('You picked: $option');
|
||||
|
||||
|
||||
switch(option) {
|
||||
case 1:
|
||||
//print("in display events option");
|
||||
@ -83,29 +81,6 @@ Future<void> terminalMenuUi(Tree node, var contactList) async {
|
||||
break;
|
||||
|
||||
case 2:
|
||||
const int n = 4;
|
||||
print("Going to get latest events...");
|
||||
//relays.clearHistory();
|
||||
//getUserEvents(defaultServerUrl, userPublickey, events, 3000, latestReceivedSeconds);
|
||||
Future.delayed(const Duration(seconds: n), () {
|
||||
print("Number of new events = ${getRecievedEvents().length}");
|
||||
});
|
||||
|
||||
|
||||
Future<void> foo() async {
|
||||
//print('foo started');
|
||||
await Future.delayed(Duration(seconds: n+1));
|
||||
//print('foo executed');
|
||||
return;
|
||||
}
|
||||
|
||||
await foo();
|
||||
node.insertEvents(getRecievedEvents());
|
||||
clearEvents();
|
||||
|
||||
break;
|
||||
|
||||
case 3:
|
||||
print("Type comment to post/reply: ");
|
||||
String? $contentVar = stdin.readLineSync();
|
||||
String content = $contentVar??"";
|
||||
@ -113,39 +88,64 @@ Future<void> terminalMenuUi(Tree node, var contactList) async {
|
||||
break;
|
||||
}
|
||||
|
||||
print("Type id of event to reply to ( leave blank if you want to make a post): ");
|
||||
String? $replyToVar = stdin.readLineSync();
|
||||
String replyToId = $replyToVar??"";
|
||||
String strTags = node.getTagsFromEvent(replyToId);
|
||||
|
||||
int createdAt = DateTime.now().millisecondsSinceEpoch ~/1000;
|
||||
String id = getShaId(testPublicKey, createdAt, content);
|
||||
String sig = sign(testPrivateKey, id, "12345612345612345612345612345612");
|
||||
|
||||
String id = getShaId(userPublicKey, createdAt, strTags, content);
|
||||
String sig = sign(userPrivateKey, id, "12345612345612345612345612345612");
|
||||
|
||||
//print("sig = $sig");
|
||||
//{"id": "da2a1321c9d2c8d53aa962f1ce83bbeaf00be0bf38e359a858ef269c17490e60","pubkey": "e8caa2028a7090ffa85f1afee67451b309ba2f9dee655ec8f7e0a02c29388180",
|
||||
//"created_at": 1660244881,"kind": 1,"tags": [],"content": "test12","sig": "8d36571b0dacdadca1e9b3373c16050a0dc6abf25ec5e6cf9a9075f4877e2a8ca8012e9fcd168a7ff1a631386979282c87b139dec912def9f5764bc7f8cfc7cb"}
|
||||
String finalMessage = '["EVENT", {"id": "$id","pubkey": "$testPublicKey","created_at": $createdAt,"kind": 1,"tags": [],"content": "$content","sig": "$sig"}]';
|
||||
String finalMessage = '["EVENT", {"id": "$id","pubkey": "$userPublicKey","created_at": $createdAt,"kind": 1,"tags": [$strTags],"content": "$content","sig": "$sig"}]';
|
||||
relays.sendMessage(finalMessage, defaultServerUrl);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
case 3:
|
||||
default:
|
||||
userContinue = false;
|
||||
//print("number of user events : $numUserEvents");
|
||||
//print("number of feed events : $numFeedEvents");
|
||||
//print("number of other events : $numOtherEvents");
|
||||
|
||||
String authorName = getAuthorName(userPublickey);
|
||||
print("\nFinished fetching feed for user $userPublickey ($authorName), whose contact list has ${contactList.length} profiles.\n ");
|
||||
String authorName = getAuthorName(userPublicKey);
|
||||
print("\nFinished fetching feed for user $userPublicKey ($authorName), whose contact list has ${contactList.length} profiles.\n ");
|
||||
contactList.forEach((x) => stdout.write("${getAuthorName(x)}, "));
|
||||
stdout.write("\n");
|
||||
|
||||
print("public key generated = ${getPublicKey(testPrivateKey)}");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
const int waitMilliSeconds = 300;
|
||||
//print("Going to get latest events...");
|
||||
Future.delayed(const Duration(milliseconds: waitMilliSeconds), () {
|
||||
print("\nNumber of new events = ${getRecievedEvents().length}");
|
||||
//print("updating event tree...");
|
||||
node.insertEvents(getRecievedEvents());
|
||||
clearEvents();
|
||||
});
|
||||
|
||||
Future<void> foo() async {
|
||||
//print('foo started');
|
||||
await Future.delayed(Duration(milliseconds: waitMilliSeconds + 100));
|
||||
//print('foo executed');
|
||||
return;
|
||||
}
|
||||
|
||||
await foo();
|
||||
|
||||
} // end while
|
||||
}
|
||||
|
||||
Future<void> main(List<String> arguments) async {
|
||||
|
||||
final parser = ArgParser()..addOption(requestArg, abbr: 'q')
|
||||
..addOption(userArg, abbr:"u")
|
||||
..addOption(prikeyArg, abbr:"p")
|
||||
..addOption(lastdaysArg, abbr:"d")
|
||||
..addOption(relayArg, abbr:"r");
|
||||
ArgResults argResults = parser.parse(arguments);
|
||||
@ -155,6 +155,16 @@ Future<void> main(List<String> arguments) async {
|
||||
print("Going to use relay: $defaultServerUrl");
|
||||
}
|
||||
|
||||
if( argResults[prikeyArg] != null) {
|
||||
userPrivateKey = argResults[prikeyArg];
|
||||
userPublicKey = getPublicKey(userPrivateKey);
|
||||
}
|
||||
if( argResults[lastdaysArg] != null) {
|
||||
|
||||
numLastDays = int.parse(argResults[lastdaysArg]);
|
||||
print("Going to show posts for last $numLastDays days");
|
||||
}
|
||||
|
||||
if( argResults[requestArg] != null) {
|
||||
stdout.write("Got argument request ${argResults[requestArg]}");
|
||||
sendRequest("wss://nostr-pub.wellorder.net", argResults[requestArg]);
|
||||
@ -169,20 +179,11 @@ Future<void> main(List<String> arguments) async {
|
||||
return;
|
||||
}
|
||||
|
||||
if( argResults[userArg] != null) {
|
||||
userPublickey = argResults[userArg];
|
||||
}
|
||||
if( argResults[lastdaysArg] != null) {
|
||||
|
||||
numLastDays = int.parse(argResults[lastdaysArg]);
|
||||
print("Going to show posts for last $numLastDays days");
|
||||
}
|
||||
|
||||
// the default in case no arguments are given is:
|
||||
// get a user's events, then from its type 3 event, gets events of its follows,
|
||||
// then get the events of user-id's mentioned in p-tags of received events
|
||||
// then display them all
|
||||
getUserEvents(defaultServerUrl, userPublickey, 1000, 0);
|
||||
getUserEvents(defaultServerUrl, userPublicKey, 1000, 0);
|
||||
|
||||
int numUserEvents = 0, numFeedEvents = 0, numOtherEvents = 0;
|
||||
|
||||
|
@ -179,10 +179,8 @@ class EventData {
|
||||
|
||||
return EventData(json['id'] as String, json['pubkey'] as String,
|
||||
json['created_at'] as int, json['kind'] as int,
|
||||
json['content'] as String,
|
||||
eTagsRead, pTagsRead,
|
||||
contactList,
|
||||
tagsRead);
|
||||
json['content'] as String, eTagsRead, pTagsRead,
|
||||
contactList, tagsRead);
|
||||
}
|
||||
|
||||
String expandMentions(String content) {
|
||||
@ -211,13 +209,13 @@ class EventData {
|
||||
content = "${content.substring(0, index)} @$author${content.substring(index + 4)}";
|
||||
}
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
|
||||
void printEventData(int depth) {
|
||||
String max3(String v) => v.length > 3? v.substring(0,3) : v.substring(0, v.length);
|
||||
int n = 5;
|
||||
String maxN(String v) => v.length > n? v.substring(0,n) : v.substring(0, v.length);
|
||||
void printGreen(String s) => stdout.supportsAnsiEscapes ?stdout.write("\x1B[32m$s\x1B[0m"):stdout.write(s);
|
||||
|
||||
DateTime dTime = DateTime.fromMillisecondsSinceEpoch(createdAt *1000);
|
||||
@ -238,12 +236,10 @@ class EventData {
|
||||
stdout.write("+-------+\n");
|
||||
printDepth(depth);
|
||||
String name = getAuthorName(pubkey);
|
||||
stdout.write("|Author : $name id: ${max3(id)} Time: $strDate\n");
|
||||
stdout.write("|Author : $name id: ${maxN(id)} Time: $strDate\n");
|
||||
printDepth(depth);
|
||||
stdout.write("|Message: ");
|
||||
printGreen(contentShifted);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
@ -335,7 +331,10 @@ class Tree {
|
||||
List<String> newEventsId = [];
|
||||
newEvents.forEach((element) {
|
||||
if( allEvents[element.eventData.id] != null) {
|
||||
return; // return if the event is already there in the map
|
||||
return; // don't process if the event is already present in the map
|
||||
}
|
||||
if( element.eventData.kind != 1) {
|
||||
return; // only kind 1 events are added to the tree
|
||||
}
|
||||
allEvents[element.eventData.id] = Tree(element, [], {});
|
||||
newEventsId.add(element.eventData.id);
|
||||
@ -397,6 +396,22 @@ class Tree {
|
||||
children[i].printTree(depth+1, false, newerThan);
|
||||
}
|
||||
}
|
||||
|
||||
String getTagsFromEvent(String replyToId) {
|
||||
|
||||
String strTags = "";
|
||||
if( replyToId == "") {
|
||||
return strTags;
|
||||
}
|
||||
for( String k in allEvents.keys) {
|
||||
if( k.substring(0, replyToId.length) == replyToId) {
|
||||
strTags = '["e","$k"]';
|
||||
break;
|
||||
}
|
||||
}
|
||||
return strTags;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
List<String> getpTags(List<Event> events) {
|
||||
|
@ -35,7 +35,7 @@ String getMultiUserRequest(List<String> publicKeys, int numUserEvents) {
|
||||
*/
|
||||
class Relays {
|
||||
Map<String, IOWebSocketChannel > relays;
|
||||
List<String> users; // is used to that duplicate requests aren't sent for same user
|
||||
List<String> users; // is used so that duplicate requests aren't sent for same user
|
||||
List<Event> rEvents = [];
|
||||
|
||||
Relays(this.relays, this.users, this.rEvents);
|
||||
@ -43,7 +43,7 @@ class Relays {
|
||||
factory Relays.relay(String relay) {
|
||||
IOWebSocketChannel fws = IOWebSocketChannel.connect(relay);
|
||||
print('In Relay.relay: connecting to relay $relay');
|
||||
Map<String, IOWebSocketChannel> r = Map();
|
||||
Map<String, IOWebSocketChannel> r = {};
|
||||
r[relay] = fws;
|
||||
return Relays(r, [], []);
|
||||
}
|
||||
@ -135,7 +135,7 @@ class Relays {
|
||||
}
|
||||
}
|
||||
|
||||
print('sending request: $request to $relay\n');
|
||||
//print('sending request: $request to $relay\n');
|
||||
fws?.sink.add(request);
|
||||
}
|
||||
|
||||
@ -184,11 +184,6 @@ class Relays {
|
||||
print( reason??"reason not found");
|
||||
});
|
||||
}
|
||||
|
||||
void clearHistory() {
|
||||
users = [];
|
||||
relays = {};
|
||||
}
|
||||
}
|
||||
|
||||
Relays relays = Relays({}, [], []);
|
||||
@ -202,9 +197,9 @@ List<String> getContactFeed(List<Contact> contacts, numEventsToGet) {
|
||||
} else {
|
||||
mContacts[contacts[i].relay] = [contacts[i].id];
|
||||
}
|
||||
if( contacts[i].id == "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245") {
|
||||
/*if( contacts[i].id == "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245") {
|
||||
print("In getContactFeed: sending request for jb55 32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245");
|
||||
}
|
||||
}*/
|
||||
contactList.add(contacts[i].id);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user