This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/server/processors/ContactsRequestProcesor.java

56 lines
2.1 KiB
Java

package envoy.server.processors;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import envoy.data.Contacts;
import envoy.event.ContactsRequest;
import envoy.server.ObjectProcessor;
import envoy.server.data.User;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectWriteProxy;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>ContactsRequestProcesor.java</strong><br>
* Created: <strong>08.02.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha
*/
public class ContactsRequestProcesor implements ObjectProcessor<ContactsRequest> {
@Override
public void process(ContactsRequest request, long socketId, ObjectWriteProxy writeProxy) throws IOException {
// Creating a List containing all searchResults
List<User> resultList = PersistenceManager.getPersistenceManager().searchUsers(request.get());
// Creating a List containing all contacts of the client this event comes from.
List<User> clientContacts = PersistenceManager.getPersistenceManager()
.getContacts(PersistenceManager.getPersistenceManager().getUserById(request.getClient().getId()));
List<User> returnList = new ArrayList<User>();
// Checking for already existing users in the contacts of the client an only
// adding the ones not included to the returnList.
if (clientContacts.size() != 0) {
for (int i = 0; i < resultList.size(); i++) {
for (int j = 0; j < clientContacts.size(); j++) {
if (resultList.get(i).getId() != clientContacts.get(j).getId()) { returnList.add(resultList.get(i)); }
}
}
} else {
for (int i = 0; i < resultList.size(); i++) {
returnList.add(resultList.get(i));
}
}
// Create new Contacts object from returnList
Contacts contacts = new Contacts(returnList.stream().map(envoy.server.data.User::toCommonUser).collect(Collectors.toList()));
writeProxy.write(socketId, contacts);
}
@Override
public Class<ContactsRequest> getInputClass() { return ContactsRequest.class; }
}