From dd622b1c0db972084e4e9eb8bae33e0d36553c0a Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 2 Nov 2020 01:04:06 +0000 Subject: [PATCH] handle hash searches for collections --- .../main/groovy/com/muwire/core/Core.groovy | 1 + .../core/collections/CollectionManager.groovy | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index fa0e0cdc..153e95e2 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -305,6 +305,7 @@ public class Core { register(UIDownloadCollectionEvent.class, collectionManager) register(FileDownloadedEvent.class, collectionManager) register(FileUnsharedEvent.class, collectionManager) + register(SearchEvent.class, collectionManager) } log.info("initializing mesh manager") diff --git a/core/src/main/groovy/com/muwire/core/collections/CollectionManager.groovy b/core/src/main/groovy/com/muwire/core/collections/CollectionManager.groovy index a0e1b52b..762ff8a0 100644 --- a/core/src/main/groovy/com/muwire/core/collections/CollectionManager.groovy +++ b/core/src/main/groovy/com/muwire/core/collections/CollectionManager.groovy @@ -13,12 +13,15 @@ import java.util.concurrent.ThreadFactory import java.util.logging.Level import com.muwire.core.EventBus +import com.muwire.core.SharedFile import com.muwire.core.InfoHash import com.muwire.core.MuWireSettings import com.muwire.core.files.AllFilesLoadedEvent import com.muwire.core.files.FileDownloadedEvent import com.muwire.core.files.FileManager import com.muwire.core.files.FileUnsharedEvent +import com.muwire.core.search.ResultsEvent +import com.muwire.core.search.SearchEvent import groovy.transform.CompileStatic import groovy.util.logging.Log @@ -247,6 +250,24 @@ class CollectionManager { affected.each { c -> diskIO.execute({delete(c)} as Runnable) } - + } + + synchronized void onSearchEvent(SearchEvent e) { + if (e.searchHash == null) + return + InfoHash ih = new InfoHash(e.searchHash) + def collection = rootToCollection.get(ih) + if (collection == null) + return + List sharedFiles = new ArrayList<>() + collection.files.each { item -> + def sfs = fileManager.getRootToFiles().get(item.infoHash) + if (sfs == null || sfs.isEmpty()) + return // hmm + sfs.each { sf -> sf.hit(e.persona, e.timestamp, "Collection Search")} + sharedFiles.addAll(sfs) + } + def resultEvent = new ResultsEvent(results : sharedFiles.toArray(new SharedFile[0]), uuid : e.uuid, searchEvent : e) + eventBus.publish(resultEvent) } }