Load chats after startup

This commit is contained in:
Andrea Cavalli 2022-07-12 17:53:52 +02:00
parent bf5281dfd1
commit e31131b62c
5 changed files with 97 additions and 9 deletions

View File

@ -13,7 +13,7 @@ import java.nio.file.Paths;
/**
* Example class for TDLight Java
* <p>
* The documentation of the TDLight functions can be found here: https://tdlight-team.github.io/tdlight-docs
* <a href="https://tdlight-team.github.io/tdlight-docs">The documentation of the TDLight functions can be found here</a>
*/
public final class Example {

View File

@ -1,11 +1,13 @@
package it.tdlight.client;
import it.tdlight.common.TelegramClient;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.AuthorizationStateReady;
import it.tdlight.jni.TdApi.GetMe;
import it.tdlight.jni.TdApi.UpdateAuthorizationState;
import it.tdlight.jni.TdApi.User;
import it.tdlight.jni.TdApi.Error;
import it.tdlight.jni.TdApi.UserTypeRegular;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -15,11 +17,16 @@ final class AuthorizationStateReadyGetMe implements GenericUpdateHandler<UpdateA
private static final Logger logger = LoggerFactory.getLogger(AuthorizationStateReadyGetMe.class);
private final TelegramClient client;
private final AtomicReference<User> me;
private final AtomicReference<User> me = new AtomicReference<>();
private final AuthorizationStateReadyLoadChats mainChatsLoader;
private final AuthorizationStateReadyLoadChats archivedChatsLoader;
public AuthorizationStateReadyGetMe(TelegramClient client, AtomicReference<User> me) {
public AuthorizationStateReadyGetMe(TelegramClient client,
AuthorizationStateReadyLoadChats mainChatsLoader,
AuthorizationStateReadyLoadChats archivedChatsLoader) {
this.client = client;
this.me = me;
this.mainChatsLoader = mainChatsLoader;
this.archivedChatsLoader = archivedChatsLoader;
}
@Override
@ -30,7 +37,15 @@ final class AuthorizationStateReadyGetMe implements GenericUpdateHandler<UpdateA
throw new TelegramError((Error) me);
}
this.me.set((User) me);
if (((User) me).type.getConstructor() == UserTypeRegular.CONSTRUCTOR) {
mainChatsLoader.onUpdate(update);
archivedChatsLoader.onUpdate(update);
}
}, error -> logger.warn("Failed to execute TdApi.GetMe()"));
}
}
public User getMe() {
return me.get();
}
}

View File

@ -0,0 +1,52 @@
package it.tdlight.client;
import it.tdlight.common.TelegramClient;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.AuthorizationStateReady;
import it.tdlight.jni.TdApi.ChatList;
import it.tdlight.jni.TdApi.ChatListMain;
import it.tdlight.jni.TdApi.Error;
import it.tdlight.jni.TdApi.GetMe;
import it.tdlight.jni.TdApi.LoadChats;
import it.tdlight.jni.TdApi.UpdateAuthorizationState;
import it.tdlight.jni.TdApi.User;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final class AuthorizationStateReadyLoadChats implements GenericUpdateHandler<UpdateAuthorizationState> {
private static final Logger logger = LoggerFactory.getLogger(AuthorizationStateReadyLoadChats.class);
private final TelegramClient client;
private final ChatList chatList;
private boolean loaded;
public AuthorizationStateReadyLoadChats(TelegramClient client, ChatList chatList) {
this.client = client;
this.chatList = chatList;
}
@Override
public void onUpdate(UpdateAuthorizationState update) {
if (update.authorizationState.getConstructor() == AuthorizationStateReady.CONSTRUCTOR) {
client.send(new LoadChats(chatList, 2000), ok -> {
if (ok.getConstructor() == Error.CONSTRUCTOR) {
var error = (Error) ok;
if (error.code != 404) {
throw new TelegramError((Error) ok);
}
logger.debug("All {} chats have already been loaded", chatList);
} else {
logger.debug("All chats have been loaded");
}
}, error -> logger.warn("Failed to execute TdApi.LoadChats()"));
}
}
public boolean isLoaded() {
return loaded;
}
}

View File

@ -12,6 +12,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -21,11 +22,11 @@ final class CommandsHandler implements GenericUpdateHandler<UpdateNewMessage> {
private final TelegramClient client;
private final Map<String, Set<CommandHandler>> commandHandlers;
private final AtomicReference<User> me;
private final Supplier<User> me;
public CommandsHandler(TelegramClient client,
Map<String, Set<CommandHandler>> commandHandlers,
AtomicReference<User> me) {
Supplier<User> me) {
this.client = client;
this.commandHandlers = commandHandlers;
this.me = me;

View File

@ -10,6 +10,8 @@ import it.tdlight.common.internal.CommonClientManager;
import it.tdlight.common.utils.CantLoadLibrary;
import it.tdlight.common.utils.LibraryVersion;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.ChatListArchive;
import it.tdlight.jni.TdApi.ChatListMain;
import it.tdlight.jni.TdApi.Function;
import it.tdlight.jni.TdApi.User;
import java.io.IOException;
@ -56,6 +58,10 @@ public final class SimpleTelegramClient implements Authenticable {
private final Set<ExceptionHandler> defaultExceptionHandlers = new ConcurrentHashMap<ExceptionHandler, Object>().keySet(
new Object());
private final AuthorizationStateReadyGetMe meGetter;
private final AuthorizationStateReadyLoadChats mainChatsLoader;
private final AuthorizationStateReadyLoadChats archivedChatsLoader;
private final CountDownLatch closed = new CountDownLatch(1);
public SimpleTelegramClient(TDLibSettings settings) {
@ -92,9 +98,11 @@ public final class SimpleTelegramClient implements Authenticable {
)
);
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class, new AuthorizationStateWaitForExit(this.closed));
AtomicReference<User> me = new AtomicReference<>();
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class, new AuthorizationStateReadyGetMe(client, me));
this.addUpdateHandler(TdApi.UpdateNewMessage.class, new CommandsHandler(client, this.commandHandlers, me));
this.mainChatsLoader = new AuthorizationStateReadyLoadChats(client, new ChatListMain());
this.archivedChatsLoader = new AuthorizationStateReadyLoadChats(client, new ChatListArchive());
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
this.meGetter = new AuthorizationStateReadyGetMe(client, mainChatsLoader, archivedChatsLoader));
this.addUpdateHandler(TdApi.UpdateNewMessage.class, new CommandsHandler(client, this.commandHandlers, this::getMe));
}
private void handleUpdate(TdApi.Object update) {
@ -284,4 +292,16 @@ public final class SimpleTelegramClient implements Authenticable {
}
}
}
public User getMe() {
return meGetter.getMe();
}
public boolean isMainChatsListLoaded() {
return mainChatsLoader.isLoaded();
}
public boolean isArchivedChatsListLoaded() {
return archivedChatsLoader.isLoaded();
}
}