Search in sources :

Example 6 with ChatResponse

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

the class ChatCore method handleUnBlock.

private void handleUnBlock(ChatMessage chatMessage, String messageUniqueId) {
    BlockedContact contact = gson.fromJson(chatMessage.getContent(), BlockedContact.class);
    ChatResponse<ResultBlock> chatResponse = new ChatResponse<>();
    ResultBlock resultBlock = new ResultBlock();
    resultBlock.setContact(contact);
    chatResponse.setResult(resultBlock);
    chatResponse.setErrorCode(0);
    chatResponse.setHasError(false);
    chatResponse.setUniqueId(chatMessage.getUniqueId());
    String jsonUnBlock = gson.toJson(chatResponse);
    if (sentryResponseLog) {
        showLog("RECEIVE_UN_BLOCK", jsonUnBlock);
    } else {
        showLog("RECEIVE_UN_BLOCK");
    }
    messageCallbacks.remove(messageUniqueId);
    if (cache) {
        dataSource.deleteBlockedContactById(contact.getBlockId());
    }
    listenerManager.callOnUnBlock(jsonUnBlock, chatResponse);
}
Also used : BlockedContact(com.fanap.podchat.mainmodel.BlockedContact) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultBlock(com.fanap.podchat.model.ResultBlock)

Example 7 with ChatResponse

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

the class ChatCore method reformatGetThreadsResponseForMutual.

/**
 * Reformat the get thread response
 */
private ChatResponse<ResultThreads> reformatGetThreadsResponseForMutual(ChatMessage chatMessage, long userId) {
    ChatResponse<ResultThreads> outPutThreads = new ChatResponse<>();
    ArrayList<Thread> threads = gson.fromJson(chatMessage.getContent(), new TypeToken<ArrayList<Thread>>() {
    }.getType());
    if (cache) {
        // get threads summary shouldn't update cache
        if (!handlerSend.containsKey(chatMessage.getUniqueId())) {
            // messageDatabaseHelper.saveThreads(threads);
            dataSource.saveThreadResultFromServer(threads);
            dataSource.saveMutualThreadResultFromServer(threads, userId);
        }
    }
    ResultThreads resultThreads = new ResultThreads();
    resultThreads.setThreads(threads);
    resultThreads.setContentCount(chatMessage.getContentCount());
    outPutThreads.setErrorCode(0);
    outPutThreads.setErrorMessage("");
    outPutThreads.setHasError(false);
    outPutThreads.setUniqueId(chatMessage.getUniqueId());
    outPutThreads.setResult(resultThreads);
    return outPutThreads;
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) ResultThreads(com.fanap.podchat.model.ResultThreads) ChatResponse(com.fanap.podchat.model.ChatResponse) RequestCreateThread(com.fanap.podchat.requestobject.RequestCreateThread) ResultPinThread(com.fanap.podchat.chat.pin.pin_thread.model.ResultPinThread) Thread(com.fanap.podchat.mainmodel.Thread) RequestLeaveThread(com.fanap.podchat.requestobject.RequestLeaveThread) HandlerThread(android.os.HandlerThread) PinThread(com.fanap.podchat.chat.pin.pin_thread.PinThread) ResultLeaveThread(com.fanap.podchat.model.ResultLeaveThread) RequestMuteThread(com.fanap.podchat.requestobject.RequestMuteThread) ResultJoinPublicThread(com.fanap.podchat.chat.thread.public_thread.ResultJoinPublicThread) RequestJoinPublicThread(com.fanap.podchat.chat.thread.public_thread.RequestJoinPublicThread) PublicThread(com.fanap.podchat.chat.thread.public_thread.PublicThread) RequestThread(com.fanap.podchat.requestobject.RequestThread) ResultThread(com.fanap.podchat.model.ResultThread) RequestPinThread(com.fanap.podchat.chat.pin.pin_thread.model.RequestPinThread)

Example 8 with ChatResponse

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

the class ChatCore method addGroupContacts.

private PublishSubject<List<PhoneContact>> addGroupContacts(List<PhoneContact> phoneContacts, String uniqueId) {
    PublishSubject<List<PhoneContact>> subject = PublishSubject.create();
    ArrayList<String> firstNames = new ArrayList<>();
    ArrayList<String> cellphoneNumbers = new ArrayList<>();
    ArrayList<String> lastNames = new ArrayList<>();
    ArrayList<String> typeCodes = new ArrayList<>();
    ArrayList<String> uniqueIds = new ArrayList<>();
    ArrayList<String> emails = new ArrayList<>();
    for (PhoneContact contact : phoneContacts) {
        firstNames.add(contact.getName());
        lastNames.add(contact.getLastName());
        uniqueIds.add(generateUniqueId());
        String phoneNum = String.valueOf(contact.getPhoneNumber());
        if (phoneNum.startsWith("9")) {
            phoneNum = phoneNum.replaceFirst("9", "09");
        }
        cellphoneNumbers.add(phoneNum);
        emails.add("");
        typeCodes.add(getTypeCode());
    }
    Observable<Response<Contacts>> addContactsObservable;
    if (getPlatformHost() != null) {
        if (!Util.isNullOrEmpty(getTypeCode())) {
            addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers, typeCodes);
        } else {
            addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers);
        }
        showLog("Call add contacts " + new Date());
        addContactsObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(contactsResponse -> {
            showLog(">>> Server Respond " + new Date());
            boolean error = false;
            if (contactsResponse.body() != null) {
                error = contactsResponse.body().getHasError();
                showLog(">>>Response: " + contactsResponse.code());
                showLog(">>>ReferenceNumber: " + contactsResponse.body().getReferenceNumber());
                showLog(">>>Ott: " + contactsResponse.body().getOtt());
            }
            if (contactsResponse.isSuccessful()) {
                if (error) {
                    captureError(contactsResponse.body().getMessage(), contactsResponse.body().getErrorCode(), uniqueId);
                    showLog("Error add Contacts: " + contactsResponse.body().getMessage());
                    subject.onError(new Throwable(contactsResponse.body().getMessage()));
                } else {
                    // successful response
                    Contacts contacts = contactsResponse.body();
                    ChatResponse<Contacts> chatResponse = new ChatResponse<>();
                    chatResponse.setResult(contacts);
                    chatResponse.setUniqueId(uniqueId);
                    Runnable updatePhoneContactsDBTask = () -> {
                        try {
                            boolean result = phoneContactDbHelper.addPhoneContacts(phoneContacts);
                            if (!result) {
                                disableCache(() -> phoneContactDbHelper.addPhoneContacts(phoneContacts));
                            }
                        } catch (Exception e) {
                            showErrorLog("Updating Contacts cache failed: " + e.getMessage());
                            onUnknownException(uniqueId, e);
                        }
                    };
                    Runnable updateCachedContactsTask = () -> {
                        if (cache) {
                            try {
                                // messageDatabaseHelper.saveContacts(chatResponse.getResult().getResult(), getExpireAmount());
                                dataSource.saveContactsResultFromServer(chatResponse.getResult().getResult());
                            } catch (Exception e) {
                                showErrorLog("Saving Contacts Failed: " + e.getMessage());
                                onUnknownException(uniqueId, e);
                            }
                        }
                    };
                    new PodThreadManager().addNewTask(updatePhoneContactsDBTask).addNewTask(updateCachedContactsTask).addNewTask(() -> subject.onNext(phoneContacts)).runTasksSynced();
                }
            } else {
                captureError(contactsResponse.message(), contactsResponse.code(), uniqueId);
                showLog("Error add Contacts: " + contactsResponse.raw());
                subject.onError(new Throwable(contactsResponse.message()));
            }
        }, throwable -> {
            captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId);
            subject.onError(throwable);
        });
    }
    return subject;
}
Also used : PhoneContact(com.fanap.podchat.cachemodel.PhoneContact) PodThreadManager(com.fanap.podchat.util.PodThreadManager) ArrayList(java.util.ArrayList) Date(java.util.Date) 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) ChatResponse(com.fanap.podchat.model.ChatResponse) Response(retrofit2.Response) Contacts(com.fanap.podchat.model.Contacts) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultBlockList(com.fanap.podchat.model.ResultBlockList) RequestGetMentionList(com.fanap.podchat.chat.mention.model.RequestGetMentionList) RequestDeliveredMessageList(com.fanap.podchat.requestobject.RequestDeliveredMessageList) ArrayList(java.util.ArrayList) RequestBlockList(com.fanap.podchat.requestobject.RequestBlockList) RequestGetHashTagList(com.fanap.podchat.chat.hashtag.model.RequestGetHashTagList) RequestSeenMessageList(com.fanap.podchat.requestobject.RequestSeenMessageList) List(java.util.List)

Example 9 with ChatResponse

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

the class ChatCore method loadParticipantsFromCache.

@SuppressWarnings("unchecked")
private void loadParticipantsFromCache(String uniqueId, int count, int offset, long threadId) {
    try {
        messageDatabaseHelper.getThreadParticipant(offset, count, threadId, (obj, listData) -> {
            if (listData != null) {
                List<Participant> participantsList = (List<Participant>) listData;
                long participantCount = (long) obj;
                ChatResponse<ResultParticipant> chatResponse = new ChatResponse<>();
                ResultParticipant resultParticipant = new ResultParticipant();
                resultParticipant.setContentCount(participantsList.size());
                resultParticipant.setHasNext(participantsList.size() + offset < participantCount);
                resultParticipant.setParticipants(participantsList);
                chatResponse.setResult(resultParticipant);
                chatResponse.setCache(true);
                chatResponse.setUniqueId(uniqueId);
                resultParticipant.setNextOffset(offset + participantsList.size());
                String jsonParticipant = gson.toJson(chatResponse);
                listenerManager.callOnGetThreadParticipant(jsonParticipant, chatResponse);
                showLog("PARTICIPANT FROM CACHE", jsonParticipant);
            }
        });
    } catch (RoomIntegrityException e) {
        disableCache();
    }
}
Also used : ResultAddParticipant(com.fanap.podchat.model.ResultAddParticipant) ResultParticipant(com.fanap.podchat.model.ResultParticipant) Participant(com.fanap.podchat.mainmodel.Participant) CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) RequestThreadParticipant(com.fanap.podchat.requestobject.RequestThreadParticipant) OutPutParticipant(com.fanap.podchat.model.OutPutParticipant) ResultParticipant(com.fanap.podchat.model.ResultParticipant) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultBlockList(com.fanap.podchat.model.ResultBlockList) RequestGetMentionList(com.fanap.podchat.chat.mention.model.RequestGetMentionList) RequestDeliveredMessageList(com.fanap.podchat.requestobject.RequestDeliveredMessageList) ArrayList(java.util.ArrayList) RequestBlockList(com.fanap.podchat.requestobject.RequestBlockList) RequestGetHashTagList(com.fanap.podchat.chat.hashtag.model.RequestGetHashTagList) RequestSeenMessageList(com.fanap.podchat.requestobject.RequestSeenMessageList) List(java.util.List)

Example 10 with ChatResponse

use of com.fanap.podchat.model.ChatResponse 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)

Aggregations

ChatResponse (com.fanap.podchat.model.ChatResponse)162 ChatListener (com.fanap.podchat.chat.ChatListener)38 Test (org.junit.Test)38 ArrayList (java.util.ArrayList)35 Thread (com.fanap.podchat.mainmodel.Thread)29 JsonSyntaxException (com.google.gson.JsonSyntaxException)29 ResultHistory (com.fanap.podchat.model.ResultHistory)28 LargeTest (android.support.test.filters.LargeTest)27 RequestThread (com.fanap.podchat.requestobject.RequestThread)27 PodChatException (com.fanap.podchat.util.PodChatException)26 MessageVO (com.fanap.podchat.mainmodel.MessageVO)25 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)23 IOException (java.io.IOException)23 JSONException (org.json.JSONException)22 ResultThreads (com.fanap.podchat.model.ResultThreads)21 RequestGetHistory (com.fanap.podchat.requestobject.RequestGetHistory)21 SentryException (io.sentry.core.protocol.SentryException)21 FlakyTest (android.support.test.filters.FlakyTest)19 MediumTest (android.support.test.filters.MediumTest)19 Activity (android.app.Activity)17