diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 77e7094e..5e5d7c98 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -28,6 +28,7 @@ import com.muwire.core.files.FileSharedEvent import com.muwire.core.files.FileUnsharedEvent import com.muwire.core.files.HasherService import com.muwire.core.files.PersisterService +import com.muwire.core.files.UICommentEvent import com.muwire.core.files.UIPersistFilesEvent import com.muwire.core.files.AllFilesLoadedEvent import com.muwire.core.files.DirectoryUnsharedEvent @@ -216,6 +217,7 @@ public class Core { eventBus.register(FileUnsharedEvent.class, fileManager) eventBus.register(SearchEvent.class, fileManager) eventBus.register(DirectoryUnsharedEvent.class, fileManager) + eventBus.register(UICommentEvent.class, fileManager) log.info("initializing mesh manager") MeshManager meshManager = new MeshManager(fileManager, home, props) diff --git a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy index 24b80602..d989739e 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy @@ -8,8 +8,10 @@ import com.muwire.core.UILoadedEvent import com.muwire.core.search.ResultsEvent import com.muwire.core.search.SearchEvent import com.muwire.core.search.SearchIndex +import com.muwire.core.util.DataUtil import groovy.util.logging.Log +import net.i2p.data.Base64 @Log class FileManager { @@ -20,6 +22,7 @@ class FileManager { final Map> rootToFiles = Collections.synchronizedMap(new HashMap<>()) final Map fileToSharedFile = Collections.synchronizedMap(new HashMap<>()) final Map> nameToFiles = new HashMap<>() + final Map> commentToFile = new HashMap<>() final SearchIndex index = new SearchIndex() FileManager(EventBus eventBus, MuWireSettings settings) { @@ -62,6 +65,18 @@ class FileManager { } existingFiles.add(sf.getFile()) + String comment = sf.getComment() + if (comment != null) { + comment = DataUtil.readi18nString(Base64.decode(comment)) + index.add(comment) + Set existingComment = commentToFile.get(comment) + if(existingComment == null) { + existingComment = new HashSet<>() + commentToFile.put(comment, existingComment) + } + existingComment.add(sf.getFile()) + } + index.add(name) } @@ -86,9 +101,43 @@ class FileManager { nameToFiles.remove(name) } } + + String comment = sf.getComment() + if (comment != null) { + index.remove(comment) + Set existingComment = commentToFile.get(comment) + if (existingComment != null) { + existingComment.remove(sf.getFile()) + if (existingComment.isEmpty()) + commentToFile.remove(comment) + } + } index.remove(name) } + + void onUICommentEvent(UICommentEvent e) { + if (e.oldComment != null) { + def comment = DataUtil.readi18nString(Base64.decode(e.oldComment)) + index.remove(comment) + Set existingFiles = commentToFile.get(comment) + existingFiles.remove(e.sharedFile.getFile()) + if (existingFiles.isEmpty()) + commentToFile.remove(comment) + } + + String comment = e.sharedFile.getComment() + comment = DataUtil.readi18nString(Base64.decode(comment)) + if (comment != null) { + index.add(comment) + Set existingComment = commentToFile.get(comment) + if(existingComment == null) { + existingComment = new HashSet<>() + commentToFile.put(comment, existingComment) + } + existingComment.add(e.sharedFile.getFile()) + } + } Map getSharedFiles() { synchronized(fileToSharedFile) { @@ -112,10 +161,14 @@ class FileManager { } else { def names = index.search e.searchTerms Set files = new HashSet<>() - names.each { files.addAll nameToFiles.getOrDefault(it, []) } + names.each { + files.addAll nameToFiles.getOrDefault(it, []) + files.addAll commentToFile.getOrDefault(it, []) + } Set sharedFiles = new HashSet<>() files.each { sharedFiles.add fileToSharedFile[it] } files = filter(sharedFiles, e.oobInfohash) + if (!sharedFiles.isEmpty()) re = new ResultsEvent(results: sharedFiles.asList(), uuid: e.uuid, searchEvent: e) diff --git a/core/src/main/groovy/com/muwire/core/files/UICommentEvent.groovy b/core/src/main/groovy/com/muwire/core/files/UICommentEvent.groovy new file mode 100644 index 00000000..b2b2d659 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/files/UICommentEvent.groovy @@ -0,0 +1,9 @@ +package com.muwire.core.files + +import com.muwire.core.Event +import com.muwire.core.SharedFile + +class UICommentEvent extends Event { + SharedFile sharedFile + String oldComment +} diff --git a/gui/griffon-app/controllers/com/muwire/gui/AddCommentController.groovy b/gui/griffon-app/controllers/com/muwire/gui/AddCommentController.groovy index 0b618b2c..0f44d777 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/AddCommentController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/AddCommentController.groovy @@ -8,6 +8,8 @@ import net.i2p.data.Base64 import javax.annotation.Nonnull +import com.muwire.core.Core +import com.muwire.core.files.UICommentEvent import com.muwire.core.util.DataUtil @ArtifactProviderFor(GriffonController) @@ -17,6 +19,8 @@ class AddCommentController { @MVCMember @Nonnull AddCommentView view + Core core + @ControllerAction void save() { String comment = view.textarea.getText() @@ -25,7 +29,9 @@ class AddCommentController { else comment = Base64.encode(DataUtil.encodei18nString(comment)) model.selectedFiles.each { + def event = new UICommentEvent(sharedFile : it, oldComment : it.getComment()) it.setComment(comment) + core.eventBus.publish(event) } mvcGroup.parentGroup.view.refreshSharedFiles() cancel() diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index d3dd1cce..dbbb7dd4 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -288,6 +288,7 @@ class MainFrameController { Map params = new HashMap<>() params['selectedFiles'] = selectedFiles + params['core'] = core mvcGroup.createMVCGroup("add-comment", "Add Comment", params) }