Skip to content

Commit

Permalink
Changing from magic number to nullable argument
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmccloskey committed Nov 29, 2023
1 parent 702ae3e commit 3fd5535
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/meta/cp4m/message/FBMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class FBMessageHandler implements MessageHandler<FBMessage> {
private final String appSecret;

private final String accessToken;
private final String owningPageID;
@Nullable private final String owningPageID;

private final Deduplicator<Identifier> messageDeduplicator = new Deduplicator<>(10_000);
private Function<Identifier, URI> baseURLFactory =
Expand All @@ -63,7 +63,7 @@ public class FBMessageHandler implements MessageHandler<FBMessage> {
}
};

public FBMessageHandler(String verifyToken, String pageAccessToken, String appSecret, String owningPageID) {
public FBMessageHandler(String verifyToken, String pageAccessToken, String appSecret, @Nullable String owningPageID) {
this.verifyToken = verifyToken;
this.appSecret = appSecret;
this.accessToken = pageAccessToken;
Expand All @@ -74,7 +74,7 @@ public FBMessageHandler(String verifyToken, String pageAccessToken, String appSe
this.verifyToken = verifyToken;
this.appSecret = appSecret;
this.accessToken = pageAccessToken;
this.owningPageID = "-1";
this.owningPageID = null;
}

FBMessageHandler(FBMessengerConfig config) {
Expand Down Expand Up @@ -220,7 +220,7 @@ private void send(String message, Identifier recipient, Identifier sender) throw
try {
bodyString = MAPPER.writeValueAsString(body);
url =
new URIBuilder(baseURLFactory.apply(owningPageID.equals("-1") ? sender : Identifier.from(owningPageID)))
new URIBuilder(baseURLFactory.apply(owningPageID == null ? sender : Identifier.from(owningPageID)))
.addParameter("access_token", accessToken)
.build();
} catch (JsonProcessingException | URISyntaxException e) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/meta/cp4m/message/FBMessengerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.UUID;
Expand All @@ -20,14 +21,15 @@ public class FBMessengerConfig implements HandlerConfig {
private final String verifyToken;
private final String appSecret;
private final String pageAccessToken;
@Nullable
private final String owningPageID;

private FBMessengerConfig(
@JsonProperty("name") String name,
@JsonProperty("verify_token") String verifyToken,
@JsonProperty("app_secret") String appSecret,
@JsonProperty("page_access_token") String pageAccessToken,
@JsonProperty("owning_page_id") String owningPageID) {
@JsonProperty("owning_page_id") @Nullable String owningPageID) {

Preconditions.checkArgument(name != null && !name.isBlank(), "name cannot be blank");
Preconditions.checkArgument(
Expand All @@ -41,10 +43,10 @@ private FBMessengerConfig(
this.verifyToken = verifyToken;
this.appSecret = appSecret;
this.pageAccessToken = pageAccessToken;
this.owningPageID = owningPageID == null || owningPageID.isBlank() ? "-1" : owningPageID;
this.owningPageID = owningPageID;
}

public static FBMessengerConfig of(String verifyToken, String appSecret, String pageAccessToken, String owningPageID) {
public static FBMessengerConfig of(String verifyToken, String appSecret, String pageAccessToken, @Nullable String owningPageID) {
// human readability of the name only matters when it's coming from a config
return new FBMessengerConfig(
UUID.randomUUID().toString(), verifyToken, appSecret, pageAccessToken, owningPageID);
Expand All @@ -53,7 +55,7 @@ public static FBMessengerConfig of(String verifyToken, String appSecret, String
public static FBMessengerConfig of(String verifyToken, String appSecret, String pageAccessToken) {
// human readability of the name only matters when it's coming from a config
return new FBMessengerConfig(
UUID.randomUUID().toString(), verifyToken, appSecret, pageAccessToken, "-1");
UUID.randomUUID().toString(), verifyToken, appSecret, pageAccessToken, null);
}

@Override
Expand Down

0 comments on commit 3fd5535

Please sign in to comment.