Search in sources :

Example 96 with ChatResponse

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

the class ChatCore method publishChatHistoryServerResult.

private void publishChatHistoryServerResult(Callback callback, ChatMessage chatMessage, List<MessageVO> messageVOS) {
    ResultHistory resultHistory = new ResultHistory();
    if (cache) {
        // 
        // Observable.just(resultHistory)
        // .asObservable()
        // .map(resultHistoryEMP -> {
        // resultHistoryEMP.setSending(dataSource.getAllSendingQueueByThreadId(chatMessage.getSubjectId()));
        // return resultHistoryEMP;
        // })
        // .map(resultHistoryWithSending -> {
        // 
        // resultHistoryWithSending.setUploadingQueue(dataSource.getAllUploadingQueueByThreadId(chatMessage.getSubjectId()));
        // return  resultHistoryWithSending;
        // 
        // })
        // .map(resultHistoryWithUploading -> {
        // 
        // resultHistoryWithUploading.setFailed(dataSource.getAllWaitQueueCacheByThreadId(chatMessage.getSubjectId()));
        // return resultHistoryWithUploading;
        // 
        // });
        // resultHistory.setSending(messageDatabaseHelper.getAllSendingQueueByThreadId(chatMessage.getSubjectId()));
        // resultHistory.setUploadingQueue(messageDatabaseHelper.getAllUploadingQueueByThreadId(chatMessage.getSubjectId()));
        // resultHistory.setFailed(messageDatabaseHelper.getAllWaitQueueCacheByThreadId(chatMessage.getSubjectId()));
        // 
        resultHistory.setSending(dataSource.getAllSendingQueueByThreadId(chatMessage.getSubjectId()));
        resultHistory.setUploadingQueue(dataSource.getAllUploadingQueueByThreadId(chatMessage.getSubjectId()));
        resultHistory.setFailed(dataSource.getAllWaitQueueCacheByThreadId(chatMessage.getSubjectId()));
    }
    ChatResponse<ResultHistory> chatResponse = new ChatResponse<>();
    resultHistory.setNextOffset(callback.getOffset() + messageVOS.size());
    resultHistory.setContentCount(chatMessage.getContentCount());
    resultHistory.setHasNext(messageVOS.size() + callback.getOffset() < chatMessage.getContentCount());
    chatResponse.setSubjectId(chatMessage.getSubjectId());
    resultHistory.setHistory(messageVOS);
    chatResponse.setErrorCode(0);
    chatResponse.setHasError(false);
    chatResponse.setErrorMessage("");
    chatResponse.setResult(resultHistory);
    chatResponse.setUniqueId(chatMessage.getUniqueId());
    String json = gson.toJson(chatResponse);
    if (sentryResponseLog) {
        showLog("RECEIVE_GET_HISTORY", json);
    } else {
        showLog("RECEIVE_GET_HISTORY");
    }
    messageCallbacks.remove(chatMessage.getUniqueId());
    listenerManager.callOnGetThreadHistory(json, chatResponse);
}
Also used : ChatResponse(com.fanap.podchat.model.ChatResponse) ResultHistory(com.fanap.podchat.model.ResultHistory)

Example 97 with ChatResponse

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

the class ChatCore method mainUploadFileMessage.

private void mainUploadFileMessage(LFileUpload lFileUpload) {
    JsonObject jsonLog = new JsonObject();
    String description = lFileUpload.getDescription();
    jsonLog.addProperty("description", description);
    Integer messageType = lFileUpload.getMessageType();
    jsonLog.addProperty("messageType", messageType);
    long threadId = lFileUpload.getThreadId();
    jsonLog.addProperty("threadId", threadId);
    String uniqueId = lFileUpload.getUniqueId();
    jsonLog.addProperty("uniqueId", uniqueId);
    String systemMetadata = lFileUpload.getSystemMetaData();
    jsonLog.addProperty("systemMetadata", systemMetadata);
    long messageId = lFileUpload.getMessageId();
    jsonLog.addProperty("messageId", messageId);
    String mimeType = lFileUpload.getMimeType();
    jsonLog.addProperty("mimeType", mimeType);
    File file = lFileUpload.getFile();
    ProgressHandler.sendFileMessage handler = lFileUpload.getHandler();
    long file_size = lFileUpload.getFileSize();
    jsonLog.addProperty("file_size", file_size);
    String methodName = lFileUpload.getMethodName();
    jsonLog.addProperty("methodName", methodName);
    jsonLog.addProperty("name", file.getName());
    showLog("UPLOADING_FILE_TO_SERVER", getJsonForLog(jsonLog));
    if (chatReady) {
        if (getFileServer() != null) {
            RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(getFileServer());
            FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
            RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), file.getName());
            ProgressRequestBody requestFile = new ProgressRequestBody(file, mimeType, uniqueId, new ProgressRequestBody.UploadCallbacks() {

                @Override
                public void onProgress(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
                    if (handler != null)
                        handler.onProgressUpdate(uniqueId, progress, totalBytesSent, totalBytesToSend);
                }
            });
            MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
            Observable<Response<FileUpload>> uploadObservable = fileApi.sendFile(body, getToken(), TOKEN_ISSUER, name);
            Subscription subscription = uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(fileUploadResponse -> {
                if (fileUploadResponse.isSuccessful() && fileUploadResponse.body() != null) {
                    boolean error = fileUploadResponse.body().isHasError();
                    if (error) {
                        String errorMessage = fileUploadResponse.body().getMessage();
                        if (log)
                            Log.e(TAG, errorMessage);
                        if (handler != null) {
                            String jsonError = captureError(ChatConstant.ERROR_UPLOAD_FILE, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
                            ErrorOutPut errorOutPut = new ErrorOutPut(true, ChatConstant.ERROR_UPLOAD_FILE, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
                            handler.onError(jsonError, errorOutPut);
                        }
                    } else {
                        // remove from Uploading Queue
                        removeFromUploadQueue(uniqueId);
                        ResultFile result = fileUploadResponse.body().getResult();
                        if (result != null) {
                            long fileId = result.getId();
                            String hashCode = result.getHashCode();
                            ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
                            chatResponse.setResult(result);
                            chatResponse.setUniqueId(uniqueId);
                            result.setSize(file_size);
                            String json = gson.toJson(chatResponse);
                            showLog("FILE_UPLOADED_TO_SERVER", json);
                            listenerManager.callOnUploadFile(json, chatResponse);
                            if (handler != null) {
                                handler.onFinishFile(json, chatResponse);
                            }
                            String jsonMeta = createFileMetadata(file, hashCode, fileId, mimeType, file_size, "");
                            if (!Util.isNullOrEmpty(methodName) && methodName.equals(ChatConstant.METHOD_REPLY_MSG)) {
                                showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
                                mainReplyMessage(description, threadId, messageId, systemMetadata, messageType, jsonMeta, uniqueId, null);
                            } else {
                                sendTextMessageWithFile(description, threadId, jsonMeta, systemMetadata, uniqueId, typeCode, messageType);
                            }
                        }
                    }
                }
            }, throwable -> {
                if (log)
                    Log.e(TAG, throwable.getMessage());
            });
            /*
                 * Cancel Uploading progress
                 * */
            initCancelUpload(uniqueId, subscription);
        } else {
            if (log)
                Log.e(TAG, "FileServer url Is null");
        }
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    // listenerManager.callOnLogEvent(jsonError);
    }
}
Also used : ProgressHandler(com.fanap.podchat.ProgressHandler) JsonObject(com.google.gson.JsonObject) ResultFile(com.fanap.podchat.model.ResultFile) RetrofitHelperFileServer(com.fanap.podchat.networking.retrofithelper.RetrofitHelperFileServer) ChatResponse(com.fanap.podchat.model.ChatResponse) Response(retrofit2.Response) MultipartBody(okhttp3.MultipartBody) ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody) FileApi(com.fanap.podchat.networking.api.FileApi) RequestBody(okhttp3.RequestBody) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody)

Example 98 with ChatResponse

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

the class ChatCore method notifyChatHistoryReceived.

private void notifyChatHistoryReceived(Callback callback, ChatMessage chatMessage, List<MessageVO> messageVOS) {
    ChatResponse<ResultHistory> chr = new ChatResponse<>();
    ResultHistory rh = new ResultHistory();
    rh.setHistory(messageVOS);
    chr.setResult(rh);
    chr.setSubjectId(chatMessage.getSubjectId());
    chr.setUniqueId(chatMessage.getUniqueId());
    Objects.requireNonNull(handlerSend.get(chatMessage.getUniqueId())).onGetHistory(chr, chatMessage, callback);
}
Also used : ChatResponse(com.fanap.podchat.model.ChatResponse) ResultHistory(com.fanap.podchat.model.ResultHistory)

Example 99 with ChatResponse

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

the class ChatCore method handleSent.

private void handleSent(ChatMessage chatMessage, String messageUniqueId, long threadId) {
    if (cache) {
        dataSource.deleteWaitQueueWithUniqueId(messageUniqueId);
    } else {
        waitQList.remove(messageUniqueId);
    }
    boolean found = false;
    try {
        if (threadCallbacks.containsKey(threadId)) {
            ArrayList<Callback> callbacks = threadCallbacks.get(threadId);
            if (callbacks != null) {
                for (Callback callback : callbacks) {
                    if (messageUniqueId.equals(callback.getUniqueId())) {
                        if (callback.isSent()) {
                            found = true;
                            ChatResponse<ResultMessage> chatResponse = new ChatResponse<>();
                            ResultMessage resultMessage = new ResultMessage();
                            chatResponse.setErrorCode(0);
                            chatResponse.setErrorMessage("");
                            chatResponse.setHasError(false);
                            chatResponse.setUniqueId(callback.getUniqueId());
                            chatResponse.setSubjectId(chatMessage.getSubjectId());
                            resultMessage.setConversationId(chatMessage.getSubjectId());
                            try {
                                resultMessage.setMessageId(Long.parseLong(chatMessage.getContent()));
                            } catch (NumberFormatException e) {
                                captureError(new PodChatException(e.getMessage(), messageUniqueId, getToken()));
                                resultMessage.setMessageId(0);
                            }
                            chatResponse.setResult(resultMessage);
                            String json = gson.toJson(chatResponse);
                            listenerManager.callOnSentMessage(json, chatResponse);
                            runOnUIThread(() -> {
                                if (handlerSend.get(callback.getUniqueId()) != null) {
                                    ChatHandler handler = handlerSend.get(callback.getUniqueId());
                                    if (handler != null) {
                                        handler.onSentResult(chatMessage.getContent());
                                    }
                                }
                            });
                            Callback callbackUpdateSent = new Callback();
                            callbackUpdateSent.setSent(false);
                            callbackUpdateSent.setDelivery(callback.isDelivery());
                            callbackUpdateSent.setSeen(callback.isSeen());
                            callbackUpdateSent.setUniqueId(callback.getUniqueId());
                            callbacks.set(callbacks.indexOf(callback), callbackUpdateSent);
                            threadCallbacks.put(threadId, callbacks);
                            if (sentryResponseLog) {
                                showLog("RECEIVED_SENT_MESSAGE", json);
                            } else {
                                showLog("RECEIVED_SENT_MESSAGE");
                            }
                        }
                        break;
                    }
                }
            }
        }
        if (!found) {
            ChatResponse<ResultMessage> chatResponse = new ChatResponse<>();
            ResultMessage resultMessage = new ResultMessage();
            chatResponse.setErrorCode(0);
            chatResponse.setErrorMessage("");
            chatResponse.setHasError(false);
            chatResponse.setUniqueId(messageUniqueId);
            chatResponse.setSubjectId(chatMessage.getSubjectId());
            resultMessage.setConversationId(chatMessage.getSubjectId());
            resultMessage.setMessageId(Long.parseLong(chatMessage.getContent()));
            chatResponse.setResult(resultMessage);
            String json = gson.toJson(chatResponse);
            listenerManager.callOnSentMessage(json, chatResponse);
            // Log.d("MTAG", "threadid isn't in callbacks!");
            runOnUIThread(() -> {
                if (handlerSend.get(messageUniqueId) != null) {
                    handlerSend.get(messageUniqueId).onSentResult(chatMessage.getContent());
                }
            });
            Callback callbackUpdateSent = new Callback();
            callbackUpdateSent.setSent(false);
            callbackUpdateSent.setUniqueId(messageUniqueId);
            if (sentryResponseLog) {
                showLog("RECEIVED_SENT_MESSAGE", json);
            } else {
                showLog("RECEIVED_SENT_MESSAGE");
            }
        }
    } catch (Throwable e) {
        showErrorLog(e.getMessage());
        onUnknownException(chatMessage.getUniqueId(), e);
    }
}
Also used : Callback(com.fanap.podchat.util.Callback) PodChatException(com.fanap.podchat.util.PodChatException) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultMessage(com.fanap.podchat.model.ResultMessage)

Example 100 with ChatResponse

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

the class ChatCore method handleForwardMessage.

private void handleForwardMessage(ChatMessage chatMessage) {
    MessageVO messageVO = gson.fromJson(chatMessage.getContent(), MessageVO.class);
    ChatResponse<ResultNewMessage> chatResponse = new ChatResponse<>();
    ResultNewMessage resultMessage = new ResultNewMessage();
    resultMessage.setThreadId(chatMessage.getSubjectId());
    resultMessage.setMessageVO(messageVO);
    chatResponse.setResult(resultMessage);
    String json = gson.toJson(chatResponse);
    long ownerId = 0;
    if (messageVO != null) {
        ownerId = messageVO.getParticipant().getId();
    }
    if (sentryResponseLog) {
        showLog("RECEIVED_FORWARD_MESSAGE", json);
    } else {
        showLog("RECEIVED_FORWARD_MESSAGE");
    }
    if (ownerId != getUserId()) {
        ChatMessage message = null;
        if (messageVO != null) {
            message = getChatMessage(messageVO);
        }
        String asyncContent = gson.toJson(message);
        showLog("SEND_DELIVERY_MESSAGE", asyncContent);
        async.sendMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK);
    }
    if (cache) {
        dataSource.saveMessageResultFromServer(messageVO, chatMessage.getSubjectId());
    }
    listenerManager.callOnNewMessage(json, chatResponse);
}
Also used : ResultNewMessage(com.fanap.podchat.model.ResultNewMessage) ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) ChatResponse(com.fanap.podchat.model.ChatResponse) GapMessageVO(com.fanap.podchat.cachemodel.GapMessageVO) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) MessageVO(com.fanap.podchat.mainmodel.MessageVO)

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