From 0801bfec08c52d43070faef2aeb276176cf04771 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Fri, 8 Nov 2019 06:46:03 +0000 Subject: [PATCH] add optinal reason for trusting/distrusting --- .../com/muwire/core/trust/TrustEvent.groovy | 1 + .../com/muwire/core/trust/TrustService.groovy | 67 +++++++++++++++---- .../muwire/gui/ContentPanelController.groovy | 7 +- .../com/muwire/gui/MainFrameController.groovy | 8 ++- .../com/muwire/gui/SearchTabController.groovy | 7 +- .../views/com/muwire/gui/MainFrameView.groovy | 6 +- 6 files changed, 75 insertions(+), 21 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy b/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy index 890a6163..a82caf98 100644 --- a/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy @@ -7,4 +7,5 @@ class TrustEvent extends Event { Persona persona TrustLevel level + String reason } diff --git a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy index 9d19b231..2f894451 100644 --- a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy +++ b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy @@ -1,21 +1,26 @@ package com.muwire.core.trust import java.util.concurrent.ConcurrentHashMap +import java.util.logging.Level import com.muwire.core.Persona import com.muwire.core.Service +import groovy.json.JsonOutput +import groovy.json.JsonSlurper +import groovy.util.logging.Log import net.i2p.data.Base64 import net.i2p.data.Destination import net.i2p.util.ConcurrentHashSet +@Log class TrustService extends Service { final File persistGood, persistBad final long persistInterval - final Map good = new ConcurrentHashMap<>() - final Map bad = new ConcurrentHashMap<>() + final Map good = new ConcurrentHashMap<>() + final Map bad = new ConcurrentHashMap<>() final Timer timer @@ -37,18 +42,41 @@ class TrustService extends Service { } void load() { + JsonSlurper slurper = new JsonSlurper() if (persistGood.exists()) { persistGood.eachLine { - byte [] decoded = Base64.decode(it) - Persona persona = new Persona(new ByteArrayInputStream(decoded)) - good.put(persona.destination, persona) + try { + byte [] decoded = Base64.decode(it) + Persona persona = new Persona(new ByteArrayInputStream(decoded)) + good.put(persona.destination, new TrustEntry(persona, null)) + } catch (Exception e) { + try { + def json = slurper.parseText(it) + byte [] decoded = Base64.decode(json.persona) + Persona persona = new Persona(new ByteArrayInputStream(decoded)) + good.put(persona.destination, new TrustEntry(persona, json.reason)) + } catch (Exception bad) { + log.log(Level.WARNING,"couldn't parse trust entry $it",bad) + } + } } } if (persistBad.exists()) { persistBad.eachLine { - byte [] decoded = Base64.decode(it) - Persona persona = new Persona(new ByteArrayInputStream(decoded)) - bad.put(persona.destination, persona) + try { + byte [] decoded = Base64.decode(it) + Persona persona = new Persona(new ByteArrayInputStream(decoded)) + bad.put(persona.destination, new TrustEntry(persona, null)) + } catch (Exception e) { + try { + def json = slurper.parseText(it) + byte [] decoded = Base64.decode(json.persona) + Persona persona = new Persona(new ByteArrayInputStream(decoded)) + bad.put(persona.destination, new TrustEntry(persona, json.reason)) + } catch (Exception bad) { + log.log(Level.WARNING,"couldn't parse trust entry $it",bad) + } + } } } timer.schedule({persist()} as TimerTask, persistInterval, persistInterval) @@ -59,13 +87,19 @@ class TrustService extends Service { persistGood.delete() persistGood.withPrintWriter { writer -> good.each {k,v -> - writer.println v.toBase64() + def json = [:] + json.persona = v.persona.toBase64() + json.reason = v.reason + writer.println JsonOutput.toJson(json) } } persistBad.delete() persistBad.withPrintWriter { writer -> bad.each { k,v -> - writer.println v.toBase64() + def json = [:] + json.persona = v.persona.toBase64() + json.reason = v.reason + writer.println JsonOutput.toJson(json) } } } @@ -82,11 +116,11 @@ class TrustService extends Service { switch(e.level) { case TrustLevel.TRUSTED: bad.remove(e.persona.destination) - good.put(e.persona.destination, e.persona) + good.put(e.persona.destination, new TrustEntry(e.persona, e.reason)) break case TrustLevel.DISTRUSTED: good.remove(e.persona.destination) - bad.put(e.persona.destination, e.persona) + bad.put(e.persona.destination, new TrustEntry(e.persona, e.reason)) break case TrustLevel.NEUTRAL: good.remove(e.persona.destination) @@ -94,4 +128,13 @@ class TrustService extends Service { break } } + + public static class TrustEntry { + private final Persona persona + private final String reason + TrustEntry(Persona persona, String reason) { + this.persona = persona + this.reason = reason + } + } } diff --git a/gui/griffon-app/controllers/com/muwire/gui/ContentPanelController.groovy b/gui/griffon-app/controllers/com/muwire/gui/ContentPanelController.groovy index 2b5ff9d8..5654e809 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/ContentPanelController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/ContentPanelController.groovy @@ -5,6 +5,7 @@ import griffon.core.controller.ControllerAction import griffon.inject.MVCMember import griffon.metadata.ArtifactProviderFor import javax.annotation.Nonnull +import javax.swing.JOptionPane import com.muwire.core.Core import com.muwire.core.EventBus @@ -83,8 +84,9 @@ class ContentPanelController { int selectedHit = view.getSelectedHit() if (selectedHit < 0) return + String reason = JOptionPane.showInputDialog("Enter reason (optional)") Match m = model.hits[selectedHit] - core.eventBus.publish(new TrustEvent(persona : m.persona, level : TrustLevel.TRUSTED)) + core.eventBus.publish(new TrustEvent(persona : m.persona, level : TrustLevel.TRUSTED, reason : reason)) } @ControllerAction @@ -92,8 +94,9 @@ class ContentPanelController { int selectedHit = view.getSelectedHit() if (selectedHit < 0) return + String reason = JOptionPane.showInputDialog("Enter reason (optional)") Match m = model.hits[selectedHit] - core.eventBus.publish(new TrustEvent(persona : m.persona, level : TrustLevel.DISTRUSTED)) + core.eventBus.publish(new TrustEvent(persona : m.persona, level : TrustLevel.DISTRUSTED, reason : reason)) } void saveMuWireSettings() { diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index 027e3832..aac1fc09 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -160,8 +160,9 @@ class MainFrameController { int selected = builder.getVariable("searches-table").getSelectedRow() if (selected < 0) return + String reason = JOptionPane.showInputDialog("Enter reason (optional)") Persona p = model.searches[selected].originator - core.eventBus.publish( new TrustEvent(persona : p, level : TrustLevel.TRUSTED) ) + core.eventBus.publish( new TrustEvent(persona : p, level : TrustLevel.TRUSTED, reason : reason) ) } @ControllerAction @@ -169,8 +170,9 @@ class MainFrameController { int selected = builder.getVariable("searches-table").getSelectedRow() if (selected < 0) return + String reason = JOptionPane.showInputDialog("Enter reason (optional)") Persona p = model.searches[selected].originator - core.eventBus.publish( new TrustEvent(persona : p, level : TrustLevel.DISTRUSTED) ) + core.eventBus.publish( new TrustEvent(persona : p, level : TrustLevel.DISTRUSTED, reason : reason) ) } @ControllerAction @@ -217,7 +219,7 @@ class MainFrameController { if (row < 0) return builder.getVariable(tableName).model.fireTableDataChanged() - core.eventBus.publish(new TrustEvent(persona : list[row], level : level)) + core.eventBus.publish(new TrustEvent(persona : list[row].persona, level : level)) } @ControllerAction diff --git a/gui/griffon-app/controllers/com/muwire/gui/SearchTabController.groovy b/gui/griffon-app/controllers/com/muwire/gui/SearchTabController.groovy index 50780b6c..bb3ef2ad 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/SearchTabController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/SearchTabController.groovy @@ -7,6 +7,7 @@ import griffon.metadata.ArtifactProviderFor import net.i2p.data.Base64 import javax.annotation.Nonnull +import javax.swing.JOptionPane import com.muwire.core.Core import com.muwire.core.Persona @@ -67,8 +68,9 @@ class SearchTabController { int row = view.selectedSenderRow() if (row < 0) return + String reason = JOptionPane.showInputDialog("Enter reason (optional)") def sender = model.senders[row] - core.eventBus.publish( new TrustEvent(persona : sender, level : TrustLevel.TRUSTED)) + core.eventBus.publish( new TrustEvent(persona : sender, level : TrustLevel.TRUSTED, reason : reason)) } @ControllerAction @@ -76,8 +78,9 @@ class SearchTabController { int row = view.selectedSenderRow() if (row < 0) return + String reason = JOptionPane.showInputDialog("Enter reason (optional)") def sender = model.senders[row] - core.eventBus.publish( new TrustEvent(persona : sender, level : TrustLevel.DISTRUSTED)) + core.eventBus.publish( new TrustEvent(persona : sender, level : TrustLevel.DISTRUSTED, reason : reason)) } @ControllerAction diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index 78997f19..fd6f2b9c 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -422,7 +422,8 @@ class MainFrameView { scrollPane(constraints : BorderLayout.CENTER) { table(id : "trusted-table", autoCreateRowSorter : true, rowHeight : rowHeight) { tableModel(list : model.trusted) { - closureColumn(header : "Trusted Users", type : String, read : { it.getHumanReadableName() } ) + closureColumn(header : "Trusted Users", type : String, read : { it.persona.getHumanReadableName() } ) + closureColumn(header : "Reason", type : String, read : {it.reason}) } } } @@ -438,7 +439,8 @@ class MainFrameView { scrollPane(constraints : BorderLayout.CENTER) { table(id : "distrusted-table", autoCreateRowSorter : true, rowHeight : rowHeight) { tableModel(list : model.distrusted) { - closureColumn(header: "Distrusted Users", type : String, read : { it.getHumanReadableName() } ) + closureColumn(header: "Distrusted Users", type : String, read : { it.persona.getHumanReadableName() } ) + closureColumn(header: "Reason", type : String, read : {it.reason}) } } }