Ability to set time periods with Instant, Duration and ZonedDateTime

This commit is contained in:
Victor 2018-12-12 18:10:00 +02:00
parent f7bde89ec8
commit d091480f71
3 changed files with 49 additions and 4 deletions

View File

@ -1,5 +1,6 @@
package org.telegram.telegrambots.meta.api.methods.groupadministration;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
@ -9,6 +10,9 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
@ -81,11 +85,25 @@ public class KickChatMember extends BotApiMethod<Boolean> {
return untilDate;
}
public KickChatMember setUntilDate(Integer untilDate) {
this.untilDate = untilDate;
public KickChatMember setUntilDate(Integer untilDateInSeconds) {
this.untilDate = untilDateInSeconds;
return this;
}
@JsonIgnore
public KickChatMember setUntilDate(Instant instant) {
return setUntilDate((int) instant.getEpochSecond());
}
@JsonIgnore
public KickChatMember setUntilDate(ZonedDateTime date) {
return setUntilDate(date.toInstant());
}
public KickChatMember forTimePeriod(Duration duration) {
return setUntilDate(Instant.now().plusMillis(duration.toMillis()));
}
@Override
public String getMethod() {
return PATH;

View File

@ -1,5 +1,6 @@
package org.telegram.telegrambots.meta.api.methods.groupadministration;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
@ -8,6 +9,9 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
@ -89,11 +93,25 @@ public class RestrictChatMember extends BotApiMethod<Boolean> {
return untilDate;
}
public RestrictChatMember setUntilDate(Integer untilDate) {
this.untilDate = untilDate;
public RestrictChatMember setUntilDate(Integer untilDateInSeconds) {
this.untilDate = untilDateInSeconds;
return this;
}
@JsonIgnore
public RestrictChatMember setUntilDate(Instant instant) {
return setUntilDate((int) instant.getEpochSecond());
}
@JsonIgnore
public RestrictChatMember setUntilDate(ZonedDateTime date) {
return setUntilDate(date.toInstant());
}
public RestrictChatMember forTimePeriod(Duration duration) {
return setUntilDate(Instant.now().plusMillis(duration.toMillis()));
}
public Boolean getCanSendMessages() {
return canSendMessages;
}

View File

@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.telegram.telegrambots.meta.api.interfaces.BotApiObject;
import java.time.Instant;
/**
* @author Ruben Bermudez
* @version 1.0
@ -77,6 +79,13 @@ public class ChatMember implements BotApiObject {
return untilDate;
}
public Instant getUntilDateAsInstant() {
if (untilDate == null) {
return null;
}
return Instant.ofEpochSecond(untilDate);
}
public Boolean getCanBeEdited() {
return canBeEdited;
}