Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix users being completely broken. #464

Merged
merged 2 commits into from Jan 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 26 additions & 62 deletions src/com/dmdirc/Client.java
Expand Up @@ -38,97 +38,61 @@
*/
public class Client implements User {

private final Collection<GroupChat> groupChats;
private final Connection connection;
private final ClientInfo clientInfo;
private String nickname;
private Optional<String> username;
private Optional<String> hostname;
private Optional<String> realname;
private Optional<String> awayMessage;

public Client(final String nickname, final Connection connection, final ClientInfo clientInfo) {
this(nickname, connection, Optional.empty(), Optional.empty(), Optional.empty(), clientInfo);
}

public Client(final String nickname, final Connection connection,
final Optional<String> username,
final Optional<String> hostname,
final Optional<String> realname,
final ClientInfo clientInfo) {
this.nickname = nickname;
public Client(final Connection connection, final ClientInfo clientInfo) {
this.connection = connection;
this.username = username;
this.hostname = hostname;
this.realname = realname;
this.clientInfo = clientInfo;
groupChats = new ArrayList<>();
awayMessage = Optional.empty();
}

@Override
public String getNickname() {
return nickname;
}

@Override
public void setNickname(final String nickname) {
this.nickname = nickname;
return clientInfo.getNickname();
}

@Override
public Optional<String> getUsername() {
return username;
}

@Override
public void setUsername(final Optional<String> username) {
this.username = username;
if (clientInfo.getUsername().isEmpty()) {
return Optional.empty();
} else {
return Optional.of(clientInfo.getUsername());
}
}

@Override
public Optional<String> getHostname() {
return hostname;
}

@Override
public void setHostname(final Optional<String> hostname) {
this.hostname = hostname;
if (clientInfo.getHostname().isEmpty()) {
return Optional.empty();
} else {
return Optional.of(clientInfo.getHostname());
}
}

@Override
public Optional<String> getRealname() {
return realname;
}

@Override
public void setRealname(final Optional<String> realname) {
this.realname = realname;
if (clientInfo.getRealname().isEmpty()) {
return Optional.empty();
} else {
return Optional.of(clientInfo.getRealname());
}
}

@Override
public Collection<GroupChat> getGroupChats() {
return Collections.unmodifiableCollection(groupChats);
}

@Override
public void addGroupChat(final GroupChat groupChat) {
groupChats.add(groupChat);
}

@Override
public void delGroupChat(final GroupChat groupChat) {
groupChats.remove(groupChat);
final Collection<GroupChat> channels = new ArrayList<>();
clientInfo.getChannelClients().forEach(c -> connection.getGroupChatManager()
.getChannel(c.getChannel().getName()).ifPresent(channels::add));
return Collections.unmodifiableCollection(channels);
}

@Override
public Optional<String> getAwayMessage() {
return awayMessage;
}

@Override
public void setAwayMessage(final Optional<String> awayMessage) {
this.awayMessage = awayMessage;
if (clientInfo.getAwayReason().isEmpty()) {
return Optional.empty();
} else {
return Optional.of(clientInfo.getAwayReason());
}
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/com/dmdirc/Query.java
Expand Up @@ -230,7 +230,6 @@ public void onNickChanged(final Parser parser, final Date date,

addLine(format, oldNick, client.getUsername(),
client.getHostname(), client.getNickname());
user.setNickname(client.getNickname());
updateTitle();

setName(client.getNickname());
Expand Down
13 changes: 2 additions & 11 deletions src/com/dmdirc/UserFactory.java
Expand Up @@ -26,8 +26,6 @@
import com.dmdirc.interfaces.User;
import com.dmdirc.parser.interfaces.ClientInfo;

import java.util.Optional;

import javax.inject.Inject;

/**
Expand All @@ -39,14 +37,7 @@ public class UserFactory {
public UserFactory() {
}

public User getUser(final String nickname, final Connection connection,
final ClientInfo clientInfo) {
return new Client(nickname, connection, clientInfo);
}

public User getUser(final String nickname, final Connection connection,
final Optional<String> username, final Optional<String> hostname,
final Optional<String> realname, final ClientInfo clientInfo) {
return new Client(nickname, connection, username, hostname, realname, clientInfo);
public User getUser(final Connection connection, final ClientInfo clientInfo) {
return new Client(connection, clientInfo);
}
}
6 changes: 1 addition & 5 deletions src/com/dmdirc/UserManager.java
Expand Up @@ -28,7 +28,6 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand All @@ -49,10 +48,7 @@ public UserManager(final UserFactory userFactory) {
}

public User getUserFromClientInfo(final ClientInfo client, final Connection connection) {
userCache.putIfAbsent(client, userFactory.getUser(client.getNickname(), connection,
Optional.ofNullable(client.getUsername()),
Optional.ofNullable(client.getHostname()),
Optional.ofNullable(client.getRealname()), client));
userCache.putIfAbsent(client, userFactory.getUser(connection,client));
return userCache.get(client);
}
}
49 changes: 0 additions & 49 deletions src/com/dmdirc/interfaces/User.java
Expand Up @@ -39,90 +39,41 @@ public interface User {
*/
String getNickname();

/**
* Sets the nickname for this client.
*
* @param nickname New nickname for this client
*/
void setNickname(final String nickname);

/**
* Retrieves the username or ident used by this client.
*
* @return This client's username
*/
Optional<String> getUsername();

/**
* Sets the username for this client.
*
* @param username New username
*/
void setUsername(final Optional<String> username);

/**
* Retrieves the hostname that this client is connecting from.
*
* @return This client's hostname
*/
Optional<String> getHostname();

/**
* Sets the hostname for this client.
*
* @param hostname New hostname
*/
void setHostname(final Optional<String> hostname);

/**
* Retrieves the full/real name of the client.
*
* @return This client's real name
*/
Optional<String> getRealname();

/**
* Sets the realname for the client.
*
* @param realname New realname
*/
void setRealname(final Optional<String> realname);

/**
* Retries the {@link GroupChat}s the client is a member of.
*
* @return Collection of {@link GroupChat}s, may be empty
*/
Collection<GroupChat> getGroupChats();

/**
* Adds a {@link GroupChat} to the list of {@link GroupChat}s this user is on.
*
* @param groupChat GroupChat to add
*/
void addGroupChat(final GroupChat groupChat);

/**
* Deletes a {@link GroupChat} from the list of {@link GroupChat}s this user is on.
*
* @param groupChat GroupChat to add
*/
void delGroupChat(final GroupChat groupChat);

/**
* Returns the away message for the client, if the away message is empty the user is not away.
*
* @return Optional.empty if the user is not away, wrapped away reason if they are
*/
Optional<String> getAwayMessage();

/**
* Sets the away message for the client.
*
* @param awayMessage Optional.empty if the user is not away, wrapped away reason if they are
*/
void setAwayMessage(final Optional<String> awayMessage);

/**
* Returns the away state for this user.
*
Expand Down
5 changes: 1 addition & 4 deletions test/com/dmdirc/ServerEventHandlerTest.java
Expand Up @@ -83,10 +83,7 @@ public void setUp() {
when(server.getState()).thenReturn(ServerState.CONNECTED);
when(parser.getCallbackManager()).thenReturn(callbackManager);
when(server.getConnection()).thenReturn(Optional.ofNullable(server));
when(userFactory.getUser(anyString(), any(Connection.class), eq(clientInfo))).thenReturn(user);
when(userFactory.getUser(anyString(), any(Connection.class), Optional.of(anyString()),
Optional.of(anyString()), Optional.of(anyString()), eq(clientInfo)))
.thenReturn(user);
when(userFactory.getUser(any(Connection.class), eq(clientInfo))).thenReturn(user);
when(server.getUser(anyString())).thenReturn(user);
when(server.getLocalUser()).thenReturn(Optional.of(user));
final ServerEventHandler handler = new ServerEventHandler(server, groupChatManager,
Expand Down