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);
}
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);
}
}
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);
}
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);
}
}
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);
}
Aggregations