Commit f79dbb14 authored by Magne.Stenseng's avatar Magne.Stenseng
Browse files

Replace InChat.java

parent f4cf8482
......@@ -30,14 +30,10 @@ public class InChat {
private final EventStorage eventStore;
private final AccountStorage accountStore;
private final SessionStorage sessionStore;
private final Map<UUID,List<Consumer<Channel.Event>>> eventCallbacks
= new TreeMap<UUID,List<Consumer<Channel.Event>>>();
private final Map<UUID,List<Consumer<Channel.Event>>> eventCallbacks = new TreeMap<UUID,List<Consumer<Channel.Event>>>();
public InChat(UserStorage userStore,
ChannelStorage channelStore,
AccountStorage accountStore,
SessionStorage sessionStore,
Connection connection) {
public InChat(UserStorage userStore, ChannelStorage channelStore, AccountStorage accountStore,
SessionStorage sessionStore, Connection connection) {
this.userStore=userStore;
this.channelStore=channelStore;
this.eventStore=channelStore.eventStore;
......@@ -83,14 +79,15 @@ public class InChat {
/**
* Log in a user to the chat.
*/
public Maybe<Stored<Session>> login(final String username,
final String password) {
public Maybe<Stored<Session>> login(final String username, final String password1) {
Password password = new Password(password1);
return atomic(result -> {
final Stored<Account> account = accountStore.lookup(username);
final Stored<Session> session =
sessionStore.save(new Session(account, Instant.now().plusSeconds(60*60*24)));
// Check that password is not incorrect and not too long.
if (!(!account.value.password.equals(password) && !(password.length() > 1000))) {
if (!(!account.value.password.equals(password) && !(password.getLength() > 1000))) {
result.accept(session);
}
});
......@@ -100,7 +97,8 @@ public class InChat {
* Register a new user.
*/
public Maybe<Stored<Session>> register(final String username,
final String password) {
final String password1) {
Password password = new Password(password1);
return atomic(result -> {
final Stored<User> user =
userStore.save(User.create(username));
......@@ -149,18 +147,12 @@ public class InChat {
UUID channelID) {
return atomic(result -> {
Stored<Channel> channel = channelStore.get(channelID);
Util.updateSingle(account,
accountStore,
a -> a.value.joinChannel(channel.value.name,channel));
Stored<Channel.Event> joinEvent
= channelStore.eventStore.save(
Channel.Event.createJoinEvent(channelID,
Instant.now(),
account.value.user.value.name));
result.accept(
Util.updateSingle(channel,
channelStore,
c -> c.value.postEvent(joinEvent)));
Util.updateSingle(account, accountStore, a ->
a.value.joinChannel(channel.value.name,channel));
Stored<Channel.Event> joinEvent =
channelStore.eventStore.save(Channel.Event.
createJoinEvent(channelID, Instant.now(), account.value.user.value.name.getUserName()));
result.accept(Util.updateSingle(channel, channelStore, c -> c.value.postEvent(joinEvent)));
});
}
......@@ -174,7 +166,7 @@ public class InChat {
Stored<Channel.Event> event
= channelStore.eventStore.save(
Channel.Event.createMessageEvent(channel.identity,Instant.now(),
account.value.user.value.name, message));
account.value.user.value.name.getUserName(), message));
result.accept (
Util.updateSingle(channel,
channelStore,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment