Search in sources :

Example 31 with AsyncMessage

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

the class ChatCore method deleteMessage.

/**
 * It deletes message from the thread
 *
 * @param messageId    Id of the message that you want to be removed.
 * @param deleteForAll If you want to delete message for everyone you can set it true if u don't want
 *                     you can set it false or even null.
 */
private String deleteMessage(Long messageId, Boolean deleteForAll, ChatHandler handler) {
    // Log.d(MTAG, "Single Message delete");
    String uniqueId;
    uniqueId = generateUniqueId();
    if (chatReady) {
        deleteForAll = deleteForAll != null ? deleteForAll : false;
        AsyncMessage asyncMessage = MessageManager.generateDeleteMessageRequest(deleteForAll, uniqueId, messageId, getTypeCode(), getToken());
        sendAsyncMessage(gson.toJson(asyncMessage), AsyncAckType.Constants.WITHOUT_ACK, "SEND_DELETE_MESSAGE");
        setCallBacks(null, null, null, true, Constants.DELETE_MESSAGE, null, uniqueId);
        if (handler != null) {
            handler.onDeleteMessage(uniqueId);
        }
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    }
    return uniqueId;
}
Also used : AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage)

Example 32 with AsyncMessage

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

the class ChatCore method getContactMain.

private String getContactMain(int count, long offset, String username, boolean syncContact, String typeCode, boolean useCache, ChatHandler handler) {
    String uniqueId = generateUniqueId();
    int mCount = count > 0 ? count : 50;
    Runnable serverRequestTask = () -> {
        if (chatReady) {
            ChatMessageContent chatMessageContent = new ChatMessageContent();
            chatMessageContent.setOffset(offset);
            JsonObject jObj = (JsonObject) gson.toJsonTree(chatMessageContent);
            jObj.remove("lastMessageId");
            jObj.remove("firstMessageId");
            jObj.remove("count");
            jObj.addProperty("size", mCount);
            if (username != null)
                jObj.addProperty("username", username);
            AsyncMessage chatMessage = new AsyncMessage();
            chatMessage.setContent(jObj.toString());
            chatMessage.setType(Constants.GET_CONTACTS);
            chatMessage.setToken(getToken());
            chatMessage.setUniqueId(uniqueId);
            chatMessage.setTypeCode(Util.isNotNullOrEmpty(typeCode) ? typeCode : getTypeCode());
            chatMessage.setTokenIssuer("1");
            JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
            String asyncContent = jsonObject.toString();
            setCallBacks(null, null, null, !syncContact, Constants.GET_CONTACTS, offset, uniqueId);
            sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "GET_CONTACT_SEND");
            if (handler != null) {
                handler.onGetContact(uniqueId);
            }
        } else {
            captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
        }
    };
    if (cache && useCache) {
        dataSource.getContactData(count, offset, username).doOnCompleted(serverRequestTask::run).doOnError(exception -> {
            if (exception instanceof RoomIntegrityException) {
                disableCache();
            } else {
                captureError(exception.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId);
            }
        }).onErrorResumeNext(Observable.empty()).subscribe(response -> {
            if (response != null && Util.isNotNullOrEmpty(response.getContactsList())) {
                showLog("SOURCE: " + response.getSource());
                publishContactResult(uniqueId, offset, new ArrayList<>(response.getContactsList()), (int) response.getContentCount());
            }
        });
    } else {
        serverRequestTask.run();
    }
    return uniqueId;
}
Also used : AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) JsonObject(com.google.gson.JsonObject) ChatMessageContent(com.fanap.podchat.mainmodel.ChatMessageContent)

Example 33 with AsyncMessage

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

the class ChatCore method searchContact.

// public String searchContact(RequestSearchContact requestSearchContact) {
// 
// String uniqueId = generateUniqueId();
// 
// Runnable cacheLoading = () -> {
// 
// if (cache && requestSearchContact.canUseCache()) {
// List<Contact> contacts = new ArrayList<>();
// if (requestSearchContact.getId() != null) {
// Contact contact = messageDatabaseHelper.getContactById(Long.parseLong(requestSearchContact.getId()));
// contacts.add(contact);
// } else if (requestSearchContact.getFirstName() != null) {
// contacts = messageDatabaseHelper.getContactsByFirst(requestSearchContact.getFirstName());
// } else if (requestSearchContact.getFirstName() != null && requestSearchContact.getLastName() != null && !requestSearchContact.getFirstName().isEmpty() && !requestSearchContact.getLastName().isEmpty()) {
// contacts = messageDatabaseHelper.getContactsByFirstAndLast(requestSearchContact.getFirstName(), requestSearchContact.getLastName());
// } else if (requestSearchContact.getEmail() != null && !requestSearchContact.getEmail().isEmpty()) {
// contacts = messageDatabaseHelper.getContactsByEmail(requestSearchContact.getEmail());
// } else if (requestSearchContact.getCellphoneNumber() != null && !requestSearchContact.getCellphoneNumber().isEmpty()) {
// contacts = messageDatabaseHelper.getContactByCell(requestSearchContact.getCellphoneNumber());
// }
// 
// ChatResponse<ResultContact> chatResponse = new ChatResponse<>();
// chatResponse.setCache(true);
// 
// ResultContact resultContact = new ResultContact();
// ArrayList<Contact> listContact = new ArrayList<>(contacts);
// resultContact.setContacts(listContact);
// chatResponse.setUniqueId(uniqueId);
// chatResponse.setHasError(false);
// chatResponse.setErrorCode(0);
// chatResponse.setErrorMessage("");
// chatResponse.setResult(resultContact);
// 
// String jsonContact = gson.toJson(chatResponse);
// listenerManager.callOnSearchContact(jsonContact, chatResponse);
// showLog("CACHE_SEARCH_CONTACT", jsonContact);
// 
// 
// }
// };
// 
// Runnable requestServer = () -> {
// if (chatReady) {
// 
// JsonObject jsonObject = (JsonObject) gson.toJsonTree(requestSearchContact);
// 
// jsonObject.addProperty("uniqueId", uniqueId);
// 
// jsonObject.addProperty("tokenIssuer", 1);
// 
// showLog("SEND_SEARCH_CONTACT", getJsonForLog(jsonObject));
// 
// 
// Observable<Response<SearchContactVO>> observable = contactApi.searchContact(
// getToken(),
// TOKEN_ISSUER,
// requestSearchContact.getId()
// , requestSearchContact.getFirstName()
// , requestSearchContact.getLastName()
// , requestSearchContact.getEmail()
// , null
// , requestSearchContact.getOffset()
// , requestSearchContact.getSize()
// , null
// , requestSearchContact.getQuery()
// , requestSearchContact.getCellphoneNumber());
// 
// observable
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe(contactResponse -> {
// 
// 
// if (contactResponse.isSuccessful()) {
// 
// if (contactResponse.body() != null && contactResponse.body().getResult() != null) {
// 
// ArrayList<Contact> contacts = new ArrayList<>(contactResponse.body().getResult());
// 
// ResultContact resultContacts = new ResultContact();
// resultContacts.setContacts(contacts);
// 
// ChatResponse<ResultContact> chatResponse = new ChatResponse<>();
// chatResponse.setUniqueId(uniqueId);
// chatResponse.setResult(resultContacts);
// 
// String content = gson.toJson(chatResponse);
// 
// 
// showLog("RECEIVE_SEARCH_CONTACT", content);
// 
// listenerManager.callOnSearchContact(content, chatResponse);
// 
// }
// 
// } else {
// 
// if (contactResponse.body() != null) {
// String errorMessage = contactResponse.body().getMessage() != null ? contactResponse.body().getMessage() : "";
// int errorCode = contactResponse.body().getErrorCode() != null ? contactResponse.body().getErrorCode() : 0;
// getErrorOutPut(errorMessage, errorCode, uniqueId);
// }
// }
// 
// }, (Throwable throwable) -> Log.e(TAG, throwable.getMessage()));
// } else {
// getErrorOutPut(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
// }
// };
// 
// 
// new PodThreadManager()
// .addNewTask(cacheLoading)
// .addNewTask(requestServer)
// .runTasksSynced();
// 
// return uniqueId;
// }
public String searchContact(RequestSearchContact requestSearchContact) {
    String uniqueId = generateUniqueId();
    String size = !Util.isNullOrEmpty(requestSearchContact.getSize()) && !requestSearchContact.getSize().equals("0") ? requestSearchContact.getSize() : "50";
    String offset = !Util.isNullOrEmpty(requestSearchContact.getOffset()) ? requestSearchContact.getOffset() : "0";
    String order = !Util.isNullOrEmpty(requestSearchContact.getOrder()) ? requestSearchContact.getOrder() : "asc";
    Runnable cacheLoading = () -> {
        if (cache && requestSearchContact.canUseCache()) {
            ChatResponse<ResultContact> chatResponse = messageDatabaseHelper.searchContacts(requestSearchContact, size, offset);
            chatResponse.setUniqueId(uniqueId);
            String jsonContact = gson.toJson(chatResponse);
            listenerManager.callOnSearchContact(jsonContact, chatResponse);
            showLog("CACHE_SEARCH_CONTACT", jsonContact);
        }
    };
    Runnable requestServer = () -> {
        if (chatReady) {
            JsonObject jObj = new JsonObject();
            String query = requestSearchContact.getQuery();
            if (Util.isNullOrEmpty(query)) {
                query = "";
                if (!Util.isNullOrEmpty(requestSearchContact.getFirstName())) {
                    query += requestSearchContact.getFirstName() + " ";
                }
                if (!Util.isNullOrEmpty(requestSearchContact.getLastName())) {
                    query += requestSearchContact.getLastName();
                }
            }
            if (!Util.isNullOrEmpty(query)) {
                jObj.addProperty("query", query);
            }
            if (!Util.isNullOrEmpty(requestSearchContact.getEmail())) {
                jObj.addProperty("email", requestSearchContact.getEmail());
            }
            if (!Util.isNullOrEmpty(requestSearchContact.getCellphoneNumber())) {
                jObj.addProperty("cellphoneNumber", requestSearchContact.getCellphoneNumber());
            }
            if (!Util.isNullOrEmpty(requestSearchContact.getId())) {
                jObj.addProperty("id", requestSearchContact.getId());
            }
            jObj.addProperty("size", size);
            jObj.addProperty("offset", offset);
            jObj.addProperty("order", order);
            AsyncMessage chatMessage = new AsyncMessage();
            chatMessage.setContent(jObj.toString());
            chatMessage.setType(Constants.GET_CONTACTS);
            chatMessage.setToken(getToken());
            chatMessage.setUniqueId(uniqueId);
            chatMessage.setTypeCode(!Util.isNullOrEmpty(typeCode) ? typeCode : getTypeCode());
            chatMessage.setTokenIssuer(String.valueOf(TOKEN_ISSUER));
            JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
            String asyncContent = jsonObject.toString();
            setCallBacks(null, null, null, true, Constants.GET_CONTACTS, Long.valueOf(offset), uniqueId);
            handlerSend.put(uniqueId, new ChatHandler() {

                @Override
                public void onGetContact(String contactJson, ChatResponse<ResultContact> chatResponse) {
                    super.onGetContact(contactJson, chatResponse);
                    showLog("RECEIVE_SEARCH_CONTACT", contactJson);
                    listenerManager.callOnSearchContact(contactJson, chatResponse);
                    handlerSend.remove(uniqueId);
                }
            });
            sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "SEND_SEARCH_CONTACT");
        } else {
            captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
        }
    };
    new PodThreadManager().addNewTask(cacheLoading).addNewTask(requestServer).runTasksSynced();
    return uniqueId;
}
Also used : PodThreadManager(com.fanap.podchat.util.PodThreadManager) AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage) ChatResponse(com.fanap.podchat.model.ChatResponse) JsonObject(com.google.gson.JsonObject)

Example 34 with AsyncMessage

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

the class ChatCore method updateThreadInfo.

/**
 * It updates the information of the thread like
 * image;
 * name;
 * description;
 * metadata;
 */
private String updateThreadInfo(long threadId, ThreadInfoVO threadInfoVO, ChatHandler handler) {
    String uniqueId;
    uniqueId = generateUniqueId();
    try {
        if (chatReady) {
            JsonObject jObj = new JsonObject();
            jObj.addProperty("name", threadInfoVO.getTitle());
            jObj.addProperty("description", threadInfoVO.getDescription());
            jObj.addProperty("metadata", threadInfoVO.getMetadata());
            jObj.addProperty("image", threadInfoVO.getImage());
            String content = jObj.toString();
            AsyncMessage chatMessage = new AsyncMessage();
            chatMessage.setContent(content);
            chatMessage.setTokenIssuer("1");
            chatMessage.setToken(getToken());
            chatMessage.setSubjectId(threadId);
            chatMessage.setUniqueId(uniqueId);
            chatMessage.setType(Constants.UPDATE_THREAD_INFO);
            JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
            jsonObject.remove("contentCount");
            jsonObject.remove("systemMetadata");
            jsonObject.remove("metadata");
            jsonObject.remove("repliedTo");
            if (Util.isNullOrEmpty(typeCode)) {
                if (Util.isNullOrEmpty(getTypeCode())) {
                    jsonObject.remove("typeCode");
                } else {
                    jsonObject.addProperty("typeCode", getTypeCode());
                }
            } else {
                jsonObject.addProperty("typeCode", typeCode);
            }
            setCallBacks(null, null, null, true, Constants.UPDATE_THREAD_INFO, null, uniqueId);
            sendAsyncMessage(jsonObject.toString(), AsyncAckType.Constants.WITHOUT_ACK, "SEND_UPDATE_THREAD_INFO");
            if (handler != null) {
                handler.onUpdateThreadInfo(uniqueId);
            }
        } else {
            captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
        }
    } catch (Exception e) {
        showErrorLog(e.getMessage());
        onUnknownException(uniqueId, e);
    }
    return uniqueId;
}
Also used : AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage) JsonObject(com.google.gson.JsonObject) 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 35 with AsyncMessage

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

the class ChatCore method pingForCheckConnection.

private void pingForCheckConnection() {
    AsyncMessage chatMessage = new AsyncMessage();
    chatMessage.setType(Constants.PING);
    chatMessage.setTokenIssuer("1");
    chatMessage.setToken(getToken());
    connectionPing = true;
    String asyncContent = gson.toJson(chatMessage);
    async.sendMessage(asyncContent, AsyncAckType.Constants.WITH_ACK);
    showLog("..:: SEND CHAT PING FOR CHECK CONNECTION ::..", asyncContent);
}
Also used : AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage)

Aggregations

AsyncMessage (com.fanap.podchat.mainmodel.AsyncMessage)78 JsonObject (com.google.gson.JsonObject)53 ChatMessage (com.fanap.podchat.mainmodel.ChatMessage)20 PodChatException (com.fanap.podchat.util.PodChatException)7 JsonElement (com.google.gson.JsonElement)6 JsonArray (com.google.gson.JsonArray)5 ChatMessageContent (com.fanap.podchat.mainmodel.ChatMessageContent)4 ArrayList (java.util.ArrayList)4 SendClientDTO (com.fanap.podchat.call.model.SendClientDTO)3 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)3 PodThreadManager (com.fanap.podchat.util.PodThreadManager)3 CreateCallVO (com.fanap.podchat.call.model.CreateCallVO)2 TurnCallParticipantVideoOffRequest (com.fanap.podchat.call.request_model.TurnCallParticipantVideoOffRequest)2 Invitee (com.fanap.podchat.mainmodel.Invitee)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 TypeToken (com.google.gson.reflect.TypeToken)2 SentryException (io.sentry.core.protocol.SentryException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 JSONException (org.json.JSONException)2