Search in sources :

Example 11 with Callback

use of com.fanap.podchat.util.Callback 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 12 with Callback

use of com.fanap.podchat.util.Callback in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method addTagParticipant.

/**
 * Add user tags participant
 */
public String addTagParticipant(AddTagParticipantRequest request) {
    String uniqueId = generateUniqueId();
    if (chatReady) {
        String message = TagManager.createAddTagParticipantRequest(request, uniqueId);
        sendAsyncMessage(message, AsyncAckType.Constants.WITHOUT_ACK, "ADD_TAG_PARTICIPANT");
        Callback callbackTagParticipant = new Callback(request.getTagId());
        messageCallbacks.put(uniqueId, callbackTagParticipant);
    } else {
        onChatNotReady(uniqueId);
    }
    return uniqueId;
}
Also used : Callback(com.fanap.podchat.util.Callback)

Example 13 with Callback

use of com.fanap.podchat.util.Callback 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 14 with Callback

use of com.fanap.podchat.util.Callback in project pod-chat-android-sdk by FanapSoft.

the class MessageManager method generateCallback.

public static Callback generateCallback(String uniqueId) {
    Callback callback = new Callback();
    callback.setDelivery(true);
    callback.setSeen(true);
    callback.setSent(true);
    callback.setUniqueId(uniqueId);
    return callback;
}
Also used : Callback(com.fanap.podchat.util.Callback)

Example 15 with Callback

use of com.fanap.podchat.util.Callback in project pod-chat-android-sdk by FanapSoft.

the class DbTest method updateCacheFirstMsgIdAndLastMsgId1.

// first messsage and last message
// cache is not empty but server is empty
@Test
public void updateCacheFirstMsgIdAndLastMsgId1() {
    long threadId = 2;
    Callback callback = new Callback();
    List<MessageVO> messageVOS = new ArrayList<>();
    callback.setOffset(0);
    callback.setCount(50);
    callback.setOrder("asc");
    callback.setFirstMessageId(9234);
    callback.setLastMessageId(9235);
    messageDatabaseHelper.updateGetHistoryResponse(callback, messageVOS, threadId, null);
}
Also used : Callback(com.fanap.podchat.util.Callback) ArrayList(java.util.ArrayList) MessageVO(com.fanap.podchat.mainmodel.MessageVO) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) Test(org.junit.Test)

Aggregations

Callback (com.fanap.podchat.util.Callback)18 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 CacheMessageVO (com.fanap.podchat.cachemodel.CacheMessageVO)5 MessageVO (com.fanap.podchat.mainmodel.MessageVO)5 PodChatException (com.fanap.podchat.util.PodChatException)5 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 SentryException (io.sentry.core.protocol.SentryException)4 IOException (java.io.IOException)4 JSONException (org.json.JSONException)4 ChatResponse (com.fanap.podchat.model.ChatResponse)3 ResultMessage (com.fanap.podchat.model.ResultMessage)3 SendingQueueCache (com.fanap.podchat.cachemodel.queue.SendingQueueCache)1 ChatMessage (com.fanap.podchat.mainmodel.ChatMessage)1 Constants (com.fanap.podchat.util.ChatMessageType.Constants)1