Search in sources :

Example 16 with CacheMessageVO

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

the class MessageDatabaseHelper method prepareMessageVOs.

private void prepareMessageVOs(List<MessageVO> messageVOS, List<CacheMessageVO> cacheMessageVOS) {
    for (CacheMessageVO cacheMessageVO : cacheMessageVOS) {
        Participant participant = null;
        ReplyInfoVO replyInfoVO = null;
        ForwardInfo forwardInfo = null;
        ConversationSummery conversationSummery = null;
        if (cacheMessageVO.getForwardInfoId() != null) {
            cacheMessageVO.setForwardInfo(messageDao.getForwardInfo(cacheMessageVO.getForwardInfoId()));
        }
        if (cacheMessageVO.getParticipantId() != null) {
            CacheParticipant cacheParticipant = messageDao.getParticipant(cacheMessageVO.getParticipantId());
            if (cacheParticipant != null) {
                ChatProfileVO profileVO = messageDao.getChatProfileVOById(cacheParticipant.getId());
                cacheParticipant.setChatProfileVO(profileVO);
                participant = cacheToParticipantMapper(cacheParticipant, null, null);
            } else {
                if (cacheMessageVO.getConversationId() > 0)
                    messageDao.deleteParticipant(cacheMessageVO.getConversationId(), cacheMessageVO.getParticipantId());
            }
        }
        ThreadVo thread = messageDao.getThreadById(cacheMessageVO.getConversationId());
        cacheMessageVO.setConversation(thread);
        if (cacheMessageVO.getReplyInfoVOId() != null) {
            CacheReplyInfoVO cacheReplyInfoVO = messageDao.getReplyInfo(cacheMessageVO.getReplyInfoVOId());
            if (cacheReplyInfoVO != null) {
                replyInfoVO = new ReplyInfoVO(cacheReplyInfoVO.getRepliedToMessageId(), cacheReplyInfoVO.getMessageType(), cacheReplyInfoVO.isDeleted(), cacheReplyInfoVO.getRepliedToMessage(), cacheReplyInfoVO.getSystemMetadata(), cacheReplyInfoVO.getMetadata(), cacheReplyInfoVO.getMessage(), cacheReplyInfoVO.getRepliedToMessageTime(), cacheReplyInfoVO.getRepliedToMessageNanos());
                if (cacheReplyInfoVO.getParticipantId() > 0) {
                    CacheParticipant cacheParticipant = messageDao.getParticipant(cacheReplyInfoVO.getParticipantId());
                    if (cacheParticipant != null) {
                        Participant replyParticipant = cacheToParticipantMapper(cacheParticipant, false, null);
                        replyInfoVO.setParticipant(replyParticipant);
                    }
                }
            }
        }
        if (cacheMessageVO.getForwardInfo() != null) {
            CacheForwardInfo cacheForwardInfo = messageDao.getForwardInfo(cacheMessageVO.getForwardInfoId());
            if (cacheForwardInfo != null) {
                if (cacheForwardInfo.getParticipantId() != null) {
                    CacheParticipant cacheParticipant = messageDao.getParticipant(cacheForwardInfo.getParticipantId());
                    if (cacheParticipant != null) {
                        participant = cacheToParticipantMapper(cacheParticipant, null, null);
                    }
                }
                if (Util.isNullOrEmpty(cacheForwardInfo.getConversationId())) {
                    // todo check it again
                    conversationSummery = messageDao.getConversationSummery(cacheForwardInfo.getConversationId());
                }
                forwardInfo = new ForwardInfo(participant, conversationSummery);
            }
        }
        Thread msgThread = threadVoToThreadMapper(cacheMessageVO.getConversation(), null);
        MessageVO messageVO = cacheMessageVoToMessageVoMapper(participant, replyInfoVO, forwardInfo, cacheMessageVO);
        messageVO.setConversation(msgThread);
        messageVOS.add(messageVO);
    }
}
Also used : ChatProfileVO(com.fanap.podchat.chat.user.profile.ChatProfileVO) ThreadVo(com.fanap.podchat.cachemodel.ThreadVo) CacheThreadParticipant(com.fanap.podchat.cachemodel.CacheThreadParticipant) CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) Participant(com.fanap.podchat.mainmodel.Participant) CacheCallParticipant(com.fanap.podchat.call.persist.CacheCallParticipant) CacheForwardInfo(com.fanap.podchat.cachemodel.CacheForwardInfo) ConversationSummery(com.fanap.podchat.model.ConversationSummery) ForwardInfo(com.fanap.podchat.mainmodel.ForwardInfo) CacheForwardInfo(com.fanap.podchat.cachemodel.CacheForwardInfo) CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) ReplyInfoVO(com.fanap.podchat.model.ReplyInfoVO) CacheReplyInfoVO(com.fanap.podchat.cachemodel.CacheReplyInfoVO) CacheReplyInfoVO(com.fanap.podchat.cachemodel.CacheReplyInfoVO) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) PinMessageVO(com.fanap.podchat.mainmodel.PinMessageVO) MessageVO(com.fanap.podchat.mainmodel.MessageVO) GapMessageVO(com.fanap.podchat.cachemodel.GapMessageVO) Thread(com.fanap.podchat.mainmodel.Thread)

Example 17 with CacheMessageVO

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

the class MessageDatabaseHelper method saveMessage.

public void saveMessage(@NonNull MessageVO message, long threadId, boolean editedMessage) {
    worker(() -> {
        CacheMessageVO cacheMessageVO = new CacheMessageVO(message);
        if (cacheMessageVO.getParticipant() != null) {
            cacheMessageVO.setParticipantId(cacheMessageVO.getParticipant().getId());
            messageDao.insertParticipant(cacheMessageVO.getParticipant());
        }
        if (cacheMessageVO.getForwardInfo() != null) {
            cacheMessageVO.setForwardInfoId(cacheMessageVO.getForwardInfo().getId());
            messageDao.insertForwardInfo(cacheMessageVO.getForwardInfo());
            if (cacheMessageVO.getForwardInfo().getParticipant() != null) {
                cacheMessageVO.getForwardInfo().setParticipantId(cacheMessageVO.getForwardInfo().getParticipant().getId());
                messageDao.insertParticipant(cacheMessageVO.getForwardInfo().getParticipant());
            }
        }
        if (cacheMessageVO.getReplyInfoVO() != null) {
            cacheMessageVO.setReplyInfoVOId(cacheMessageVO.getReplyInfoVO().getRepliedToMessageId());
            if (cacheMessageVO.getReplyInfoVO().getParticipant() != null) {
                cacheMessageVO.getReplyInfoVO().setParticipantId(cacheMessageVO.getReplyInfoVO().getParticipant().getId());
                messageDao.insertParticipant(cacheMessageVO.getReplyInfoVO().getParticipant());
            }
            messageDao.insertReplyInfoVO(cacheMessageVO.getReplyInfoVO());
        }
        // update thread last message id
        // check if message is new or edited message is thread last message
        boolean shouldUpdateIfEdited = false;
        if (editedMessage) {
            long lastMessageId = messageDao.getLastMessageId(threadId);
            if (lastMessageId > 0 && lastMessageId == cacheMessageVO.getId()) {
                shouldUpdateIfEdited = true;
            }
        }
        List<String> hashtags = getHashtags(cacheMessageVO.getMessage());
        if (hashtags != null && hashtags.size() > 0)
            cacheMessageVO.setHashtags(hashtags);
        messageDao.insertMessage(cacheMessageVO);
        boolean shouldUpdateLastMessage = !editedMessage || shouldUpdateIfEdited;
        if (shouldUpdateLastMessage)
            messageDao.updateThreadLastMessageVOId(threadId, cacheMessageVO.getId(), cacheMessageVO.getMessage());
    });
}
Also used : CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO)

Example 18 with CacheMessageVO

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

the class MessageDatabaseHelper method getMentionList.

public void getMentionList(RequestGetMentionList request, FunctionalListener listener) {
    worker(() -> {
        List<MessageVO> messageVOS = new ArrayList<>();
        List<CacheMessageVO> cacheMessageVOS = new ArrayList<>();
        String condition;
        condition = request.getUnreadMentioned() != null && request.getUnreadMentioned() ? " and seen = false " : " ";
        String rawQuery = "SELECT * FROM CacheMessageVO WHERE threadVoId = " + request.getThreadId() + " and mentioned = true" + condition + "ORDER BY timeStamp ASC LIMIT " + request.getCount() + " OFFSET " + request.getOffset();
        SupportSQLiteQuery sqLiteQuery = new SimpleSQLiteQuery(rawQuery);
        cacheMessageVOS = messageDao.getRawHistory(sqLiteQuery);
        String contentCountQuery = "SELECT count(*) FROM CacheMessageVO WHERE threadVoId = " + request.getThreadId() + " and mentioned = true" + condition;
        long contentCount = messageDao.getHistoryContentCount(new SimpleSQLiteQuery(contentCountQuery));
        prepareMessageVOs(messageVOS, cacheMessageVOS);
        if (messageVOS.size() > 0)
            listener.onWorkDone(messageVOS, contentCount);
    });
}
Also used : SupportSQLiteQuery(android.arch.persistence.db.SupportSQLiteQuery) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) ArrayList(java.util.ArrayList) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) PinMessageVO(com.fanap.podchat.mainmodel.PinMessageVO) MessageVO(com.fanap.podchat.mainmodel.MessageVO) GapMessageVO(com.fanap.podchat.cachemodel.GapMessageVO) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO)

Example 19 with CacheMessageVO

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

the class MessageDatabaseHelper method updateMessage.

public void updateMessage(MessageVO lastMessage, long threadId) {
    worker(() -> {
        CacheMessageVO cacheMessageVO = new CacheMessageVO(lastMessage);
        cacheMessageVO.setThreadVoId(threadId);
        if (cacheMessageVO.getParticipant() != null) {
            cacheMessageVO.setParticipantId(cacheMessageVO.getParticipant().getId());
            messageDao.insertParticipant(cacheMessageVO.getParticipant());
        }
        if (cacheMessageVO.getForwardInfo() != null) {
            cacheMessageVO.setForwardInfoId(cacheMessageVO.getForwardInfo().getId());
            messageDao.insertForwardInfo(cacheMessageVO.getForwardInfo());
            if (cacheMessageVO.getForwardInfo().getParticipant() != null) {
                cacheMessageVO.getForwardInfo().setParticipantId(cacheMessageVO.getForwardInfo().getParticipant().getId());
                messageDao.insertParticipant(cacheMessageVO.getForwardInfo().getParticipant());
            }
        }
        if (cacheMessageVO.getReplyInfoVO() != null) {
            cacheMessageVO.setReplyInfoVOId(cacheMessageVO.getReplyInfoVO().getRepliedToMessageId());
            if (cacheMessageVO.getReplyInfoVO().getParticipant() != null) {
                cacheMessageVO.getReplyInfoVO().setParticipantId(cacheMessageVO.getReplyInfoVO().getParticipant().getId());
                messageDao.insertParticipant(cacheMessageVO.getReplyInfoVO().getParticipant());
            }
            messageDao.insertReplyInfoVO(cacheMessageVO.getReplyInfoVO());
        }
        List<String> hashtags = getHashtags(cacheMessageVO.getMessage());
        if (hashtags != null && hashtags.size() > 0)
            cacheMessageVO.setHashtags(hashtags);
        messageDao.updateMessage(cacheMessageVO);
    });
}
Also used : CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO)

Example 20 with CacheMessageVO

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

the class MessageDatabaseHelper method getHistories.

public void getHistories(@NonNull History history, long threadId, OnWorkDone listener) {
    List<MessageVO> messageVOS = new ArrayList<>();
    List<CacheMessageVO> cacheMessageVOS;
    long fromTime = history.getFromTime();
    long fromTimeNanos = history.getFromTimeNanos();
    long toTime = history.getToTime();
    long toTimeNanos = history.getToTimeNanos();
    long messageId = history.getId();
    long offset = history.getOffset();
    long count = history.getCount();
    int messageType = history.getMessageType();
    String query = history.getQuery();
    String order = history.getOrder();
    offset = offset >= 0 ? offset : 0;
    count = count > 0 ? count : 50;
    if (Util.isNullOrEmpty(order)) {
        order = "desc";
    }
    String rawQuery = "SELECT * FROM CacheMessageVO WHERE threadVoId =" + threadId;
    rawQuery = addMessageIdIfExist(messageId, rawQuery);
    rawQuery = addFromTimeIfExist(fromTime, fromTimeNanos, rawQuery);
    rawQuery = addToTimeIfExist(toTime, toTimeNanos, rawQuery);
    rawQuery = addQueryIfExist(query, rawQuery);
    rawQuery = addMessageTypeIfExist(messageType, rawQuery);
    long contentCount = messageDao.getHistoryContentCount(new SimpleSQLiteQuery(rawQuery.replaceFirst("SELECT \\* ", "SELECT COUNT(ID) ")));
    rawQuery = addOrderAndLimitAndOffset(offset, count, order, rawQuery);
    SupportSQLiteQuery sqLiteQuery = new SimpleSQLiteQuery(rawQuery);
    cacheMessageVOS = messageDao.getRawHistory(sqLiteQuery);
    prepareMessageVOs(messageVOS, cacheMessageVOS);
    List<Sending> sendingList = getAllSendingQueueByThreadId(threadId);
    List<Uploading> uploadingList = getAllUploadingQueueByThreadId(threadId);
    List<Failed> failedList = getAllWaitQueueCacheByThreadId(threadId);
    ChatResponse<ResultHistory> chatResponse = new ChatResponse<>();
    chatResponse.setCache(true);
    ResultHistory resultHistory = new ResultHistory();
    resultHistory.setHistory(messageVOS);
    resultHistory.setNextOffset(history.getOffset() + messageVOS.size());
    resultHistory.setContentCount(contentCount);
    if (messageVOS.size() + history.getOffset() < contentCount) {
        resultHistory.setHasNext(true);
    } else {
        resultHistory.setHasNext(false);
    }
    resultHistory.setHistory(messageVOS);
    resultHistory.setSending(sendingList);
    resultHistory.setUploadingQueue(uploadingList);
    resultHistory.setFailed(failedList);
    chatResponse.setErrorCode(0);
    chatResponse.setHasError(false);
    chatResponse.setErrorMessage("");
    chatResponse.setResult(resultHistory);
    chatResponse.setCache(true);
    chatResponse.setSubjectId(threadId);
    listener.onWorkDone(chatResponse);
}
Also used : Sending(com.fanap.podchat.cachemodel.queue.Sending) SupportSQLiteQuery(android.arch.persistence.db.SupportSQLiteQuery) Failed(com.fanap.podchat.cachemodel.queue.Failed) ArrayList(java.util.ArrayList) Uploading(com.fanap.podchat.cachemodel.queue.Uploading) ResultHistory(com.fanap.podchat.model.ResultHistory) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) ChatResponse(com.fanap.podchat.model.ChatResponse) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) PinMessageVO(com.fanap.podchat.mainmodel.PinMessageVO) MessageVO(com.fanap.podchat.mainmodel.MessageVO) GapMessageVO(com.fanap.podchat.cachemodel.GapMessageVO) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO)

Aggregations

CacheMessageVO (com.fanap.podchat.cachemodel.CacheMessageVO)22 MessageVO (com.fanap.podchat.mainmodel.MessageVO)15 GapMessageVO (com.fanap.podchat.cachemodel.GapMessageVO)14 PinMessageVO (com.fanap.podchat.mainmodel.PinMessageVO)14 ArrayList (java.util.ArrayList)12 SimpleSQLiteQuery (android.arch.persistence.db.SimpleSQLiteQuery)8 SupportSQLiteQuery (android.arch.persistence.db.SupportSQLiteQuery)7 CacheParticipant (com.fanap.podchat.cachemodel.CacheParticipant)7 CacheReplyInfoVO (com.fanap.podchat.cachemodel.CacheReplyInfoVO)7 ThreadVo (com.fanap.podchat.cachemodel.ThreadVo)6 CacheThreadParticipant (com.fanap.podchat.cachemodel.CacheThreadParticipant)5 CacheCallParticipant (com.fanap.podchat.call.persist.CacheCallParticipant)5 Participant (com.fanap.podchat.mainmodel.Participant)5 Thread (com.fanap.podchat.mainmodel.Thread)5 ReplyInfoVO (com.fanap.podchat.model.ReplyInfoVO)5 Failed (com.fanap.podchat.cachemodel.queue.Failed)3 Sending (com.fanap.podchat.cachemodel.queue.Sending)3 Uploading (com.fanap.podchat.cachemodel.queue.Uploading)3 ChatResponse (com.fanap.podchat.model.ChatResponse)3 ResultHistory (com.fanap.podchat.model.ResultHistory)3