Skip to content

Commit

Permalink
Add experimental trends search output
Browse files Browse the repository at this point in the history
  • Loading branch information
zapek committed Nov 17, 2024
1 parent 7301fd1 commit dfa74df
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.xeres.app.service.notification.contact.ContactNotificationService;
import io.xeres.app.service.notification.file.FileNotificationService;
import io.xeres.app.service.notification.file.FileSearchNotificationService;
import io.xeres.app.service.notification.file.FileTrendNotificationService;
import io.xeres.app.service.notification.forum.ForumNotificationService;
import io.xeres.app.service.notification.status.StatusNotificationService;
import org.springframework.http.MediaType;
Expand All @@ -46,15 +47,17 @@ public class NotificationController
private final ForumNotificationService forumNotificationService;
private final FileNotificationService fileNotificationService;
private final FileSearchNotificationService fileSearchNotificationService;
private final FileTrendNotificationService fileTrendNotificationService;
private final ContactNotificationService contactNotificationService;
private final AvailabilityNotificationService availabilityNotificationService;

public NotificationController(StatusNotificationService statusNotificationService, ForumNotificationService forumNotificationService, FileNotificationService fileNotificationService, FileSearchNotificationService fileSearchNotificationService, ContactNotificationService contactNotificationService, AvailabilityNotificationService availabilityNotificationService)
public NotificationController(StatusNotificationService statusNotificationService, ForumNotificationService forumNotificationService, FileNotificationService fileNotificationService, FileSearchNotificationService fileSearchNotificationService, FileTrendNotificationService fileTrendNotificationService, ContactNotificationService contactNotificationService, AvailabilityNotificationService availabilityNotificationService)
{
this.statusNotificationService = statusNotificationService;
this.forumNotificationService = forumNotificationService;
this.fileNotificationService = fileNotificationService;
this.fileSearchNotificationService = fileSearchNotificationService;
this.fileTrendNotificationService = fileTrendNotificationService;
this.contactNotificationService = contactNotificationService;
this.availabilityNotificationService = availabilityNotificationService;
}
Expand Down Expand Up @@ -91,6 +94,14 @@ public SseEmitter setupFileSearchNotification()
return fileSearchNotificationService.addClient();
}

@GetMapping("/file-trend")
@Operation(summary = "Subscribe to file trend notifications")
@ApiResponse(responseCode = "200", description = "Request completed successfully")
public SseEmitter setupFileTrendNotification()
{
return fileTrendNotificationService.addClient();
}

@GetMapping("/contact")
@Operation(summary = "Subscribe to contact notifications")
@ApiResponse(responseCode = "200", description = "Request completed successfully")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.app.service.notification.file;

import io.xeres.app.service.notification.NotificationService;
import io.xeres.common.rest.notification.file.FileTrendNotification;
import org.springframework.stereotype.Service;

@Service
public class FileTrendNotificationService extends NotificationService
{
public void receivedSearch(String keywords)
{
sendNotification(new FileTrendNotification(keywords));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.xeres.app.service.SettingsService;
import io.xeres.app.service.file.FileService;
import io.xeres.app.service.notification.file.FileSearchNotificationService;
import io.xeres.app.service.notification.file.FileTrendNotificationService;
import io.xeres.app.xrs.item.Item;
import io.xeres.app.xrs.item.ItemUtils;
import io.xeres.app.xrs.service.RsService;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class FileTransferRsService extends RsService implements TurtleRsClient
private final FileService fileService;
private final PeerConnectionManager peerConnectionManager;
private final FileSearchNotificationService fileSearchNotificationService;
private final FileTrendNotificationService fileTrendNotificationService;
private final RsServiceRegistry rsServiceRegistry;
private final DatabaseSessionManager databaseSessionManager;
private final LocationService locationService;
Expand All @@ -88,13 +90,14 @@ public class FileTransferRsService extends RsService implements TurtleRsClient

private final Map<Sha1Sum, Sha1Sum> encryptedHashes = new ConcurrentHashMap<>();

public FileTransferRsService(RsServiceRegistry rsServiceRegistry, FileService fileService, PeerConnectionManager peerConnectionManager, FileSearchNotificationService fileSearchNotificationService, DatabaseSessionManager databaseSessionManager, LocationService locationService, SettingsService settingsService, NetworkProperties networkProperties, FileDownloadRepository fileDownloadRepository)
public FileTransferRsService(RsServiceRegistry rsServiceRegistry, FileService fileService, PeerConnectionManager peerConnectionManager, FileSearchNotificationService fileSearchNotificationService, FileTrendNotificationService fileTrendNotificationService, DatabaseSessionManager databaseSessionManager, LocationService locationService, SettingsService settingsService, NetworkProperties networkProperties, FileDownloadRepository fileDownloadRepository)
{
super(rsServiceRegistry);
this.fileService = fileService;
this.peerConnectionManager = peerConnectionManager;
this.fileSearchNotificationService = fileSearchNotificationService;
this.rsServiceRegistry = rsServiceRegistry;
this.fileTrendNotificationService = fileTrendNotificationService;
this.databaseSessionManager = databaseSessionManager;
this.locationService = locationService;
this.settingsService = settingsService;
Expand Down Expand Up @@ -279,6 +282,12 @@ public List<byte[]> receiveSearchRequest(byte[] query, int maxHits)
return List.of();
}

@Override
public void receiveSearchRequestString(String keywords)
{
fileTrendNotificationService.receivedSearch(keywords);
}

@Override
public void receiveSearchResult(int requestId, TurtleSearchResultItem item)
{
Expand Down Expand Up @@ -362,6 +371,7 @@ public void removeDownload(long id)
public void shutdown()
{
fileSearchNotificationService.shutdown();
fileTrendNotificationService.shutdown();
if (fileTransferManagerThread != null)
{
log.info("Stopping FileTransferManager...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public interface TurtleRsClient extends RsServiceSlave
*/
List<byte[]> receiveSearchRequest(byte[] query, int maxHits); // XXX: return a list of results (TurtleFileInfoV2.. actually it's generic stuff so service dependent)

void receiveSearchRequestString(String keywords); // XXX: experimental for now...

/**
* Called when receiving search results.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ private void handleSearchRequest(PeerConnection sender, TurtleSearchRequestItem
return;
}

// XXX: experimental
turtleClients.forEach(turtleRsClient -> turtleRsClient.receiveSearchRequestString(item.getKeywords()));

// Do not search further if enough has been sent back already.
if (searchRequest.isFull())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.xeres.app.service.notification.contact.ContactNotificationService;
import io.xeres.app.service.notification.file.FileNotificationService;
import io.xeres.app.service.notification.file.FileSearchNotificationService;
import io.xeres.app.service.notification.file.FileTrendNotificationService;
import io.xeres.app.service.notification.forum.ForumNotificationService;
import io.xeres.app.service.notification.status.StatusNotificationService;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -63,6 +64,9 @@ class NotificationControllerTest extends AbstractControllerTest
@MockBean
private AvailabilityNotificationService availabilityNotificationService;

@MockBean
private FileTrendNotificationService fileTrendNotificationService;

@Autowired
public MockMvc mvc;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.common.rest.notification.file;

import io.xeres.common.rest.notification.Notification;

public record FileTrendNotification(String keywords) implements Notification
{
}
11 changes: 11 additions & 0 deletions ui/src/main/java/io/xeres/ui/client/NotificationClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.xeres.common.rest.notification.contact.ContactNotification;
import io.xeres.common.rest.notification.file.FileNotification;
import io.xeres.common.rest.notification.file.FileSearchNotification;
import io.xeres.common.rest.notification.file.FileTrendNotification;
import io.xeres.common.rest.notification.forum.ForumNotification;
import io.xeres.common.rest.notification.status.StatusNotification;
import io.xeres.common.util.RemoteUtils;
Expand Down Expand Up @@ -96,6 +97,16 @@ public Flux<ServerSentEvent<FileSearchNotification>> getFileSearchNotifications(
});
}

public Flux<ServerSentEvent<FileTrendNotification>> getFileTrendNotifications()
{
return webClient.get()
.uri("/file-trend")
.retrieve()
.bodyToFlux(new ParameterizedTypeReference<>()
{
});
}

public Flux<ServerSentEvent<ContactNotification>> getContactNotifications()
{
return webClient.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public class FileMainController implements Controller
@FXML
private FileUploadViewController fileUploadViewController;

@FXML
private FileTrendViewController fileTrendViewController;

@Override
public void initialize()
{
Expand All @@ -72,6 +75,7 @@ private TabActivation idToController(String id)
case "search" -> fileSearchViewController;
case "downloads" -> fileDownloadViewController;
case "uploads" -> fileUploadViewController;
case "trends" -> fileTrendViewController;
default -> throw new IllegalStateException("Unexpected value: " + id);
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.ui.controller.file;

import io.xeres.ui.client.NotificationClient;
import io.xeres.ui.controller.Controller;
import io.xeres.ui.controller.TabActivation;
import io.xeres.ui.support.util.UiUtils;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import net.rgielen.fxweaver.core.FxmlView;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import reactor.core.Disposable;

import java.io.IOException;

@Component
@FxmlView(value = "/view/file/trend.fxml")
public class FileTrendViewController implements Controller, TabActivation
{
private final NotificationClient notificationClient;
private Disposable notificationDisposable;

@FXML
private TableView<TrendResult> trendTableView;

// @FXML
// private TableColumn<TrendResult, Integer> tableHits;
//
// @FXML
// private TableColumn<TrendResult, String> tableFrom;

@FXML
private TableColumn<TrendResult, String> tableTerms;

public FileTrendViewController(NotificationClient notificationClient)
{
this.notificationClient = notificationClient;
}

@Override
public void initialize() throws IOException
{
tableTerms.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().keywords()));

setupFileTrendNotifications();
}

private void setupFileTrendNotifications()
{
notificationDisposable = notificationClient.getFileTrendNotifications()
.doOnError(UiUtils::showAlertError)
.doOnNext(sse -> Platform.runLater(() -> {
assert sse.data() != null;
trendTableView.getItems().add(new TrendResult(sse.data().keywords()));
if (trendTableView.getItems().size() > 255) // XXX: maybe not optimal...
{
trendTableView.getItems().removeFirst();
}
}))
.subscribe();
}

@EventListener
public void onApplicationEvent(ContextClosedEvent ignored)
{
if (notificationDisposable != null && !notificationDisposable.isDisposed())
{
notificationDisposable.dispose();
}
}

@Override
public void activate()
{

}

@Override
public void deactivate()
{

}
}
24 changes: 24 additions & 0 deletions ui/src/main/java/io/xeres/ui/controller/file/TrendResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.ui.controller.file;

public record TrendResult(String keywords)
{
}
9 changes: 7 additions & 2 deletions ui/src/main/resources/view/file/main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
~ along with Xeres. If not, see <http://www.gnu.org/licenses/>.
-->

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import org.kordamp.ikonli.javafx.FontIcon?>
<VBox xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.xeres.ui.controller.file.FileMainController">
Expand All @@ -44,6 +43,12 @@
</graphic>
<fx:include fx:id="fileUploadView" source="upload.fxml"/>
</Tab>
<Tab fx:id="trends" text="Trends">
<graphic>
<FontIcon iconLiteral="mdi2t-trending-up"/>
</graphic>
<fx:include fx:id="fileTrendView" source="trend.fxml"/>
</Tab>
</TabPane>
</AnchorPane>
</VBox>
Loading

0 comments on commit dfa74df

Please sign in to comment.