Search in sources :

Example 6 with CacheMessageVO

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

the class MessageDatabaseHelper method saveMessageHistory.

/**
 * Cache history
 */
public void saveMessageHistory(@NonNull List<MessageVO> messageVOS, long threadId, int expireAmount) {
    worker(() -> {
        List<CacheMessageVO> cacheMessageVOList = new ArrayList<>();
        for (MessageVO messageVO : messageVOS) {
            CacheMessageVO cacheMessageVO = new CacheMessageVO(messageVO);
            cacheMessageVO.setThreadVoId(threadId);
            long time = cacheMessageVO.getTime();
            long timeNanos = cacheMessageVO.getTimeNanos();
            long pow = (long) Math.pow(10, 9);
            long timestamp = ((time / 1000) * pow) + timeNanos;
            cacheMessageVO.setTimeStamp(timestamp);
            if (messageVO.getParticipant() != null) {
                CacheParticipant cacheParticipant = new CacheParticipant(messageVO.getParticipant(), threadId);
                cacheMessageVO.setParticipant(cacheParticipant);
            }
            if (cacheMessageVO.getParticipant() != null) {
                cacheMessageVO.setParticipantId(cacheMessageVO.getParticipant().getId());
                saveParticipant(cacheMessageVO.getParticipant(), threadId, expireAmount);
            }
            if (cacheMessageVO.getConversation() != null) {
                cacheMessageVO.setConversationId(cacheMessageVO.getConversation().getId());
            }
            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());
                }
                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);
            cacheMessageVOList.add(cacheMessageVO);
        }
        messageDao.insertHistories(cacheMessageVOList);
    });
}
Also used : ArrayList(java.util.ArrayList) CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) PinMessageVO(com.fanap.podchat.mainmodel.PinMessageVO) MessageVO(com.fanap.podchat.mainmodel.MessageVO) GapMessageVO(com.fanap.podchat.cachemodel.GapMessageVO)

Example 7 with CacheMessageVO

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

the class MessageDatabaseHelper method getThreads.

public List<Thread> getThreads(long count, long offset) {
    List<Thread> threads;
    if (messageDao.getThreads(count, offset) != null) {
        List<ThreadVo> threadVos = messageDao.getThreads(count, offset);
        threads = new ArrayList<>();
        CacheParticipant cacheParticipant;
        CacheReplyInfoVO cacheReplyInfoVO;
        Participant participant = null;
        ReplyInfoVO replyInfoVO = null;
        for (ThreadVo threadVo : threadVos) {
            MessageVO lastMessageVO = null;
            if (threadVo.getInviterId() > 0) {
                threadVo.setInviter(messageDao.getInviter(threadVo.getInviterId()));
            }
            if (threadVo.getLastMessageVOId() > 0) {
                threadVo.setLastMessageVO(messageDao.getLastMessageVO(threadVo.getLastMessageVOId()));
                CacheMessageVO cacheLastMessageVO = threadVo.getLastMessageVO();
                if (cacheLastMessageVO != null) {
                    if (cacheLastMessageVO.getParticipantId() != null) {
                        cacheParticipant = messageDao.getParticipant(cacheLastMessageVO.getParticipantId());
                        if (cacheParticipant != null) {
                            participant = cacheToParticipantMapper(cacheParticipant, null, null);
                        }
                    }
                    if (cacheLastMessageVO.getReplyInfoVOId() != null) {
                        cacheReplyInfoVO = messageDao.getReplyInfo(cacheLastMessageVO.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());
                    }
                    lastMessageVO = cacheMessageVoToMessageVoMapper(participant, replyInfoVO, null, cacheLastMessageVO);
                }
            }
            // adding pinned message of thread if exist
            addPinnedMessageOfThread(threadVo);
            Thread thread = threadVoToThreadMapper(threadVo, lastMessageVO);
            threads.add(thread);
        }
        // sort threads by last message time
        Collections.sort(new ArrayList<>(threads), (o1, o2) -> Long.compare(o1.getLastMessageVO().getTime(), o2.getLastMessageVO().getTime()));
        return threads;
    } else {
        threads = new ArrayList<>();
    }
    return threads;
}
Also used : 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) CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) CacheReplyInfoVO(com.fanap.podchat.cachemodel.CacheReplyInfoVO) ReplyInfoVO(com.fanap.podchat.model.ReplyInfoVO) 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) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) Thread(com.fanap.podchat.mainmodel.Thread)

Example 8 with CacheMessageVO

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

the class MessageDatabaseHelper method getThreadsByThreadIds.

@NonNull
public List<Thread> getThreadsByThreadIds(@NonNull ArrayList<Integer> threadIds) {
    List<Thread> threads = new ArrayList<>();
    for (int id : threadIds) {
        if (messageDao.getThreadById(id) != null) {
            ThreadVo threadVo = messageDao.getThreadById(id);
            CacheParticipant cacheParticipant;
            CacheReplyInfoVO cacheReplyInfoVO;
            Participant participant = null;
            ReplyInfoVO replyInfoVO = null;
            MessageVO lastMessageVO = null;
            if (threadVo.getInviterId() > 0) {
                threadVo.setInviter(messageDao.getInviter(threadVo.getInviterId()));
            }
            if (threadVo.getLastMessageVOId() > 0) {
                threadVo.setLastMessageVO(messageDao.getLastMessageVO(threadVo.getLastMessageVOId()));
                CacheMessageVO cacheLastMessageVO = threadVo.getLastMessageVO();
                if (cacheLastMessageVO != null) {
                    if (cacheLastMessageVO.getParticipantId() != null) {
                        cacheParticipant = messageDao.getParticipant(cacheLastMessageVO.getParticipantId());
                        participant = cacheToParticipantMapper(cacheParticipant, null, null);
                    }
                    if (cacheLastMessageVO.getReplyInfoVOId() != null) {
                        cacheReplyInfoVO = messageDao.getReplyInfo(cacheLastMessageVO.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());
                    }
                    lastMessageVO = cacheMessageVoToMessageVoMapper(participant, replyInfoVO, null, cacheLastMessageVO);
                }
            }
            // adding pinned message of thread if exist
            addPinnedMessageOfThread(threadVo);
            Thread thread = threadVoToThreadMapper(threadVo, lastMessageVO);
            threads.add(thread);
        }
    }
    return threads;
}
Also used : 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) ArrayList(java.util.ArrayList) CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) CacheReplyInfoVO(com.fanap.podchat.cachemodel.CacheReplyInfoVO) ReplyInfoVO(com.fanap.podchat.model.ReplyInfoVO) 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) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) Thread(com.fanap.podchat.mainmodel.Thread) NonNull(android.support.annotation.NonNull)

Example 9 with CacheMessageVO

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

the class MessageDatabaseHelper method getThreadHistory.

public Observable<ChatResponse<ResultHistory>> getThreadHistory(@NonNull History history, long threadId) {
    return rx.Observable.create(subscriber -> {
        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);
        subscriber.onNext(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)

Example 10 with CacheMessageVO

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

the class MessageDatabaseHelper method getCacheHistories.

public List<CacheMessageVO> getCacheHistories(@NonNull History history, long threadId) {
    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();
    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 = addOrderAndLimitAndOffset(offset, count, order, rawQuery);
    SupportSQLiteQuery sqLiteQuery = new SimpleSQLiteQuery(rawQuery);
    cacheMessageVOS = messageDao.getRawHistory(sqLiteQuery);
    return cacheMessageVOS;
}
Also used : SupportSQLiteQuery(android.arch.persistence.db.SupportSQLiteQuery) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) 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