com.github.messenger4j.webhook.factory.MessageEchoEventFactory Maven / Gradle / Ivy
package com.github.messenger4j.webhook.factory;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_APP_ID;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_ID;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_IS_ECHO;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_MESSAGE;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_METADATA;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_MID;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_RECIPIENT;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_SENDER;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_TIMESTAMP;
import static com.github.messenger4j.internal.gson.GsonUtil.getPropertyAsInstant;
import static com.github.messenger4j.internal.gson.GsonUtil.getPropertyAsString;
import static com.github.messenger4j.internal.gson.GsonUtil.hasProperty;
import com.github.messenger4j.webhook.event.MessageEchoEvent;
import com.google.gson.JsonObject;
import java.time.Instant;
import java.util.Optional;
/**
* @author Max Grabenhorst
* @since 1.0.0
*/
final class MessageEchoEventFactory implements BaseEventFactory {
@Override
public boolean isResponsible(JsonObject messagingEvent) {
return hasProperty(messagingEvent, PROP_MESSAGE, PROP_IS_ECHO);
}
@Override
public MessageEchoEvent createEventFromJson(JsonObject messagingEvent) {
final String senderId =
getPropertyAsString(messagingEvent, PROP_SENDER, PROP_ID)
.orElseThrow(IllegalArgumentException::new);
final String recipientId =
getPropertyAsString(messagingEvent, PROP_RECIPIENT, PROP_ID)
.orElseThrow(IllegalArgumentException::new);
final Instant timestamp =
getPropertyAsInstant(messagingEvent, PROP_TIMESTAMP)
.orElseThrow(IllegalArgumentException::new);
final String messageId =
getPropertyAsString(messagingEvent, PROP_MESSAGE, PROP_MID)
.orElseThrow(IllegalArgumentException::new);
final String appId =
getPropertyAsString(messagingEvent, PROP_MESSAGE, PROP_APP_ID)
.orElseThrow(IllegalArgumentException::new);
final Optional metadata =
getPropertyAsString(messagingEvent, PROP_MESSAGE, PROP_METADATA);
return new MessageEchoEvent(senderId, recipientId, timestamp, messageId, appId, metadata);
}
}