it.auties.whatsapp.model.info.MessageIndexInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whatsappweb4j Show documentation
Show all versions of whatsappweb4j Show documentation
Standalone fully-featured Whatsapp Web API for Java and Kotlin
package it.auties.whatsapp.model.info;
import com.fasterxml.jackson.core.type.TypeReference;
import it.auties.whatsapp.model.contact.ContactJid;
import it.auties.whatsapp.util.Json;
import lombok.Builder;
import lombok.NonNull;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
/**
* An index that contains data about a setting change or an action
*
* @param type the type of the change
* @param chatJid the chat where the change happened
* @param messageId the nullable id of the message regarding the chane
* @param fromMe whether the change regards yourself
*/
@Builder
public record MessageIndexInfo(@NonNull String type, @NonNull Optional chatJid,
@NonNull Optional messageId, boolean fromMe) implements Info {
/**
* Constructs a new message index info
*
* @param type the type of the change
* @param chatJid the chat where the change happened
* @param messageId the nullable id of the message regarding the chane
* @param fromMe whether the change regards yourself
* @return a non-null message index info
*/
public static MessageIndexInfo of(@NonNull String type, ContactJid chatJid, String messageId, boolean fromMe) {
return new MessageIndexInfo(type, Optional.ofNullable(chatJid), Optional.ofNullable(messageId), fromMe);
}
/**
* Constructs a new index info from a json string
*
* @param json the non-null json string
* @return a non-null index info
*/
public static MessageIndexInfo ofJson(@NonNull String json) {
var array = Json.readValue(json, new TypeReference>() {});
var type = getProperty(array, 0).orElseThrow(() -> new NoSuchElementException("Cannot parse MessageSync: missing type"));
var chatJid = getProperty(array, 1).map(ContactJid::of);
var messageId = getProperty(array, 2);
var fromMe = getProperty(array, 3).map(Boolean::parseBoolean).orElse(false);
return new MessageIndexInfo(type, chatJid, messageId, fromMe);
}
private static Optional getProperty(List list, int index) {
return list.size() > index ? Optional.ofNullable(list.get(index)) : Optional.empty();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy