Search in sources :

Example 1 with CacheParticipant

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

the class ChatCore method reformatThreadParticipants.

private ChatResponse<ResultParticipant> reformatThreadParticipants(Callback callback, ChatMessage chatMessage) {
    ArrayList<Participant> participants = new ArrayList<>();
    if (!Util.isNullOrEmpty(chatMessage.getContent())) {
        try {
            participants = gson.fromJson(chatMessage.getContent(), new TypeToken<ArrayList<Participant>>() {
            }.getType());
        } catch (Exception e) {
            showErrorLog(e.getMessage());
            onUnknownException(chatMessage.getUniqueId(), e);
        }
    }
    if (cache) {
        List<CacheParticipant> cacheParticipants = new ArrayList<>();
        if (!Util.isNullOrEmpty(chatMessage.getContent())) {
            try {
                cacheParticipants = gson.fromJson(chatMessage.getContent(), new TypeToken<ArrayList<CacheParticipant>>() {
                }.getType());
            } catch (JsonSyntaxException e) {
                showErrorLog(e.getMessage());
                onUnknownException(chatMessage.getUniqueId(), e);
            }
        }
        if (!cacheParticipants.isEmpty())
            messageDatabaseHelper.saveParticipants(cacheParticipants, chatMessage.getSubjectId(), getExpireAmount());
    }
    ChatResponse<ResultParticipant> outPutParticipant = new ChatResponse<>();
    outPutParticipant.setErrorCode(0);
    outPutParticipant.setErrorMessage("");
    outPutParticipant.setHasError(false);
    outPutParticipant.setUniqueId(chatMessage.getUniqueId());
    outPutParticipant.setSubjectId(chatMessage.getSubjectId());
    ResultParticipant resultParticipant = new ResultParticipant();
    resultParticipant.setContentCount(chatMessage.getContentCount());
    resultParticipant.setThreadId(chatMessage.getSubjectId());
    if (callback != null) {
        resultParticipant.setHasNext(participants.size() + callback.getOffset() < chatMessage.getContentCount());
        resultParticipant.setNextOffset(callback.getOffset() + participants.size());
    }
    resultParticipant.setParticipants(participants);
    outPutParticipant.setResult(resultParticipant);
    return outPutParticipant;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) ResultAddParticipant(com.fanap.podchat.model.ResultAddParticipant) ResultParticipant(com.fanap.podchat.model.ResultParticipant) Participant(com.fanap.podchat.mainmodel.Participant) CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) RequestThreadParticipant(com.fanap.podchat.requestobject.RequestThreadParticipant) OutPutParticipant(com.fanap.podchat.model.OutPutParticipant) ResultParticipant(com.fanap.podchat.model.ResultParticipant) ChatResponse(com.fanap.podchat.model.ChatResponse) ArrayList(java.util.ArrayList) CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) 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)

Example 2 with CacheParticipant

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

the class MessageDatabaseHelper method saveHistory.

private void saveHistory(@NonNull List<CacheMessageVO> messageVOS, long threadId) {
    worker(() -> {
        for (CacheMessageVO cacheMessageVO : messageVOS) {
            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 (cacheMessageVO.getParticipant() != null) {
                cacheMessageVO.setParticipantId(cacheMessageVO.getParticipant().getId());
                messageDao.insertParticipant(cacheMessageVO.getParticipant());
            }
            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) {
                CacheReplyInfoVO cacheReplyInfoVO = cacheMessageVO.getReplyInfoVO();
                cacheMessageVO.setReplyInfoVOId(cacheReplyInfoVO.getRepliedToMessageId());
                if (cacheReplyInfoVO.getParticipant() != null) {
                    CacheParticipant cacheReplyParticipant = cacheReplyInfoVO.getParticipant();
                    cacheReplyInfoVO.setParticipantId(cacheReplyParticipant.getId());
                    messageDao.insertParticipant(cacheReplyParticipant);
                }
                messageDao.insertReplyInfoVO(cacheReplyInfoVO);
            }
        }
        messageDao.insertHistories(messageVOS);
    });
}
Also used : CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) CacheReplyInfoVO(com.fanap.podchat.cachemodel.CacheReplyInfoVO)

Example 3 with CacheParticipant

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

the class MessageDatabaseHelper method getThreadRaw.

public void getThreadRaw(Integer count, Long offset, @Nullable ArrayList<Integer> threadIds, @Nullable String threadName, boolean isNew, OnWorkDone listener) throws RoomIntegrityException {
    if (!canUseDatabase())
        throw new RoomIntegrityException();
    worker(() -> {
        String sQuery;
        final String ORDER = "order by pin desc,time desc";
        sQuery = "select * from ThreadVo " + ORDER;
        if (threadName != null && !isNew) {
            sQuery = "select * from ThreadVo where title LIKE '%" + threadName + "%' " + ORDER;
        }
        if (threadIds != null && threadIds.size() > 0 && !isNew) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int id : threadIds) {
                stringBuilder.append(id).append(",");
            }
            String stringIds = stringBuilder.toString();
            String lastString = stringIds.replaceAll(",$", "");
            if (threadName != null) {
                sQuery = "select * from ThreadVo where id IN " + "(" + lastString + ")" + "AND title LIKE  '%" + threadName + "%' " + ORDER;
            } else {
                sQuery = "select * from ThreadVo where id IN " + "(" + lastString + ")" + " " + ORDER;
            }
        }
        // only threads with unreadCount > 0 if isNew == true
        if (isNew) {
            sQuery = "select * from ThreadVo where unreadCount > 0 " + ORDER;
        }
        long contentCount = 0;
        SimpleSQLiteQuery countQuery = new SimpleSQLiteQuery(sQuery.replaceFirst("select \\* ", "select count(id) "));
        contentCount = messageDao.getThreadContentCount(countQuery);
        sQuery += getPaging(count, offset);
        SimpleSQLiteQuery query = new SimpleSQLiteQuery(sQuery);
        List<ThreadVo> threadVos = messageDao.getThreadRaw(query);
        List<Thread> threads = new ArrayList<>();
        if (threadVos != null) {
            for (ThreadVo threadVo : threadVos) {
                if (threadVo.getId() == 0)
                    continue;
                CacheParticipant cacheParticipant;
                CacheReplyInfoVO cacheReplyInfoVO;
                Participant participant = null;
                ReplyInfoVO replyInfoVO = null;
                @Nullable 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);
            }
        }
        listener.onWorkDone(threads);
        listener.onWorkDone(contentCount, threads);
    });
}
Also used : ArrayList(java.util.ArrayList) Thread(com.fanap.podchat.mainmodel.Thread) 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) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) 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) Nullable(android.support.annotation.Nullable)

Example 4 with CacheParticipant

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

the class MessageDatabaseHelper method saveParticipants.

/**
 * Cache participant
 */
public void saveParticipants(@NonNull List<CacheParticipant> participants, long threadId, int expireSecond) {
    worker(() -> {
        for (CacheParticipant participant : participants) {
            participant.setThreadId(threadId);
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss", Locale.getDefault());
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.SECOND, expireSecond);
            String expireDate = format.format(c.getTime());
            messageDao.insertParticipant(participant);
            CacheThreadParticipant ctp = new CacheThreadParticipant();
            ctp.setExpireDate(expireDate);
            ctp.setParticipantId(participant.getId());
            ctp.setThreadId(threadId);
            messageDao.insertThreadParticipant(ctp);
            if (!Util.isNullOrEmpty(participant.getRoles())) {
                CacheParticipantRoles cpr = new CacheParticipantRoles();
                cpr.setId(participant.getId());
                cpr.setThreadId(threadId);
                cpr.setRoles(participant.getRoles());
                Log.d("MTAG", "SAVE CPR: " + cpr);
                messageDao.insertRoles(cpr);
            }
            if (participant.getChatProfileVO() != null) {
                ChatProfileVO chatProfileVO = participant.getChatProfileVO();
                chatProfileVO.setId(participant.getId());
                messageDao.insertChatProfile(chatProfileVO);
            }
        }
    });
}
Also used : CacheThreadParticipant(com.fanap.podchat.cachemodel.CacheThreadParticipant) ChatProfileVO(com.fanap.podchat.chat.user.profile.ChatProfileVO) CacheParticipantRoles(com.fanap.podchat.cachemodel.CacheParticipantRoles) Calendar(java.util.Calendar) CacheParticipant(com.fanap.podchat.cachemodel.CacheParticipant) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with CacheParticipant

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

the class MessageDatabaseHelper method insertCacheAssistantVo.

private void insertCacheAssistantVo(AssistantVo assistantVo) {
    CacheAssistantVo cacheFile = new CacheAssistantVo();
    cacheFile.setRoles(assistantVo.getRoles());
    cacheFile.setBlock(assistantVo.getBlock());
    if (assistantVo.getParticipantVO() != null) {
        Participant participant = assistantVo.getParticipantVO();
        CacheParticipant pInCache = messageDao.getParticipant(participant.getId());
        CacheParticipant cacheParticipant;
        if (pInCache != null) {
            cacheParticipant = new CacheParticipant(participant, pInCache.getThreadId());
        } else {
            cacheParticipant = new CacheParticipant(participant, 0);
        }
        cacheFile.setParticipantVOId(cacheParticipant.getId());
        cacheFile.setInviteeId(cacheParticipant.getId());
        messageDao.insertParticipant(cacheParticipant);
        if (participant.getChatProfileVO() != null) {
            participant.getChatProfileVO().setId(participant.getId());
            messageDao.insertChatProfile(participant.getChatProfileVO());
        }
    }
    cacheFile.setContactType(assistantVo.getContactType());
    messageDao.insertCacheAssistantVo(cacheFile);
}
Also used : 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) CacheAssistantVo(com.fanap.podchat.cachemodel.CacheAssistantVo)

Aggregations

CacheParticipant (com.fanap.podchat.cachemodel.CacheParticipant)19 Participant (com.fanap.podchat.mainmodel.Participant)14 CacheThreadParticipant (com.fanap.podchat.cachemodel.CacheThreadParticipant)13 CacheCallParticipant (com.fanap.podchat.call.persist.CacheCallParticipant)12 PinMessageVO (com.fanap.podchat.mainmodel.PinMessageVO)9 ArrayList (java.util.ArrayList)9 CacheMessageVO (com.fanap.podchat.cachemodel.CacheMessageVO)7 CacheReplyInfoVO (com.fanap.podchat.cachemodel.CacheReplyInfoVO)6 GapMessageVO (com.fanap.podchat.cachemodel.GapMessageVO)6 ThreadVo (com.fanap.podchat.cachemodel.ThreadVo)6 MessageVO (com.fanap.podchat.mainmodel.MessageVO)6 Thread (com.fanap.podchat.mainmodel.Thread)6 ReplyInfoVO (com.fanap.podchat.model.ReplyInfoVO)5 ChatProfileVO (com.fanap.podchat.chat.user.profile.ChatProfileVO)4 IOException (java.io.IOException)4 ResultAddParticipant (com.fanap.podchat.model.ResultAddParticipant)3 CacheParticipantRoles (com.fanap.podchat.cachemodel.CacheParticipantRoles)2 ChatResponse (com.fanap.podchat.model.ChatResponse)2 OutPutParticipant (com.fanap.podchat.model.OutPutParticipant)2 ResultParticipant (com.fanap.podchat.model.ResultParticipant)2