add optinal reason for trusting/distrusting

This commit is contained in:
Zlatin Balevsky
2019-11-08 06:46:03 +00:00
parent 00a8d100fe
commit 0801bfec08
6 changed files with 75 additions and 21 deletions

View File

@@ -7,4 +7,5 @@ class TrustEvent extends Event {
Persona persona
TrustLevel level
String reason
}

View File

@@ -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<Destination, Persona> good = new ConcurrentHashMap<>()
final Map<Destination, Persona> bad = new ConcurrentHashMap<>()
final Map<Destination, TrustEntry> good = new ConcurrentHashMap<>()
final Map<Destination, TrustEntry> 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
}
}
}

View File

@@ -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() {

View File

@@ -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

View File

@@ -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

View File

@@ -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})
}
}
}