Merge pull request #26 from informatik-ag-ngl/f/contacts
Contacts handling on the server
This commit is contained in:
commit
0a032c4cd1
@ -38,6 +38,8 @@ public class Startup {
|
|||||||
processors.add(new MessageStatusChangeProcessor());
|
processors.add(new MessageStatusChangeProcessor());
|
||||||
processors.add(new UserStatusChangeProcessor());
|
processors.add(new UserStatusChangeProcessor());
|
||||||
processors.add(new IdGeneratorRequestProcessor());
|
processors.add(new IdGeneratorRequestProcessor());
|
||||||
|
processors.add(new ContactsRequestEventProcessor());
|
||||||
|
processors.add(new ContactOperationProcessor());
|
||||||
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
|
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
|
||||||
|
|
||||||
initializeCurrentMessageId();
|
initializeCurrentMessageId();
|
||||||
|
@ -16,14 +16,19 @@ import javax.persistence.*;
|
|||||||
* Created: <strong>02.01.2020</strong><br>
|
* Created: <strong>02.01.2020</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
|
* @author Maximilian Käfer
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
@NamedQueries(
|
@NamedQueries(
|
||||||
{ @NamedQuery(query = "SELECT u FROM User u WHERE u.name = :name", name = "getUserByName"),
|
{ @NamedQuery(query = "SELECT u FROM User u WHERE u.name = :name", name = "getUserByName"), @NamedQuery(
|
||||||
@NamedQuery(query = "SELECT u.contacts FROM User u WHERE u = :user", name = "getContactsOfUser")// not tested
|
query = "SELECT u.contacts FROM User u WHERE u = :user",
|
||||||
}
|
name = "getContactsOfUser"
|
||||||
|
), @NamedQuery(
|
||||||
|
query = "SELECT u FROM User u WHERE (lower(u.name) LIKE lower(:searchPhrase) AND u <> :context AND NOT :context in elements(u.contacts))",
|
||||||
|
name = "searchUsers"
|
||||||
|
) }
|
||||||
)
|
)
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
@ -37,7 +42,7 @@ public class User {
|
|||||||
private Date lastSeen;
|
private Date lastSeen;
|
||||||
private envoy.data.User.UserStatus status;
|
private envoy.data.User.UserStatus status;
|
||||||
|
|
||||||
@OneToMany(targetEntity = User.class, cascade = CascadeType.ALL, orphanRemoval = true)
|
@ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL)
|
||||||
private List<User> contacts;
|
private List<User> contacts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -177,12 +177,44 @@ public class PersistenceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param user the User whose contacts should be retrieved
|
* Searches for users matching a search phrase. Contacts of the attached user
|
||||||
* @return the contacts of this User - currently everyone using Envoy
|
* and the attached user is ignored.
|
||||||
|
*
|
||||||
|
* @param searchPhrase the search phrase
|
||||||
|
* @param userId the ID of the user in whose context the search is
|
||||||
|
* performed
|
||||||
|
* @return a list of all users who matched the criteria
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public List<User> getContacts(User user) { return entityManager.createQuery("FROM User").getResultList(); }
|
public List<User> searchUsers(String searchPhrase, long userId) {
|
||||||
// TODO current solution gets all users, not just contacts. Should be changed to
|
return entityManager.createNamedQuery("searchUsers")
|
||||||
// entityManager.createNamedQuery("getContactsOfUser").setParameter("user",
|
.setParameter("searchPhrase", searchPhrase + "%")
|
||||||
// user).getResultList();
|
.setParameter("context", getUserById(userId))
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a user to the contact list of another user and vice versa.
|
||||||
|
*
|
||||||
|
* @param userId1 the ID of the first user
|
||||||
|
* @param userId2 the ID of the second user
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
public void addContact(long userId1, long userId2) {
|
||||||
|
User u1 = getUserById(userId1);
|
||||||
|
User u2 = getUserById(userId2);
|
||||||
|
u1.getContacts().add(u2);
|
||||||
|
u2.getContacts().add(u1);
|
||||||
|
updateUser(u1);
|
||||||
|
updateUser(u2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param user the User whose contacts should be retrieved
|
||||||
|
* @return the contacts of this User
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
public List<User> getContacts(User user) {
|
||||||
|
return entityManager.createNamedQuery("getContactsOfUser").setParameter("user", user).getResultList();
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package envoy.server.processors;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import envoy.data.Contacts;
|
||||||
|
import envoy.event.ContactOperationEvent;
|
||||||
|
import envoy.server.ConnectionManager;
|
||||||
|
import envoy.server.ObjectProcessor;
|
||||||
|
import envoy.server.database.PersistenceManager;
|
||||||
|
import envoy.server.net.ObjectWriteProxy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project: <strong>envoy-server-standalone</strong><br>
|
||||||
|
* File: <strong>ContactOperationProcessor.java</strong><br>
|
||||||
|
* Created: <strong>08.02.2020</strong><br>
|
||||||
|
*
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
public class ContactOperationProcessor implements ObjectProcessor<ContactOperationEvent> {
|
||||||
|
|
||||||
|
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void process(ContactOperationEvent evt, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||||
|
switch (evt.getOperationType()) {
|
||||||
|
case ADD:
|
||||||
|
final long userId = ConnectionManager.getInstance().getUserIdBySocketId(socketId);
|
||||||
|
final long contactId = evt.get().getId();
|
||||||
|
|
||||||
|
System.out.printf("Adding user %s to the contact list of user %d.%n", evt.get(), userId);
|
||||||
|
PersistenceManager.getPersistenceManager().addContact(userId, contactId);
|
||||||
|
|
||||||
|
// Notify the contact if online
|
||||||
|
if (ConnectionManager.getInstance().isOnline(contactId)) writeProxy.write(connectionManager.getSocketId(contactId),
|
||||||
|
new Contacts(Arrays.asList(PersistenceManager.getPersistenceManager().getUserById(userId).toCommonUser())));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.err.printf("Received %s with an unsupported operation.%n", evt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<ContactOperationEvent> getInputClass() { return ContactOperationEvent.class; }
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package envoy.server.processors;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import envoy.data.Contacts;
|
||||||
|
import envoy.event.ContactSearchRequest;
|
||||||
|
import envoy.event.ContactSearchResult;
|
||||||
|
import envoy.server.ConnectionManager;
|
||||||
|
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>ContactsRequestEventProcessor.java</strong><br>
|
||||||
|
* Created: <strong>08.02.2020</strong><br>
|
||||||
|
*
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
|
* @author Maximilian Käfer
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
public class ContactsRequestEventProcessor implements ObjectProcessor<ContactSearchRequest> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a {@link Contacts} list to the client containing all {@link User}s
|
||||||
|
* matching the search phrase contained inside the request. The client and their
|
||||||
|
* contacts are excluded from the result.
|
||||||
|
*
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void process(ContactSearchRequest request, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||||
|
writeProxy.write(socketId,
|
||||||
|
new ContactSearchResult(PersistenceManager.getPersistenceManager()
|
||||||
|
.searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketId(socketId))
|
||||||
|
.stream()
|
||||||
|
.map(User::toCommonUser)
|
||||||
|
.collect(Collectors.toList())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<ContactSearchRequest> getInputClass() { return ContactSearchRequest.class; }
|
||||||
|
}
|
@ -1,9 +1,7 @@
|
|||||||
package envoy.server.processors;
|
package envoy.server.processors;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import envoy.data.Contacts;
|
import envoy.data.Contacts;
|
||||||
@ -52,8 +50,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
|||||||
UserStatusChangeProcessor.updateUserStatus(user);
|
UserStatusChangeProcessor.updateUserStatus(user);
|
||||||
|
|
||||||
// Create contacts
|
// Create contacts
|
||||||
Contacts contacts = new Contacts(user.getId(),
|
Contacts contacts = new Contacts(user.getContacts().stream().map(envoy.server.data.User::toCommonUser).collect(Collectors.toList()));
|
||||||
user.getContacts().stream().map(envoy.server.data.User::toCommonUser).collect(Collectors.toList()));
|
|
||||||
contacts.getContacts().add(user.toCommonUser());
|
contacts.getContacts().add(user.toCommonUser());
|
||||||
|
|
||||||
// Complete handshake
|
// Complete handshake
|
||||||
@ -81,8 +78,8 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
|||||||
user.setLastSeen(new Date());
|
user.setLastSeen(new Date());
|
||||||
user.setStatus(User.UserStatus.ONLINE);
|
user.setStatus(User.UserStatus.ONLINE);
|
||||||
user.setPasswordHash(credentials.getPasswordHash());
|
user.setPasswordHash(credentials.getPasswordHash());
|
||||||
user.setContacts(PersistenceManager.getPersistenceManager().getContacts(user));
|
|
||||||
persistenceManager.addUser(user);
|
persistenceManager.addUser(user);
|
||||||
|
user.setContacts(new ArrayList<>());
|
||||||
} else {
|
} else {
|
||||||
user = persistenceManager.getUserByName(credentials.getName());
|
user = persistenceManager.getUserByName(credentials.getName());
|
||||||
// TODO: Implement error when user does not exist
|
// TODO: Implement error when user does not exist
|
||||||
|
Reference in New Issue
Block a user