use of com.fanap.podchat.cachemodel.CacheMessageVO in project pod-chat-android-sdk by FanapSoft.
the class MessageDatabaseHelper method insertLastMessage.
@Nullable
private CacheMessageVO insertLastMessage(Thread thread, ThreadVo threadVo) {
CacheMessageVO cacheMessageVO;
try {
MessageVO messageVO = thread.getLastMessageVO();
messageVO.setConversation(thread);
cacheMessageVO = new CacheMessageVO(messageVO);
threadVo.setLastMessageVO(cacheMessageVO);
threadVo.setLastMessageVOId(cacheMessageVO.getId());
messageDao.insertLastMessageVO(cacheMessageVO);
// to avoid infinite message-thread loop
messageVO.setConversation(null);
} catch (Exception e) {
captureException(e);
return null;
}
return cacheMessageVO;
}
use of com.fanap.podchat.cachemodel.CacheMessageVO in project pod-chat-android-sdk by FanapSoft.
the class DbTest method updateCacheFirstMsgIdAndLastMsgIdConditional2.
// first messsage and last message
// Conditional 2
// cache siz more than one but server size is 1
@Test
public void updateCacheFirstMsgIdAndLastMsgIdConditional2() {
long threadId = 2;
Callback callback = new Callback();
List<MessageVO> messageVOS = new ArrayList<>();
// MessageVO messageVO = new MessageVO(
// 5653,
// false,
// false,
// false,
// false,
// false,
// "91efe7da-547f-4c5f-c34b-0442951ffbbc",
// 0,
// 5652,
// "",
// null,
// 13354321,
// 321000000,
// "",
// null,
// null,
// null,
// null
//
// );
List<CacheMessageVO> cacheMessageVOS = new ArrayList<>();
CacheMessageVO cacheMessageVO = new CacheMessageVO();
cacheMessageVO.setId(5878);
cacheMessageVO.setThreadVoId(2L);
cacheMessageVOS.add(cacheMessageVO);
// messageVOS.add(messageVO);
callback.setOffset(0);
callback.setOffset(0);
callback.setCount(50);
callback.setOrder("asc");
callback.setFirstMessageId(5652);
callback.setLastMessageId(5878);
messageDatabaseHelper.updateGetHistoryResponse(callback, messageVOS, threadId, cacheMessageVOS);
}
use of com.fanap.podchat.cachemodel.CacheMessageVO in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method findAndUpdateGaps.
private void findAndUpdateGaps(List<MessageVO> newMessagesFromServer, long threadId) {
Runnable jobFindAndInsertGap = () -> {
if (newMessagesFromServer.size() == 0)
return;
MessageVO lastMessage = newMessagesFromServer.get(newMessagesFromServer.size() - 1);
if (lastMessage.getPreviousId() == 0)
return;
List<CacheMessageVO> messages = messageDatabaseHelper.getMessageById(lastMessage.getPreviousId());
if (Util.isNullOrEmpty(messages)) {
GapMessageVO gapMessageVO = new GapMessageVO();
gapMessageVO.setId(lastMessage.getId());
gapMessageVO.setPreviousId(lastMessage.getPreviousId());
gapMessageVO.setThreadId(threadId);
gapMessageVO.setTime(lastMessage.getTime());
gapMessageVO.setUniqueId(lastMessage.getUniqueId());
messageDatabaseHelper.insertGap(gapMessageVO);
lastMessage.setHasGap(true);
dataSource.updateMessage(lastMessage, threadId);
// messageDatabaseHelper.updateMessage(lastMessage, threadId);
}
};
Runnable jobUpdateGaps = () -> {
List<GapMessageVO> gaps = messageDatabaseHelper.getAllGaps(threadId);
if (!Util.isNullOrEmpty(gaps)) {
Map<Long, Long> msgIdAndPreviousId = new HashMap<>();
for (GapMessageVO gapMessage : gaps) {
msgIdAndPreviousId.put(gapMessage.getPreviousId(), gapMessage.getId());
}
for (MessageVO newMessage : newMessagesFromServer) {
if (msgIdAndPreviousId.containsKey(newMessage.getId())) {
// delete gap that produced by this message
messageDatabaseHelper.deleteGapForMessageId(msgIdAndPreviousId.get(newMessage.getId()));
// set message gap field to false
messageDatabaseHelper.updateMessageGapState(msgIdAndPreviousId.get(newMessage.getId()), false);
}
}
}
};
PodThreadManager podThreadManager = new PodThreadManager();
podThreadManager.addTask(jobFindAndInsertGap);
podThreadManager.addTask(jobUpdateGaps);
podThreadManager.runTasksSynced();
}
use of com.fanap.podchat.cachemodel.CacheMessageVO in project pod-chat-android-sdk by FanapSoft.
the class MessageDatabaseHelper method updateMessageGapState.
public void updateMessageGapState(Long msgId, boolean hasGap) {
worker(() -> {
List<CacheMessageVO> cacheMessageVO = messageDao.getMessage(msgId);
if (!Util.isNullOrEmpty(cacheMessageVO)) {
for (CacheMessageVO msg : cacheMessageVO) {
msg.setHasGap(hasGap);
messageDao.updateMessage(msg);
}
}
});
}
use of com.fanap.podchat.cachemodel.CacheMessageVO in project pod-chat-android-sdk by FanapSoft.
the class MessageDatabaseHelper method getHistories.
@NonNull
public List<MessageVO> getHistories(@NonNull History history, long threadId) {
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();
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);
prepareMessageVOs(messageVOS, cacheMessageVOS);
return messageVOS;
}
Aggregations