From 69223d407d1fbff1897415935e2c2a2b9485ee9d Mon Sep 17 00:00:00 2001 From: Vishal <64505169+vishalxl@users.noreply.github.com> Date: Thu, 1 Sep 2022 05:28:43 +0530 Subject: [PATCH] handled received delete kind 5 event so that the original message is not printed. If the original message was in file, its still not deleted. --- lib/event_ds.dart | 23 ++++++++++++++++------- lib/settings.dart | 4 ++++ lib/tree_ds.dart | 47 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/lib/event_ds.dart b/lib/event_ds.dart index 0df4f64..24e95d1 100644 --- a/lib/event_ds.dart +++ b/lib/event_ds.dart @@ -182,6 +182,15 @@ String getShaId(String pubkey, int createdAt, String kind, String strTags, Strin return value.toString(); } +// get printable date from seconds since epoch +String getPrintableDate(int createdAt) { + final df1 = DateFormat('hh:mm a'); + final df2 = DateFormat(DateFormat.ABBR_MONTH_DAY); + String strDate = df1.format(DateTime.fromMillisecondsSinceEpoch(createdAt*1000)); + strDate += " ${df2.format(DateTime.fromMillisecondsSinceEpoch(createdAt*1000))}"; + return strDate; +} + class EventData { String id; String pubkey; @@ -195,9 +204,12 @@ class EventData { String evaluatedContent; // content which has mentions expanded, and which has been translated Set newLikes; // + + List contactList = []; // used for kind:3 events, which is contact list event bool isHidden; // hidden by sending a reaction kind 7 event to this event, by the logged in user + bool isDeleted; // deleted by kind 5 event String getParent() { if( eTagsRest.isNotEmpty) { @@ -207,7 +219,7 @@ class EventData { } EventData(this.id, this.pubkey, this.createdAt, this.kind, this.content, this.eTagsRest, this.pTags, - this.contactList, this.tags, this.newLikes, {this.isNotification = false, this.evaluatedContent = "", this.isHidden = false}); + this.contactList, this.tags, this.newLikes, {this.isNotification = false, this.evaluatedContent = "", this.isHidden = false, this.isDeleted = false}); factory EventData.fromJson(dynamic json) { List contactList = []; @@ -242,7 +254,8 @@ class EventData { } } } else { - if ( json['kind'] == 1 || json['kind'] == 7 || json['kind'] == 42 ) { + int eKind = json['kind']; + if ( eKind == 1 || eKind == 7 || eKind == 42 || eKind == 5) { for( int i = 0; i < numTags; i++) { var tag = jsonTags[i]; //stdout.write(tag); @@ -359,11 +372,7 @@ class EventData { String maxN(String v) => v.length > n? v.substring(0,n) : v.substring(0, v.length); void printInColor(String s, String commentColor) => stdout.supportsAnsiEscapes ?stdout.write("$commentColor$s$gColorEndMarker"):stdout.write(s); - // TODO do it in one call - final df1 = DateFormat('hh:mm a'); - final df2 = DateFormat(DateFormat.ABBR_MONTH_DAY); - String strDate = df1.format(DateTime.fromMillisecondsSinceEpoch(createdAt*1000)); - strDate += " ${df2.format(DateTime.fromMillisecondsSinceEpoch(createdAt*1000))}"; + String strDate = getPrintableDate(createdAt); if( createdAt == 0) { print("debug: createdAt == 0 for event $content"); } diff --git a/lib/settings.dart b/lib/settings.dart index 47048b7..fb06200 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -115,6 +115,10 @@ int gDifficulty = 0; const int gNumChannelMessagesToShow = 15; const int gMaxChannelPagesDisplayed = 50; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////// User interface messages +String gDeletedEventMessage = "This post was deleted by its original writer"; + const String gUsage = """$exename version $version The nostr console client built using dart. diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index 3264759..24d60fe 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -275,7 +275,7 @@ class Store { }); } - static const Set typesInEventMap = {0, 1, 3, 7, 40, 42}; // 0 meta, 1 post, 3 follows list, 7 reactions + static const Set typesInEventMap = {0, 1, 3, 5, 7, 40, 42}; // 0 meta, 1 post, 3 follows list, 7 reactions static void handleChannelEvents( Map rooms, Map tempChildEventsMap, Event ce) { String eId = ce.eventData.id; @@ -353,6 +353,8 @@ class Store { } }); + processDeleteEvents(tempChildEventsMap); // handle returned values perhaps later + // once tempChildEventsMap has been created, create connections between them so we get a tree structure from all these events. List topLevelTrees = [];// this will become the children of the main top node. These are events without parents, which are printed at top. List tempWithoutParent = []; @@ -445,7 +447,7 @@ class Store { } } - // only kind 0, 1, 3, 7, 40, 42 events are added to map, return otherwise + // only kind 0, 1, 3, 5( delete), 7, 40, 42 events are added to map, return otherwise if( !typesInEventMap.contains(newEvent.eventData.kind) ) { return; } @@ -863,18 +865,6 @@ class Store { return strTags; } - -/* - void addChild(Event child) { - Store node; - node = Store(child, [], {}, [], false, {}, {}); - children.add(node); - } - - void addChildNode(Store node) { - children.add(node); - } -*/ // for any tree node, returns its top most parent Tree getTopTree(Tree tree) { while( true) { @@ -965,6 +955,35 @@ class Store { return totalEvents; } + static List processDeleteEvents(Map tempChildEventsMap) { + List deletedEventIds = []; + tempChildEventsMap.forEach((key, tree) { + EventData deleterEvent = tree.event.eventData; + if( deleterEvent.kind == 5) { + deleterEvent.tags.forEach((tag) { + if( tag.length < 2) { + return; + } + if( tag[0] == "e") { + String deletedEventId = tag[1]; + // look up that event and ensure its kind 1 etc, and then mark it deleted. + Event? deletedEvent = tempChildEventsMap[deletedEventId]?.event; + if( deletedEvent != null) { + if( deletedEvent.eventData.kind == 1) { + deletedEvent.eventData.isDeleted = true; + deletedEvent.eventData.content = gDeletedEventMessage + " on ${getPrintableDate(deleterEvent.createdAt)}"; + deletedEvent.eventData.evaluatedContent = ""; + EventData ed = deletedEvent.eventData; + deletedEvent.originalJson = '["EVENT","none",{"id":${ed.id},"pubkey":${ed.pubkey},"createdAt":${ed.createdAt},"kind":1,"tags":[],"sig":"invalid","comment":"deleted"}]'; + deletedEventIds.add(deletedEventId); + } + } + } + }); + } + }); + return deletedEventIds; + } // end processDeleteEvents } // end Store void addMessageToChannel(String channelId, String messageId, Map tempChildEventsMap, var chatRooms) {