Mark all elements as known in the processing method, not after rendering.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SearchPage.java
index b10162e..73673ab 100644 (file)
@@ -110,6 +110,17 @@ public class SearchPage extends SoneTemplatePage {
                templateContext.set("soneHits", sonePagination.getItems());
                templateContext.set("postPagination", postPagination);
                templateContext.set("postHits", postPagination.getItems());
+
+               /* mark found posts and Sones as knew. */
+               for (Sone sone : sonePagination.getItems()) {
+                       webInterface.getCore().markSoneKnown(sone);
+               }
+               for (Post post : postPagination.getItems()) {
+                       webInterface.getCore().markPostKnown(post);
+                       for (Reply reply : webInterface.getCore().getReplies(post)) {
+                               webInterface.getCore().markReplyKnown(reply);
+                       }
+               }
        }
 
        //
@@ -191,21 +202,31 @@ public class SearchPage extends SoneTemplatePage {
                int forbiddenHits = 0;
                int requiredPhrases = 0;
                for (Phrase phrase : phrases) {
+                       String phraseString = phrase.getPhrase().toLowerCase();
                        if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
                                ++requiredPhrases;
                        }
-                       boolean matches = expression.toLowerCase().contains(phrase.getPhrase().toLowerCase());
-                       if (!matches) {
+                       int matches = 0;
+                       int index = 0;
+                       while (index < expression.length()) {
+                               int position = expression.toLowerCase().indexOf(phraseString, index);
+                               if (position == -1) {
+                                       break;
+                               }
+                               index = position + phraseString.length();
+                               ++matches;
+                       }
+                       if (matches == 0) {
                                continue;
                        }
                        if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
-                               ++requiredHits;
+                               requiredHits += matches;
                        }
                        if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
-                               ++optionalHits;
+                               optionalHits += matches;
                        }
                        if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
-                               ++forbiddenHits;
+                               forbiddenHits += matches;
                        }
                }
                return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);