com.github.messenger4j.webhook.factory.MessageReadEventFactory Maven / Gradle / Ivy
package com.github.messenger4j.webhook.factory;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_ID;
import static com.github.messenger4j.internal.gson.GsonUtil.Constants.PROP_READ;
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.Constants.PROP_WATERMARK;
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.MessageReadEvent;
import com.google.gson.JsonObject;
import java.time.Instant;
/**
* @author Max Grabenhorst
* @since 1.0.0
*/
final class MessageReadEventFactory implements BaseEventFactory {
@Override
public boolean isResponsible(JsonObject messagingEvent) {
return hasProperty(messagingEvent, PROP_READ);
}
@Override
public MessageReadEvent 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 Instant watermark =
getPropertyAsInstant(messagingEvent, PROP_READ, PROP_WATERMARK)
.orElseThrow(IllegalArgumentException::new);
return new MessageReadEvent(senderId, recipientId, timestamp, watermark);
}
}