Commit e1d909d1 authored by Ronan's avatar Ronan
Browse files

feat(MainDb): provide a way to fetch messages from db key

parent a0df884b
......@@ -149,8 +149,6 @@ private:
ContentType cContentType;
std::string cText;
std::weak_ptr<ConferenceChatMessageEvent> chatEvent;
// TODO: Remove my comment. VARIABLES OK.
// Do not expose.
......
......@@ -586,9 +586,10 @@ void ChatMessagePrivate::store() {
return;
}
shared_ptr<ConferenceChatMessageEvent> eventLog = chatEvent.lock();
if (eventLog) {
q->getChatRoom()->getCore()->getPrivate()->mainDb->updateEvent(eventLog);
unique_ptr<MainDb> &mainDb = q->getChatRoom()->getCore()->getPrivate()->mainDb;
if (dbKey.isValid()) {
shared_ptr<EventLog> eventLog = mainDb->getEventFromKey(dbKey);
mainDb->updateEvent(eventLog);
if (direction == ChatMessage::Direction::Incoming) {
if (!hasFileTransferContent()) {
......@@ -602,9 +603,8 @@ void ChatMessagePrivate::store() {
}
}
} else {
eventLog = make_shared<ConferenceChatMessageEvent>(time, q->getSharedFromThis());
chatEvent = eventLog;
q->getChatRoom()->getCore()->getPrivate()->mainDb->addEvent(eventLog);
shared_ptr<EventLog> eventLog = make_shared<ConferenceChatMessageEvent>(time, q->getSharedFromThis());
mainDb->addEvent(eventLog);
if (direction == ChatMessage::Direction::Incoming) {
if (hasFileTransferContent()) {
......
......@@ -1311,6 +1311,60 @@ MainDb::MainDb (const shared_ptr<Core> &core) : AbstractDb(*new MainDbPrivate),
return count;
}
shared_ptr<EventLog> MainDb::getEventFromKey (const MainDbKey &dbKey) {
shared_ptr<EventLog> event;
if (!dbKey.isValid()) {
lWarning() << "Unable to get event from invalid key.";
return event;
}
unique_ptr<MainDb> &q = dbKey.getPrivate()->core.lock()->getPrivate()->mainDb;
MainDbPrivate *d = q->getPrivate();
if (!q->isConnected()) {
lWarning() << "Unable to get event from key. Not connected.";
return event;
}
const long long &storageId = dbKey.getPrivate()->storageId;
event = d->getEventFromCache(storageId);
if (event)
return event;
// TODO: Improve. Deal with all events in the future.
static string query = "SELECT peer_sip_address.value, local_sip_address.value, type, event.creation_time"
" FROM event, conference_event, chat_room, sip_address AS peer_sip_address, sip_address as local_sip_address"
" WHERE event.id = :eventId"
" AND conference_event.event_id = event.id"
" AND conference_event.chat_room_id = chat_room.id"
" AND chat_room.peer_sip_address_id = peer_sip_address.id"
" AND chat_room.local_sip_address_id = local_sip_address.id";
L_BEGIN_LOG_EXCEPTION
soci::session *session = d->dbSession.getBackendSession<soci::session>();
soci::transaction tr(*session);
string peerSipAddress;
string localSipAddress;
int type;
tm creationTime;
*session << query, soci::into(peerSipAddress), soci::into(localSipAddress), soci::into(type),
soci::into(creationTime), soci::use(storageId);
event = d->selectGenericConferenceEvent(
storageId,
static_cast<EventLog::Type>(type),
Utils::getTmAsTimeT(creationTime),
ChatRoomId(IdentityAddress(peerSipAddress), IdentityAddress(localSipAddress))
);
L_END_LOG_EXCEPTION
return event;
}
list<shared_ptr<EventLog>> MainDb::getConferenceNotifiedEvents (
const ChatRoomId &chatRoomId,
unsigned int lastNotifyId
......@@ -1449,7 +1503,7 @@ MainDb::MainDb (const shared_ptr<Core> &core) : AbstractDb(*new MainDbPrivate),
if (getUnreadChatMessagesCount(chatRoomId) == 0)
return;
string query = "UPDATE FROM conference_chat_message_event"
string query = "UPDATE conference_chat_message_event"
" SET state = " + Utils::toString(static_cast<int>(ChatMessage::State::Displayed)) + " ";
query += "WHERE";
if (chatRoomId.isValid())
......
......@@ -34,6 +34,7 @@ class ChatMessage;
class ChatRoom;
class Core;
class EventLog;
class MainDbKey;
class MainDbPrivate;
class MainDb : public AbstractDb, public CoreAccessor {
......@@ -61,6 +62,8 @@ public:
static bool deleteEvent (const std::shared_ptr<EventLog> &eventLog);
int getEventsCount (FilterMask mask = NoFilter) const;
static std::shared_ptr<EventLog> getEventFromKey (const MainDbKey &dbKey);
// ---------------------------------------------------------------------------
// Conference notified events.
// ---------------------------------------------------------------------------
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment