tdlib-session-container/src/main/java/it/tdlight/reactiveapi/Event.java

79 lines
2.4 KiB
Java
Raw Normal View History

2021-12-05 15:15:28 +01:00
package it.tdlight.reactiveapi;
import it.tdlight.jni.TdApi;
2021-12-07 02:25:01 +01:00
import it.tdlight.reactiveapi.Event.ClientBoundEvent;
import it.tdlight.reactiveapi.Event.ServerBoundEvent;
import java.io.ByteArrayInputStream;
import java.io.DataInput;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import org.apache.commons.lang3.SerializationException;
2021-12-05 15:15:28 +01:00
/**
* Any event received from a session
*/
2021-12-07 02:25:01 +01:00
public sealed interface Event permits ClientBoundEvent, ServerBoundEvent {
2021-12-05 15:15:28 +01:00
/**
*
* @return temporary unique identifier of the session
*/
2021-12-07 02:25:01 +01:00
long liveId();
2021-12-05 15:15:28 +01:00
/**
* Event received after choosing the user id of the session
*/
2021-12-07 02:25:01 +01:00
sealed interface ClientBoundEvent extends Event permits OnLoginCodeRequested, OnOtherDeviceLoginRequested,
OnPasswordRequested, OnUpdate {
2021-12-05 15:15:28 +01:00
/**
*
* @return telegram user id of the session
*/
long userId();
}
2021-12-07 02:25:01 +01:00
sealed interface ServerBoundEvent extends Event permits Request {}
2021-12-05 15:15:28 +01:00
/**
* TDLib is asking for an authorization code
*/
2021-12-07 02:25:01 +01:00
sealed interface OnLoginCodeRequested extends ClientBoundEvent
2021-12-05 15:15:28 +01:00
permits OnBotLoginCodeRequested, OnUserLoginCodeRequested {}
2021-12-07 02:25:01 +01:00
record OnUserLoginCodeRequested(long liveId, long userId, long phoneNumber) implements OnLoginCodeRequested {}
record OnBotLoginCodeRequested(long liveId, long userId, String token) implements OnLoginCodeRequested {}
record OnOtherDeviceLoginRequested(long liveId, long userId) implements ClientBoundEvent {}
2021-12-05 15:15:28 +01:00
2021-12-07 02:25:01 +01:00
record OnPasswordRequested(long liveId, long userId) implements ClientBoundEvent {}
2021-12-05 15:15:28 +01:00
/**
* Event received from TDLib
*/
2021-12-07 02:25:01 +01:00
sealed interface OnUpdate extends ClientBoundEvent permits OnUpdateData, OnUpdateError {}
2021-12-05 15:15:28 +01:00
2021-12-07 02:25:01 +01:00
record OnUpdateData(long liveId, long userId, TdApi.Update update) implements OnUpdate {}
2021-12-05 15:15:28 +01:00
2021-12-07 02:25:01 +01:00
record OnUpdateError(long liveId, long userId, TdApi.Error error) implements OnUpdate {}
record Request<T extends TdApi.Object>(long liveId, TdApi.Function<T> request, Instant timeout) implements
ServerBoundEvent {
public static <T extends TdApi.Object> Request<T> deserialize(DataInput dataInput) {
try {
var liveId = dataInput.readLong();
@SuppressWarnings("unchecked")
TdApi.Function<T> request = (TdApi.Function<T>) TdApi.Deserializer.deserialize(dataInput);
long millis = dataInput.readLong();
var timeout = Instant.ofEpochMilli(millis);
return new Request<>(liveId, request, timeout);
} catch (IOException e) {
throw new SerializationException(e);
}
}
}
2021-12-05 15:15:28 +01:00
}