Search in sources :

Example 6 with SendingQueueCache

use of com.fanap.podchat.cachemodel.queue.SendingQueueCache in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method forwardMessage.

/**
 * forward message
 *
 * @param threadId   destination thread id
 * @param messageIds Array of message ids that we want to forward them
 */
public List<String> forwardMessage(long threadId, ArrayList<Long> messageIds) {
    ArrayList<String> uniqueIds = new ArrayList<>();
    ArrayList<Callback> callbacks = new ArrayList<>();
    for (long messageId : messageIds) {
        String uniqueId = generateUniqueId();
        uniqueIds.add(uniqueId);
        callbacks.add(MessageManager.generateCallback(uniqueId));
        /*
             * add to message Queue
             * */
        SendingQueueCache sendingQueue = MessageManager.generateSendingQueueCache(threadId, messageId, uniqueId, getTypeCode(), getToken());
        insertToSendQueue(uniqueId, sendingQueue);
    }
    if (log)
        Log.i(TAG, "Messages " + messageIds + "with this" + "uniqueIds" + uniqueIds + "has been added to Message Queue");
    if (chatReady) {
        threadCallbacks.put(threadId, callbacks);
        String jsonUniqueIds = Util.listToJson(uniqueIds, gson);
        String asyncContent = MessageManager.generateForwardMessage(threadId, messageIds.toString(), jsonUniqueIds, getTypeCode(), getToken());
        for (String uniqueId : uniqueIds) {
            moveFromSendingQueueToWaitQueue(uniqueId);
        }
        sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "SEND_FORWARD_MESSAGE");
    } else {
        if (Util.isNullOrEmpty(uniqueIds)) {
            for (String uniqueId : uniqueIds) {
                captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
            }
        }
    }
    return uniqueIds;
}
Also used : SendingQueueCache(com.fanap.podchat.cachemodel.queue.SendingQueueCache) Callback(com.fanap.podchat.util.Callback) ArrayList(java.util.ArrayList)

Example 7 with SendingQueueCache

use of com.fanap.podchat.cachemodel.queue.SendingQueueCache in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method checkMessageQueue.

/**
 * When chat is ready its checking the message that was stored in the Sending Queue and if there is any,
 * it's sent them to async
 */
private void checkMessageQueue() {
    try {
        if (log)
            showLog("checkMessageQueue");
        if (cache) {
            dataSource.getAllSendingQueue().doOnError(exception -> {
                handleException(exception);
                captureError(ChatConstant.ERROR_UNKNOWN_EXCEPTION, ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, "", exception);
            }).onErrorResumeNext(Observable.empty()).subscribe(sendingQueueCaches -> {
                if (!Util.isNullOrEmpty(sendingQueueCaches)) {
                    for (SendingQueueCache sendingQueue : sendingQueueCaches) {
                        String uniqueId = sendingQueue.getUniqueId();
                        long threadId = sendingQueue.getThreadId();
                        setThreadCallbacks(threadId, uniqueId);
                        dataSource.moveFromSendingToWaitingQueue(uniqueId);
                        JsonObject jsonObject = (new JsonParser()).parse(sendingQueue.getAsyncContent()).getAsJsonObject();
                        jsonObject.remove("token");
                        jsonObject.addProperty("token", getToken());
                        if (log)
                            Log.i(TAG, "checkMessageQueue");
                        String async = jsonObject.toString();
                        sendAsyncMessage(async, AsyncAckType.Constants.WITHOUT_ACK, "SEND_TEXT_MESSAGE_FROM_MESSAGE_QUEUE");
                    }
                }
            });
        // new PodThreadManager().doThisAndGo(() -> {
        // 
        // //                    List<SendingQueueCache> sendingQueueCaches = messageDatabaseHelper.getAllSendingQueue();
        // List<SendingQueueCache> sendingQueueCaches = dataSource.getAllSendingQueue();
        // 
        // if (!Util.isNullOrEmpty(sendingQueueCaches)) {
        // for (SendingQueueCache sendingQueue : sendingQueueCaches) {
        // String uniqueId = sendingQueue.getUniqueId();
        // long threadId = sendingQueue.getThreadId();
        // setThreadCallbacks(threadId, uniqueId);
        // 
        // //                            messageDatabaseHelper.insertWaitMessageQueue(sendingQueue);
        // //                            messageDatabaseHelper.deleteSendingMessageQueue(uniqueId);
        // 
        // dataSource.moveFromSendingToWaitingQueue(uniqueId);
        // 
        // JsonObject jsonObject = (new JsonParser()).parse(sendingQueue.getAsyncContent()).getAsJsonObject();
        // 
        // jsonObject.remove("token");
        // jsonObject.addProperty("token", getToken());
        // 
        // if (log) Log.i(TAG, "checkMessageQueue");
        // String async = jsonObject.toString();
        // sendAsyncMessage(async, AsyncAckType.Constants.WITHOUT_ACK, "SEND_TEXT_MESSAGE_FROM_MESSAGE_QUEUE");
        // }
        // }
        // });
        } else {
            if (!sendingQList.isEmpty()) {
                for (SendingQueueCache sendingQueue : sendingQList.values()) {
                    String uniqueId = sendingQueue.getUniqueId();
                    long threadId = sendingQueue.getThreadId();
                    setThreadCallbacks(threadId, uniqueId);
                    WaitQueueCache waitMessageQueue = getWaitQueueCacheFromSendQueue(sendingQueue, uniqueId);
                    waitQList.put(uniqueId, waitMessageQueue);
                    // sendingQList.remove(uniqueId);
                    JsonObject jsonObject = (new JsonParser()).parse(sendingQueue.getAsyncContent()).getAsJsonObject();
                    jsonObject.remove("token");
                    jsonObject.addProperty("token", getToken());
                    sendAsyncMessage(jsonObject.toString(), AsyncAckType.Constants.WITHOUT_ACK, "SEND_TEXT_MESSAGE_FROM_MESSAGE_QUEUE");
                }
                sendingQList.clear();
            }
        }
    } catch (Throwable throwable) {
        showErrorLog(throwable.getMessage());
        onUnknownException("", throwable);
    }
}
Also used : SendingQueueCache(com.fanap.podchat.cachemodel.queue.SendingQueueCache) JsonObject(com.google.gson.JsonObject) WaitQueueCache(com.fanap.podchat.cachemodel.queue.WaitQueueCache) JsonParser(com.google.gson.JsonParser)

Example 8 with SendingQueueCache

use of com.fanap.podchat.cachemodel.queue.SendingQueueCache in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method sendTextMessage.

/**
 * Send text message to the thread
 * All of the messages first send to Message Queue(Cache) and then send to chat server
 *
 * @param textMessage        String that we want to sent to the thread
 * @param threadId           Id of the destination thread
 * @param jsonSystemMetadata It should be Json,if you don't have metaData you can set it to "null"
 */
public String sendTextMessage(String textMessage, long threadId, Integer messageType, String jsonSystemMetadata, ChatHandler handler) {
    String asyncContentWaitQueue;
    String uniqueId;
    uniqueId = generateUniqueId();
    try {
        JsonObject jsonObject = (MessageManager.prepareSendTextMessageRequest(textMessage, threadId, messageType, jsonSystemMetadata, uniqueId, getTypeCode(), getToken()));
        SendingQueueCache sendingQueue = new SendingQueueCache();
        sendingQueue.setSystemMetadata(jsonSystemMetadata);
        sendingQueue.setMessageType(messageType);
        sendingQueue.setThreadId(threadId);
        sendingQueue.setUniqueId(uniqueId);
        sendingQueue.setMessage(textMessage);
        asyncContentWaitQueue = jsonObject.toString();
        sendingQueue.setAsyncContent(asyncContentWaitQueue);
        insertToSendQueue(uniqueId, sendingQueue);
        if (log) {
            Log.i(TAG, "Message with this" + "  uniqueId  " + uniqueId + "  has been added to Message Queue");
        }
        if (chatReady) {
            if (handler != null) {
                handler.onSent(uniqueId, threadId);
                handler.onSentResult(null);
                handlerSend.put(uniqueId, handler);
            }
            moveFromSendingQueueToWaitQueue(uniqueId, sendingQueue);
            setThreadCallbacks(threadId, uniqueId);
            sendAsyncMessage(asyncContentWaitQueue, AsyncAckType.Constants.WITHOUT_ACK, "SEND_TEXT_MESSAGE");
            stopTyping();
        } else {
            onChatNotReady(uniqueId);
        }
    } catch (Throwable throwable) {
        showErrorLog(throwable.getMessage());
        onUnknownException(uniqueId, throwable);
    }
    return uniqueId;
}
Also used : SendingQueueCache(com.fanap.podchat.cachemodel.queue.SendingQueueCache) JsonObject(com.google.gson.JsonObject)

Example 9 with SendingQueueCache

use of com.fanap.podchat.cachemodel.queue.SendingQueueCache in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method sendTextMessageWithFile.

private void sendTextMessageWithFile(String description, long threadId, String metaData, String systemMetadata, String uniqueId, String typeCode, Integer messageType) {
    /* Add to sending Queue*/
    SendingQueueCache sendingQueue = new SendingQueueCache();
    if (systemMetadata != null)
        sendingQueue.setSystemMetadata(systemMetadata);
    else
        sendingQueue.setSystemMetadata("");
    if (messageType != null)
        sendingQueue.setMessageType(messageType);
    else
        sendingQueue.setMessageType(0);
    sendingQueue.setThreadId(threadId);
    sendingQueue.setUniqueId(uniqueId);
    sendingQueue.setMessage(description);
    sendingQueue.setMetadata(metaData);
    ChatMessage chatMessage = new ChatMessage();
    chatMessage.setContent(description);
    chatMessage.setType(Constants.MESSAGE);
    chatMessage.setTokenIssuer("1");
    chatMessage.setToken(getToken());
    chatMessage.setMetadata(metaData);
    chatMessage.setSystemMetadata(systemMetadata);
    chatMessage.setMessageType(messageType);
    chatMessage.setUniqueId(uniqueId);
    chatMessage.setSubjectId(threadId);
    chatMessage.setTypeCode(typeCode != null ? typeCode : getTypeCode());
    JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
    if (Util.isNullOrEmpty(systemMetadata)) {
        jsonObject.remove("systemMetadata");
    }
    if (chatMessage.getRepliedTo() == 0)
        jsonObject.remove("repliedTo");
    if (chatMessage.getTime() == 0)
        jsonObject.remove("time");
    jsonObject.remove("contentCount");
    String asyncContent = jsonObject.toString();
    sendingQueue.setAsyncContent(asyncContent);
    insertToSendQueue(uniqueId, sendingQueue);
    if (chatReady) {
        setThreadCallbacks(threadId, uniqueId);
        moveFromSendingQueueToWaitQueue(uniqueId, sendingQueue);
        sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "SEND_TXT_MSG_WITH_FILE");
        stopTyping();
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    }
}
Also used : SendingQueueCache(com.fanap.podchat.cachemodel.queue.SendingQueueCache) ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) JsonObject(com.google.gson.JsonObject)

Example 10 with SendingQueueCache

use of com.fanap.podchat.cachemodel.queue.SendingQueueCache in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method resendMessage.

/**
 * If you want to resend the message that is stored in waitQueue
 *
 * @param uniqueId the unique id of the waitQueue message
 */
public void resendMessage(String uniqueId) {
    if (uniqueId != null) {
        if (cache) {
            dataSource.moveFromWaitingToSendingQueue(uniqueId).subscribe(sendQueueData -> {
                if (sendQueueData != null) {
                    setThreadCallbacks((sendQueueData).getThreadId(), uniqueId);
                    sendSendQueueMessage(uniqueId, sendQueueData);
                }
            });
        // messageDatabaseHelper.moveFromWaitQueueToSendQueue(uniqueId, (sendQueueData) -> {
        // 
        // SendingQueueCache sendingQueue = (SendingQueueCache) sendQueueData;
        // 
        // if (sendingQueue != null) {
        // setThreadCallbacks(sendingQueue.getThreadId(), uniqueId);
        // sendSendQueueMessage(uniqueId, sendingQueue);
        // }
        // 
        // 
        // });
        } else {
            SendingQueueCache sendingQueueCache = sendingQList.get(uniqueId);
            sendSendQueueMessage(uniqueId, sendingQueueCache);
        }
    }
}
Also used : SendingQueueCache(com.fanap.podchat.cachemodel.queue.SendingQueueCache)

Aggregations

SendingQueueCache (com.fanap.podchat.cachemodel.queue.SendingQueueCache)14 WaitQueueCache (com.fanap.podchat.cachemodel.queue.WaitQueueCache)4 JsonObject (com.google.gson.JsonObject)4 MessageVO (com.fanap.podchat.mainmodel.MessageVO)3 ArrayList (java.util.ArrayList)3 ChatMessage (com.fanap.podchat.mainmodel.ChatMessage)2 Activity (android.app.Activity)1 Context (android.content.Context)1 Looper (android.os.Looper)1 InstrumentationRegistry (android.support.test.InstrumentationRegistry)1 ActivityTestRule (android.support.test.rule.ActivityTestRule)1 AndroidJUnit4 (android.support.test.runner.AndroidJUnit4)1 BaseApplication (com.example.chat.application.chatexample.BaseApplication)1 ChatActivity (com.example.chat.application.chatexample.ChatActivity)1 APP_ID (com.example.chat.application.chatexample.ChatActivity.APP_ID)1 ChatContract (com.example.chat.application.chatexample.ChatContract)1 ChatPresenter (com.example.chat.application.chatexample.ChatPresenter)1 Sending (com.fanap.podchat.cachemodel.queue.Sending)1 Chat (com.fanap.podchat.chat.Chat)1 ChatListener (com.fanap.podchat.chat.ChatListener)1