Search in sources :

Example 36 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage 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)

Example 37 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage in project pod-chat-android-sdk by FanapSoft.

the class AssistantManager method createDeActiveAssistantRequest.

public static String createDeActiveAssistantRequest(DeActiveAssistantRequest request, String uniqueId) {
    String content = App.getGson().toJson(request.getAssistantVos());
    AsyncMessage message = new ChatMessage();
    message.setType(ChatMessageType.Constants.DEACTIVE_ASSISTANT);
    message.setToken(CoreConfig.token);
    message.setTokenIssuer(CoreConfig.tokenIssuer);
    message.setTypeCode(request.getTypeCode() != null ? request.getTypeCode() : CoreConfig.typeCode);
    message.setContent(content);
    message.setUniqueId(uniqueId);
    return App.getGson().toJson(message);
}
Also used : ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage)

Example 38 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage in project pod-chat-android-sdk by FanapSoft.

the class AssistantManager method createGetAssistantsRequest.

public static String createGetAssistantsRequest(GetAssistantRequest request, String uniqueId) {
    JsonObject content = new JsonObject();
    content.addProperty("contactType", request.getTypeCode() != null ? request.getTypeCode() : CoreConfig.typeCode);
    if ((Long) request.getOffset() != null) {
        content.addProperty("offset", request.getOffset());
    }
    if ((Long) request.getCount() != null) {
        content.addProperty("count", request.getCount());
    } else {
        content.addProperty("count", 50);
    }
    AsyncMessage message = new ChatMessage();
    message.setType(ChatMessageType.Constants.GET_ASSISTANTS);
    message.setToken(CoreConfig.token);
    message.setTokenIssuer(CoreConfig.tokenIssuer);
    message.setTypeCode(request.getTypeCode() != null ? request.getTypeCode() : CoreConfig.typeCode);
    message.setContent(App.getGson().toJson(content));
    message.setUniqueId(uniqueId);
    return App.getGson().toJson(message);
}
Also used : ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage) JsonObject(com.google.gson.JsonObject)

Example 39 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage in project pod-chat-android-sdk by FanapSoft.

the class AssistantManager method createUnBlockAssistantRequest.

public static String createUnBlockAssistantRequest(BlockUnblockAssistantRequest request, String uniqueId) {
    String content = App.getGson().toJson(request.getAssistantVos());
    AsyncMessage message = new ChatMessage();
    message.setType(ChatMessageType.Constants.UNBLOCK_ASSISTANT);
    message.setToken(CoreConfig.token);
    message.setTokenIssuer(CoreConfig.tokenIssuer);
    message.setTypeCode(request.getTypeCode() != null ? request.getTypeCode() : CoreConfig.typeCode);
    message.setContent(content);
    message.setUniqueId(uniqueId);
    return App.getGson().toJson(message);
}
Also used : ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage)

Example 40 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage in project pod-chat-android-sdk by FanapSoft.

the class AssistantManager method createRegisterAssistantRequest.

public static String createRegisterAssistantRequest(RegisterAssistantRequest request, String uniqueId) {
    String content = App.getGson().toJson(request.getAssistantVos());
    AsyncMessage message = new ChatMessage();
    message.setType(ChatMessageType.Constants.REGISTER_ASSISTANT);
    message.setToken(CoreConfig.token);
    message.setTokenIssuer(CoreConfig.tokenIssuer);
    message.setTypeCode(request.getTypeCode() != null ? request.getTypeCode() : CoreConfig.typeCode);
    message.setContent(content);
    message.setUniqueId(uniqueId);
    return App.getGson().toJson(message);
}
Also used : ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage)

Aggregations

ChatMessage (com.fanap.podchat.mainmodel.ChatMessage)56 JsonObject (com.google.gson.JsonObject)33 AsyncMessage (com.fanap.podchat.mainmodel.AsyncMessage)20 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)9 PodChatException (com.fanap.podchat.util.PodChatException)9 JsonSyntaxException (com.google.gson.JsonSyntaxException)9 SentryException (io.sentry.core.protocol.SentryException)9 IOException (java.io.IOException)9 JSONException (org.json.JSONException)9 ArrayList (java.util.ArrayList)5 ChatResponse (com.fanap.podchat.model.ChatResponse)4 JsonArray (com.google.gson.JsonArray)4 NonNull (android.support.annotation.NonNull)3 CacheMessageVO (com.fanap.podchat.cachemodel.CacheMessageVO)3 GapMessageVO (com.fanap.podchat.cachemodel.GapMessageVO)3 MessageVO (com.fanap.podchat.mainmodel.MessageVO)3 ResultNewMessage (com.fanap.podchat.model.ResultNewMessage)3 JsonElement (com.google.gson.JsonElement)3 TypeToken (com.google.gson.reflect.TypeToken)3 SendingQueueCache (com.fanap.podchat.cachemodel.queue.SendingQueueCache)2