Search in sources :

Example 16 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method seenMessageList.

// Get the list of the participants that saw the specific message
/*
     * The replacement method is getMessageSeenList.
     * */
private String seenMessageList(RequestSeenMessageList requestParams) {
    String uniqueId;
    uniqueId = generateUniqueId();
    if (chatReady) {
        try {
            if (Util.isNullOrEmpty(requestParams.getCount())) {
                requestParams.setCount(50);
            }
            JsonObject object = (JsonObject) gson.toJsonTree(requestParams);
            object.remove("typeCode");
            ChatMessage chatMessage = new ChatMessage(uniqueId, Constants.SEEN_MESSAGE_LIST, object.toString(), getToken());
            String asyncContent = chatMessage.getJson(requestParams.getTypeCode(), getTypeCode()).toString();
            setCallBacks(null, null, null, true, Constants.SEEN_MESSAGE_LIST, requestParams.getOffset(), uniqueId);
            sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "SEND_SEEN_MESSAGE_LIST");
        } catch (Throwable e) {
            showErrorLog(e.getMessage());
            onUnknownException(uniqueId, e);
        }
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    }
    return uniqueId;
}
Also used : ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) JsonObject(com.google.gson.JsonObject)

Example 17 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method mainReplyMessage.

private String mainReplyMessage(String messageContent, long threadId, long messageId, String systemMetaData, Integer messageType, String metaData, String iUniqueId, ChatHandler handler) {
    String uniqueId = iUniqueId;
    if (iUniqueId == null)
        uniqueId = generateUniqueId();
    /* Add to sending Queue*/
    SendingQueueCache sendingQueue = new SendingQueueCache();
    sendingQueue.setSystemMetadata(systemMetaData);
    sendingQueue.setMessageType(messageType);
    sendingQueue.setThreadId(threadId);
    sendingQueue.setUniqueId(uniqueId);
    sendingQueue.setMessage(messageContent);
    sendingQueue.setMetadata(metaData);
    ChatMessage chatMessage = new ChatMessage();
    chatMessage.setUniqueId(uniqueId);
    chatMessage.setRepliedTo(messageId);
    chatMessage.setSubjectId(threadId);
    chatMessage.setTokenIssuer("1");
    chatMessage.setToken(getToken());
    chatMessage.setContent(messageContent);
    chatMessage.setMetadata(metaData);
    chatMessage.setSystemMetadata(systemMetaData);
    chatMessage.setType(Constants.MESSAGE);
    chatMessage.setTypeCode(getTypeCode());
    chatMessage.setMessageType(messageType);
    JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
    if (Util.isNullOrEmpty(systemMetaData)) {
        jsonObject.remove("systemMetaData");
    }
    if (Util.isNullOrEmpty(metaData)) {
        jsonObject.remove("metadata");
    }
    jsonObject.remove("time");
    String asyncContent = jsonObject.toString();
    sendingQueue.setAsyncContent(asyncContent);
    insertToSendQueue(uniqueId, sendingQueue);
    if (log)
        Log.i(TAG, "Message with this" + "uniqueId" + uniqueId + "has been added to Message Queue");
    if (chatReady) {
        moveFromSendingQueueToWaitQueue(uniqueId, sendingQueue);
        setThreadCallbacks(threadId, uniqueId);
        sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "SEND_REPLY_MESSAGE");
        stopTyping();
        if (handler != null) {
            handler.onReplyMessage(uniqueId);
        }
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    }
    return uniqueId;
}
Also used : SendingQueueCache(com.fanap.podchat.cachemodel.queue.SendingQueueCache) ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) JsonObject(com.google.gson.JsonObject)

Example 18 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method createThreadWithMessage.

/**
 * Create the thread with message is just for  p to p.( Thread Type is int NORMAL = 0)
 *
 * @return The first UniqueId is for create thread and the rest of them are for new message or forward messages
 * Its have three kind of Unique Ids. One of them is for message. One of them for Create Thread
 * and the others for Forward Message or Messages.
 * <p>
 * int messageType  Type of the Thread (You can have Thread Type from ThreadType.class)
 * String ownerSsoId  [Optional]
 * List<Invitee> invitees  you can add your invite list here
 * String title  [Optional] title of the group thread
 * <p>
 * RequestThreadInnerMessage message{  object of the inner message
 * <p>
 * -------------  String text  text of the message
 * -------------  int messageType  messageType of the message  [Optional]
 * -------------  String metadata  [Optional]
 * -------------  String systemMetadata  [Optional]
 * -------------  List<Long> forwardedMessageIds  [Optional]
 * }
 */
public ArrayList<String> createThreadWithMessage(RequestCreateThread threadRequest) {
    List<String> forwardUniqueIds;
    JsonObject innerMessageObj;
    JsonObject jsonObject;
    String threadUniqueId = generateUniqueId();
    ArrayList<String> uniqueIds = new ArrayList<>();
    uniqueIds.add(threadUniqueId);
    try {
        if (chatReady) {
            RequestThreadInnerMessage innerMessage = threadRequest.getMessage();
            innerMessageObj = (JsonObject) gson.toJsonTree(innerMessage);
            if (Util.isNullOrEmpty(threadRequest.getMessage().getType())) {
                innerMessageObj.remove("type");
            }
            if (Util.isNullOrEmpty(threadRequest.getMessage().getText())) {
                innerMessageObj.remove("message");
            } else {
                String newMsgUniqueId = generateUniqueId();
                innerMessageObj.addProperty("uniqueId", newMsgUniqueId);
                uniqueIds.add(newMsgUniqueId);
                setCallBacks(true, true, true, true, Constants.MESSAGE, null, newMsgUniqueId);
            }
            if (!Util.isNullOrEmptyNumber(threadRequest.getMessage().getForwardedMessageIds())) {
                /**
                 * Its generated new unique id for each forward message
                 */
                List<Long> messageIds = threadRequest.getMessage().getForwardedMessageIds();
                forwardUniqueIds = new ArrayList<>();
                for (long ids : messageIds) {
                    String frwMsgUniqueId = generateUniqueId();
                    forwardUniqueIds.add(frwMsgUniqueId);
                    uniqueIds.add(frwMsgUniqueId);
                    setCallBacks(true, true, true, true, Constants.MESSAGE, null, frwMsgUniqueId);
                }
                JsonElement element = gson.toJsonTree(forwardUniqueIds, new TypeToken<List<Long>>() {
                }.getType());
                JsonArray jsonArray = element.getAsJsonArray();
                innerMessageObj.add("forwardedUniqueIds", jsonArray);
            } else {
                innerMessageObj.remove("forwardedUniqueIds");
                innerMessageObj.remove("forwardedMessageIds");
            }
            JsonObject jsonObjectCreateThread = (JsonObject) gson.toJsonTree(threadRequest);
            jsonObjectCreateThread.remove("count");
            jsonObjectCreateThread.remove("offset");
            jsonObjectCreateThread.add("message", innerMessageObj);
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.setContent(jsonObjectCreateThread.toString());
            chatMessage.setType(Constants.INVITATION);
            chatMessage.setUniqueId(threadUniqueId);
            chatMessage.setToken(getToken());
            chatMessage.setTokenIssuer("1");
            jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
            jsonObject.remove("repliedTo");
            jsonObject.remove("subjectId");
            jsonObject.remove("systemMetadata");
            jsonObject.remove("contentCount");
            String typeCode = threadRequest.getTypeCode();
            if (Util.isNullOrEmpty(typeCode)) {
                if (Util.isNullOrEmpty(getTypeCode())) {
                    jsonObject.remove("typeCode");
                } else {
                    jsonObject.addProperty("typeCode", getTypeCode());
                }
            } else {
                jsonObject.addProperty("typeCode", typeCode);
            }
            setCallBacks(null, null, null, true, Constants.INVITATION, null, threadUniqueId);
            if (threadRequest.getUploadThreadImageRequest() != null) {
                handlerSend.put(threadUniqueId, new ChatHandler() {

                    @Override
                    public void onThreadCreated(ResultThread thread) {
                        super.onThreadCreated(thread);
                        updateThreadImage(thread, threadRequest.getUploadThreadImageRequest());
                    }
                });
            }
            sendAsyncMessage(jsonObject.toString(), AsyncAckType.Constants.WITHOUT_ACK, "SEND_CREATE_THREAD_WITH_MESSAGE");
        } else {
            captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, threadUniqueId);
        }
    } catch (Throwable e) {
        showErrorLog(e.getMessage());
        onUnknownException(uniqueIds.get(0), e);
    }
    return uniqueIds;
}
Also used : ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) RequestThreadInnerMessage(com.fanap.podchat.mainmodel.RequestThreadInnerMessage) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) ResultThread(com.fanap.podchat.model.ResultThread)

Example 19 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method seenMessage.

/**
 * In order to send seen message you have to call this method
 */
public String seenMessage(long messageId, long ownerId, ChatHandler handler) {
    String uniqueId;
    uniqueId = generateUniqueId();
    if (chatReady) {
        if (ownerId != getUserId()) {
            ChatMessage message = new ChatMessage();
            message.setType(Constants.SEEN);
            message.setContent(String.valueOf(messageId));
            message.setTokenIssuer("1");
            message.setToken(getToken());
            message.setUniqueId(uniqueId);
            message.setTime(1000);
            JsonObject jsonObject = (JsonObject) gson.toJsonTree(message);
            if (Util.isNullOrEmpty(getTypeCode())) {
                jsonObject.remove("typeCode");
            } else {
                jsonObject.remove("typeCode");
                jsonObject.addProperty("typeCode", getTypeCode());
            }
            jsonObject.remove("contentCount");
            jsonObject.remove("systemMetadata");
            jsonObject.remove("metadata");
            jsonObject.remove("repliedTo");
            String asyncContent = jsonObject.toString();
            sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "SEND_SEEN_MESSAGE");
            if (handler != null) {
                handler.onSeen(uniqueId);
            }
        }
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    }
    return uniqueId;
}
Also used : ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) JsonObject(com.google.gson.JsonObject)

Example 20 with ChatMessage

use of com.fanap.podchat.mainmodel.ChatMessage in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method getAllThreads.

private void getAllThreads() {
    if (cache) {
        String uniqueId = generateUniqueId();
        JsonObject content = new JsonObject();
        content.addProperty("summary", true);
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setContent(content.toString());
        chatMessage.setToken(getToken());
        chatMessage.setType(Constants.GET_THREADS);
        chatMessage.setTypeCode(getTypeCode());
        chatMessage.setUniqueId(uniqueId);
        ChatHandler handler = new ChatHandler() {

            @Override
            public void onGetThread(String data) {
                super.onGetThread(data);
                ArrayList<ResultThreadsSummary> result = gson.fromJson(data, new TypeToken<ArrayList<ResultThreadsSummary>>() {
                }.getType());
                ArrayList<Long> serverResultThreadIds = new ArrayList<>();
                for (ResultThreadsSummary rts : result) {
                    serverResultThreadIds.add((long) rts.getId());
                }
                messageDatabaseHelper.getThreadIdsList(t -> {
                    try {
                        if (t == null)
                            return;
                        List<Long> threadsIdsInCache = (List<Long>) t;
                        if (threadsIdsInCache.size() > 0) {
                            threadsIdsInCache.removeAll(serverResultThreadIds);
                            if (serverResultThreadIds.size() > 0) {
                                messageDatabaseHelper.deleteThreads(new ArrayList<>(threadsIdsInCache));
                                showLog("THREADS CACHE UPDATED", "");
                            }
                        }
                    } catch (Exception e) {
                        showErrorLog(e.getMessage());
                        onUnknownException(uniqueId, e);
                    }
                });
            }
        };
        // callback
        handlerSend.put(uniqueId, handler);
        JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
        jsonObject.remove("contentCount");
        jsonObject.remove("repliedTo");
        jsonObject.remove("subjectId");
        jsonObject.remove("time");
        sendAsyncMessage(jsonObject.toString(), AsyncAckType.Constants.WITHOUT_ACK, "UPDATING_THREADS_CACHE");
    }
}
Also used : ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) ResultThreadsSummary(com.fanap.podchat.model.ResultThreadsSummary) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) 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) TypeToken(com.google.gson.reflect.TypeToken) 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)

Aggregations

ChatMessage (com.fanap.podchat.mainmodel.ChatMessage)56 JsonObject (com.google.gson.JsonObject)33 AsyncMessage (com.fanap.podchat.mainmodel.AsyncMessage)20 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)9 PodChatException (com.fanap.podchat.util.PodChatException)9 JsonSyntaxException (com.google.gson.JsonSyntaxException)9 SentryException (io.sentry.core.protocol.SentryException)9 IOException (java.io.IOException)9 JSONException (org.json.JSONException)9 ArrayList (java.util.ArrayList)5 ChatResponse (com.fanap.podchat.model.ChatResponse)4 JsonArray (com.google.gson.JsonArray)4 NonNull (android.support.annotation.NonNull)3 CacheMessageVO (com.fanap.podchat.cachemodel.CacheMessageVO)3 GapMessageVO (com.fanap.podchat.cachemodel.GapMessageVO)3 MessageVO (com.fanap.podchat.mainmodel.MessageVO)3 ResultNewMessage (com.fanap.podchat.model.ResultNewMessage)3 JsonElement (com.google.gson.JsonElement)3 TypeToken (com.google.gson.reflect.TypeToken)3 SendingQueueCache (com.fanap.podchat.cachemodel.queue.SendingQueueCache)2