Skip to content

Commit

Permalink
feat(api): add option to extend chatter with custom pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed May 13, 2022
1 parent b8e71a1 commit 6074eb9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
17 changes: 17 additions & 0 deletions core/src/main/java/net/silthus/schat/chatter/Chatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import lombok.NonNull;
import net.kyori.adventure.text.Component;
import net.silthus.schat.channel.Channel;
Expand All @@ -42,6 +43,7 @@
import net.silthus.schat.message.MessageTarget;
import net.silthus.schat.message.Messages;
import net.silthus.schat.pointer.Pointer;
import net.silthus.schat.pointer.Pointers;
import net.silthus.schat.policies.JoinChannelPolicy;
import net.silthus.schat.policies.LeaveChannelPolicy;
import net.silthus.schat.repository.Entity;
Expand Down Expand Up @@ -247,6 +249,21 @@ interface Builder {
@ApiStatus.Internal
@NotNull Builder permissionHandler(@NonNull PermissionHandler permissionHandler);

/**
* Sets additional pointers provided by the created chatter.
*
* <p>An example would be adding a dynamic pointer to the current location of the chatter,
* allowing special channels to act upon the location of their members.</p>
*
* <p>This should be done by the {@link ChatterFactory} of the implementing platform.</p>
*
* @param pointers the pointers builder to consume
* @return this builder
* @since next
*/
@ApiStatus.Internal
@NotNull Builder pointers(Consumer<Pointers.Builder> pointers);

/**
* Creates the chatter.
*
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/net/silthus/schat/chatter/ChatterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -85,7 +86,7 @@ protected ChatterImpl(Builder builder) {
this.eventBus = builder.eventBus();
this.messageHandler = builder.messageHandler;
this.permissionHandler = builder.permissionHandler();
this.pointers = Pointers.pointersBuilder()
this.pointers = builder.pointers()
.withForward(Identity.ID, identity(), Identity.ID)
.withForward(Identity.NAME, identity(), Identity.NAME)
.withForward(Identity.DISPLAY_NAME, identity(), Identity.DISPLAY_NAME)
Expand Down Expand Up @@ -191,11 +192,18 @@ static final class Builder implements Chatter.Builder {
};
private @NonNull PermissionHandler permissionHandler = permission -> false;
private @NonNull EventBus eventBus = EventBus.empty();
private @NonNull Pointers.Builder pointers = Pointers.pointersBuilder();

private Builder(Identity identity) {
this.identity = identity;
}

@Override
public Chatter.@NotNull Builder pointers(Consumer<Pointers.Builder> pointers) {
pointers.accept(this.pointers);
return this;
}

@Override
public @NotNull Chatter create() {
return new ChatterImpl(this);
Expand Down
26 changes: 25 additions & 1 deletion core/src/test/java/net/silthus/schat/chatter/ChatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@
import net.silthus.schat.events.chatter.ChatterReceivedMessageEvent;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.message.Message;
import net.silthus.schat.pointer.Pointer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static net.silthus.schat.AssertionHelper.assertNPE;
import static net.silthus.schat.channel.ChannelHelper.randomChannel;
import static net.silthus.schat.chatter.Chatter.chatterBuilder;
import static net.silthus.schat.eventbus.EventBusMock.eventBusMock;
import static net.silthus.schat.identity.IdentityHelper.randomIdentity;
import static net.silthus.schat.message.MessageHelper.randomMessage;
import static net.silthus.schat.pointer.Pointer.pointer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

Expand Down Expand Up @@ -306,7 +309,7 @@ void given_no_permission_handler_does_not_throw() {

@BeforeEach
void setUp() {
chatter = Chatter.chatterBuilder(randomIdentity()).permissionHandler(permission -> {
chatter = chatterBuilder(randomIdentity()).permissionHandler(permission -> {
permissionHandlerCalled = true;
return false;
}).create();
Expand All @@ -319,4 +322,25 @@ void then_handler_is_called() {
}
}
}

@Nested class pointers {
@Test
void additional_pointers_are_accessible() {
final Pointer<String> pointer = pointer(String.class, "test");
Chatter chatter = chatterBuilder(randomIdentity())
.pointers(p -> p.withStatic(pointer, "TEST"))
.create();
assertThat(chatter.get(pointer))
.isPresent().get().isEqualTo("TEST");
}

@Test
void system_pointers_cannot_be_overwritten() {
final Identity identity = randomIdentity();
final Chatter chatter = chatterBuilder(identity)
.pointers(builder -> builder.withStatic(Identity.NAME, "invalid"))
.create();
assertThat(chatter.name()).isEqualTo(identity.name());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.chatter.ChatterFactory;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.pointer.Pointers;
import org.jetbrains.annotations.NotNull;

import static net.silthus.schat.chatter.Chatter.chatterBuilder;
Expand All @@ -38,6 +39,7 @@ public final Chatter createChatter(UUID id) {
return chatterBuilder(createIdentity(id))
.permissionHandler(createPermissionHandler(id))
.messageHandler(createMessageHandler(id))
.pointers(builder -> buildPointers(id, builder))
.create();
}

Expand All @@ -48,4 +50,6 @@ public final Chatter createChatter(UUID id) {

protected abstract Chatter.MessageHandler createMessageHandler(UUID id);

protected void buildPointers(UUID id, Pointers.Builder pointers) {
}
}

0 comments on commit 6074eb9

Please sign in to comment.