Search in sources :

Example 1 with ResultUserInfo

use of com.fanap.podchat.model.ResultUserInfo in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method getUserInfo.

/**
 * It Gets the information of the current user
 */
public String getUserInfo(ChatHandler handler) {
    String uniqueId = generateUniqueId();
    Runnable cacheLoading = () -> {
        if (permit && cache && handler != null) {
            try {
                UserInfo userInfo = messageDatabaseHelper.getUserInfo();
                if (userInfo != null) {
                    ChatResponse<ResultUserInfo> chatResponse = new ChatResponse<>();
                    ResultUserInfo result = new ResultUserInfo();
                    setUserId(userInfo.getId());
                    result.setUser(userInfo);
                    chatResponse.setResult(result);
                    chatResponse.setCache(true);
                    chatResponse.setUniqueId(uniqueId);
                    String userInfoJson = gson.toJson(chatResponse);
                    listenerManager.callOnUserInfo(userInfoJson, chatResponse);
                    showLog("CACHE_USER_INFO", userInfoJson);
                }
            } catch (Exception e) {
                showErrorLog(e.getMessage());
                onUnknownException(uniqueId, e);
            }
        }
    };
    Runnable requestServer = () -> {
        if (asyncReady) {
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.setType(Constants.USER_INFO);
            chatMessage.setUniqueId(uniqueId);
            chatMessage.setToken(getToken());
            chatMessage.setTokenIssuer("1");
            setCallBacks(null, null, null, true, Constants.USER_INFO, null, uniqueId);
            JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
            if (Util.isNullOrEmpty(getTypeCode())) {
                jsonObject.remove("typeCode");
            } else {
                jsonObject.remove("typeCode");
                jsonObject.addProperty("typeCode", getTypeCode());
            }
            String asyncContent = jsonObject.toString();
            showLog("SEND_USER_INFO", asyncContent);
            async.sendMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK);
            if (handler != null) {
                handler.onGetUserInfo(uniqueId);
            }
        }
    };
    new PodThreadManager().addNewTask(cacheLoading).addNewTask(requestServer).runTasksSynced();
    return uniqueId;
}
Also used : PodThreadManager(com.fanap.podchat.util.PodThreadManager) ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) ChatResponse(com.fanap.podchat.model.ChatResponse) JsonObject(com.google.gson.JsonObject) ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) UserInfo(com.fanap.podchat.mainmodel.UserInfo) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException)

Example 2 with ResultUserInfo

use of com.fanap.podchat.model.ResultUserInfo in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method handleOnGetUserInfo.

/**
 * Its check the Failed Queue {@link #checkMessageQueue()} to send all the message that is waiting to be send.
 */
private void handleOnGetUserInfo(ChatMessage chatMessage, String messageUniqueId, Callback callback) {
    if (callback.isResult()) {
        // if there is a key its ok if not it will go for the key and then chat ready
        ChatResponse<ResultUserInfo> chatResponse = new ChatResponse<>();
        UserInfo userInfo = gson.fromJson(chatMessage.getContent(), UserInfo.class);
        setUserId(userInfo.getId());
        setChatReady("CHAT_READY", true);
        userInfoResponse = true;
        // add user info for sentry
        setSentryUser(userInfo);
        String userInfoJson = reformatUserInfo(chatMessage, chatResponse, userInfo);
        if (sentryResponseLog) {
            showLog("RECEIVE_USER_INFO", userInfoJson);
        } else {
            showLog("RECEIVE_USER_INFO");
        }
        pingWithDelay();
        messageCallbacks.remove(messageUniqueId);
        listenerManager.callOnUserInfo(userInfoJson, chatResponse);
    }
}
Also used : ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) UserInfo(com.fanap.podchat.mainmodel.UserInfo)

Example 3 with ResultUserInfo

use of com.fanap.podchat.model.ResultUserInfo in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method reformatUserInfo.

private String reformatUserInfo(ChatMessage chatMessage, ChatResponse<ResultUserInfo> outPutUserInfo, UserInfo userInfo) {
    ResultUserInfo result = new ResultUserInfo();
    if (cache && permit) {
        messageDatabaseHelper.saveUserInfo(userInfo);
    // messageDatabaseHelper.saveUserInfo(userInfo, handleDBError(() -> {
    // 
    // messageDatabaseHelper.saveUserInfo(userInfo);
    // }, () -> {
    // }));
    }
    setUserId(userInfo.getId());
    result.setUser(userInfo);
    outPutUserInfo.setErrorCode(0);
    outPutUserInfo.setErrorMessage("");
    outPutUserInfo.setHasError(false);
    outPutUserInfo.setResult(result);
    outPutUserInfo.setUniqueId(chatMessage.getUniqueId());
    return gson.toJson(outPutUserInfo);
}
Also used : ResultUserInfo(com.fanap.podchat.model.ResultUserInfo)

Example 4 with ResultUserInfo

use of com.fanap.podchat.model.ResultUserInfo in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method getUserInfo.

/**
 * It Gets the information of the current user
 * <p>
 * set useCache = false to disable cache for only this function
 */
public String getUserInfo(boolean useCache, ChatHandler handler) {
    String uniqueId = generateUniqueId();
    Runnable cacheLoading = () -> {
        if (cache && useCache) {
            try {
                UserInfo userInfo = messageDatabaseHelper.getUserInfo();
                if (userInfo != null) {
                    ChatResponse<ResultUserInfo> chatResponse = new ChatResponse<>();
                    ResultUserInfo result = new ResultUserInfo();
                    setUserId(userInfo.getId());
                    result.setUser(userInfo);
                    chatResponse.setResult(result);
                    chatResponse.setCache(true);
                    chatResponse.setUniqueId(uniqueId);
                    String userInfoJson = gson.toJson(chatResponse);
                    listenerManager.callOnUserInfo(userInfoJson, chatResponse);
                    showLog("CACHE_USER_INFO", userInfoJson);
                }
            } catch (Exception e) {
                showErrorLog(e.getMessage());
                onUnknownException(uniqueId, e);
            }
        }
    };
    Runnable requestServer = () -> {
        if (chatReady) {
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.setType(Constants.USER_INFO);
            chatMessage.setUniqueId(uniqueId);
            chatMessage.setToken(getToken());
            chatMessage.setTokenIssuer("1");
            setCallBacks(null, null, null, true, Constants.USER_INFO, null, uniqueId);
            JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
            if (Util.isNullOrEmpty(getTypeCode())) {
                jsonObject.remove("typeCode");
            } else {
                jsonObject.remove("typeCode");
                jsonObject.addProperty("typeCode", getTypeCode());
            }
            String asyncContent = jsonObject.toString();
            showLog("SEND_USER_INFO", asyncContent);
            async.sendMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK);
            if (handler != null) {
                handler.onGetUserInfo(uniqueId);
            }
        } else
            onChatNotReady(uniqueId);
    };
    new PodThreadManager().addNewTask(cacheLoading).addNewTask(requestServer).runTasksSynced();
    return uniqueId;
}
Also used : PodThreadManager(com.fanap.podchat.util.PodThreadManager) ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) ChatResponse(com.fanap.podchat.model.ChatResponse) JsonObject(com.google.gson.JsonObject) ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) UserInfo(com.fanap.podchat.mainmodel.UserInfo) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException)

Aggregations

ResultUserInfo (com.fanap.podchat.model.ResultUserInfo)4 UserInfo (com.fanap.podchat.mainmodel.UserInfo)3 ChatResponse (com.fanap.podchat.model.ChatResponse)3 ChatMessage (com.fanap.podchat.mainmodel.ChatMessage)2 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)2 PodChatException (com.fanap.podchat.util.PodChatException)2 PodThreadManager (com.fanap.podchat.util.PodThreadManager)2 JsonObject (com.google.gson.JsonObject)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 SentryException (io.sentry.core.protocol.SentryException)2 IOException (java.io.IOException)2 JSONException (org.json.JSONException)2