improved notification logic, added some pow code but unused now, added large intro print

This commit is contained in:
Vishal 2022-08-30 03:07:32 +05:30
parent e3aae09f9d
commit 1e493147e8
4 changed files with 72 additions and 12 deletions

View File

@ -30,7 +30,7 @@ void printUsage() {
}
Future<void> main(List<String> arguments) async {
printIntro("Nostr");
Logger.root.level = Level.ALL; // defaults to Level.INFO
Logger.root.onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');

View File

@ -31,7 +31,7 @@ Future<void> processNotifications(Tree node) async {
* If replyToId is blank, then it does not reference any e/p tags, and thus becomes a top post
* otherwise e and p tags are found for the given event being replied to, if that event data is available
*/
Future<void> sendReplyPostLike(Tree node, String replyToId, String replyKind, String content) async {
Future<void> sendReplyPostLike(Tree node, String replyToId, String replyKind, String content, [int powLeadingBits = 0]) async {
String strTags = node.getTagStr(replyToId, exename);
if( replyToId.isNotEmpty && strTags == "") { // this returns empty only when the given replyto ID is non-empty, but its not found ( nor is it 64 bytes)
print("${gWarningColor}The given target id was not found and/or is not a valid id. Not sending the event.$colorEndMarker");
@ -40,12 +40,33 @@ Future<void> sendReplyPostLike(Tree node, String replyToId, String replyKind, St
int createdAt = DateTime.now().millisecondsSinceEpoch ~/1000;
String id = getShaId(userPublicKey, createdAt, replyKind, strTags, content);
// generate POW if required
String vanityTag = strTags;
if (powLeadingBits> 0) {
log.info("Starting pow");
for( int i = 0; i < 1000000; i++) {
vanityTag = strTags + ',["nonce","$i"]';
id = getShaId(userPublicKey, createdAt, replyKind, vanityTag, content);
if( id.substring(0, 4) == "000") {
break;
}
}
log.info("Ending pow");
print("Found id: $id");
}
String sig = sign(userPrivateKey, id, "12345612345612345612345612345612");
String toSendMessage = '["EVENT",{"id":"$id","pubkey":"$userPublicKey","created_at":$createdAt,"kind":$replyKind,"tags":[$strTags],"content":"$content","sig":"$sig"}]';
relays.sendRequest(defaultServerUrl, toSendMessage);
String toSendMessage = '["EVENT",{"id":"$id","pubkey":"$userPublicKey","created_at":$createdAt,"kind":$replyKind,"tags":[$vanityTag],"content":"$content","sig":"$sig"}]';
//relays.sendRequest(defaultServerUrl, toSendMessage);
sendRequest( gListRelayUrls, toSendMessage);
}
// is same as above. remove it TODO
Future<void> sendChatMessage(Tree node, String channelId, String messageToSend) async {
String replyKind = "42";
@ -56,7 +77,9 @@ Future<void> sendChatMessage(Tree node, String channelId, String messageToSend)
String sig = sign(userPrivateKey, id, "12345612345612345612345612345612");
String toSendMessage = '["EVENT",{"id":"$id","pubkey":"$userPublicKey","created_at":$createdAt,"kind":$replyKind,"tags":[$strTags],"content":"$messageToSend","sig":"$sig"}]';
relays.sendRequest(defaultServerUrl, toSendMessage);
//relays.sendRequest(defaultServerUrl, toSendMessage);
sendRequest( gListRelayUrls, toSendMessage);
}
// send event e
@ -473,7 +496,7 @@ Future<void> mainMenuUi(Tree node, var contactList) async {
replyKind = "7";
}
await sendReplyPostLike(node, replyToId, replyKind, content);
await sendReplyPostLike(node, replyToId, replyKind, content, 0);
break;
case 3:

View File

@ -201,3 +201,26 @@ Source Code and Binaries: https://github.com/vishalxl/nostr_console
''';
void printIntro(String msg) {
String toPrint =
"""
""";
print("\n$toPrint\n");
}

View File

@ -699,7 +699,7 @@ class Tree {
}
}
// returns true if the treee or its children has a post or like by user; and notification flags are set for such events
// returns true if the treee or its children has a reply or like for the user with public key pk; and notification flags are set for such events
bool hasRepliesAndLikes(String pk) {
//print("----- pk = $pk");
bool hasReaction = false;
@ -710,16 +710,26 @@ class Tree {
if( reactions != null) {
if( reactions.length > 0) {
//print("has reactions");
reactions.forEach((reaction) { e.eventData.newLikes.add(reaction[0]);});
hasReaction = true;
reactions.forEach((reaction) {
// dont add notificatoin for self reaction
Event? reactorEvent = allChildEventsMap[reaction[0]]?.e;
if( reactorEvent != null) {
if( reactorEvent.eventData.pubkey != pk){ // ignore self likes
e.eventData.newLikes.add(reaction[0]);
hasReaction = true;
}
}
});
}
}
}
if( e.eventData.pubkey == pk && children.length > 0) {
for( int i = 0; i < children.length; i++ ) {
// if child is someone else then set notifications and flag
children.forEach((c) { c.e.eventData.isNotification = ((c.e.eventData.pubkey != pk)? true: false) ; childMatches = true; });
children.forEach((child) {
// if child is someone else then set notifications and flag, means there are replies to this event
childMatches = child.e.eventData.isNotification = ((child.e.eventData.pubkey != pk)? true: false) ;
});
}
}
@ -937,16 +947,20 @@ void addMessageToChannel(String channelId, String messageId, var tempChildEvents
room.messageIds.add(messageId);
return;
}
//if(gDebug> 0) print("room has ${room.messageIds.length} messages already. adding new one to it. ");
if(gDebug> 0) print("room has ${room.messageIds.length} messages already. adding new one to it. ");
for(int i = 0; i < room.messageIds.length; i++) {
int eventTime = (tempChildEventsMap[room.messageIds[i]]?.e.eventData.createdAt??0);
if( newEventTime < eventTime) {
// shift current i and rest one to the right, and put event Time here
if(gDebug> 0) print("In addMessageToChannel: inserted in middle to channel ${room.chatRoomId} ");
room.messageIds.insert(i, messageId);
return;
}
}
if(gDebug> 0) print("In addMessageToChannel: added to channel ${room.chatRoomId} ");
// insert at end
room.messageIds.add(messageId);
return;