Commit 655f2e36 authored by Nicolas Jager's avatar Nicolas Jager Committed by Sébastien Blin

get number of unread messages

add functions to get the number of unread messages. Clients get the
number of unread messages in a conversation when getting
ConversationInfo.

additional test coming with : testCount, count the number of
occurrences in the database matching a value.

Change-Id: Ie7db0b7f288bfe82b64518dd1e9790837c2a2087
Reviewed-by: Sébastien Blin's avatarSébastien Blin <sebastien.blin@savoirfairelinux.com>
parent 0137d9f3
......@@ -342,6 +342,11 @@ addContact(Database& db, const std::string& accountUri, const std::string& conta
}
}
int
countUnreadFromInteractions(Database& db, const std::string& conversationId)
{
return db.count("status", "interactions", "status='UNREAD' AND conversation_id='" + conversationId + "'");
}
} // namespace database
......
......@@ -210,6 +210,10 @@ void removeAccount(Database& db, const std::string& accountUri);
*/
void addContact(Database& db, const std::string& accountUri, const std::string& contactUri);
/**
* count number of 'UNREAD' from 'interactions' table.
*/
int countUnreadFromInteractions(Database& db, const std::string& conversationId);
} // namespace database
......
......@@ -125,6 +125,11 @@ public:
*/
void placeCall(const std::string& uid, bool isAudioOnly = false);
/**
* get number of unread messages
*/
int getNumberOfUnreadMessagesFor(const std::string& uid);
const ConversationModel& linked;
Database& db;
const CallbacksHandler& callbacksHandler;
......@@ -276,7 +281,11 @@ ConversationModel::filteredConversation(const unsigned int row) const
const auto& conversations = allFilteredConversations();
if (row >= conversations.size())
return conversation::Info();
return conversations.at(row);
auto conversationInfo = conversations.at(row);
conversationInfo.unreadMessages = pimpl_->getNumberOfUnreadMessagesFor(conversationInfo.uid);
return conversationInfo;
}
void
......@@ -896,6 +905,7 @@ ConversationModelPimpl::addConversationWith(const std::string& convId,
}
}
conversation.unreadMessages = getNumberOfUnreadMessagesFor(convId);
conversations.emplace_front(conversation);
dirtyConversations = true;
}
......@@ -1163,6 +1173,12 @@ ConversationModelPimpl::slotConferenceRemoved(const std::string& confId)
}
}
int
ConversationModelPimpl::getNumberOfUnreadMessagesFor(const std::string& uid)
{
database::countUnreadFromInteractions(db, uid);
}
} // namespace lrc
#include "api/moc_conversationmodel.cpp"
......
......@@ -250,6 +250,23 @@ Database::select(const std::string& select, // "id",
return std::move(result);
}
int
Database::count(const std::string& count, // "id", "body", ...
const std::string& table, // "tests"
const std::string& where) // "contact=:name AND id=:id"
{
QSqlQuery query;
std::string columnsSelect;
auto prepareStr = std::string("SELECT count(" + count + ") FROM " + table + " WHERE " + where);
query.prepare(prepareStr.c_str());
if (not query.exec())
throw QueryError(query);
query.next();
return query.value(0).toInt();
}
void
Database::deleteFrom(const std::string& table, // "tests"
const std::string& where, // "contact=:name AND id=:id
......
......@@ -208,6 +208,14 @@ public:
const std::string& where,
const std::map<std::string, std::string>& bindsWhere);
/**
* Returns the count of an expression.
* @param count is the column to count.
* @param table where to perfom the action on.
* @param where defines the conditional to select.
*/
int count(const std::string& count, const std::string& table, const std::string& where);
private:
void createTables();
void storeVersion(const std::string& version);
......
......@@ -220,5 +220,13 @@ DatabaseTester::testDeleteInexistantValue()
// Should not throw anything if fails
}
void
DatabaseTester::testCountUnreadMessages()
{
auto table = "profiles";
auto count = database_->count("uri", table, "status='0'");
CPPUNIT_ASSERT(count == 1);
}
} // namespace test
} // namespace ring
......@@ -39,6 +39,7 @@ class DatabaseTester : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DatabaseTester);
CPPUNIT_TEST(testInsertAndSelectCorrectValue);
CPPUNIT_TEST(testCountUnreadMessages);
CPPUNIT_TEST(testInsertIncorrectFail);
CPPUNIT_TEST(testSelectInexistantValue);
CPPUNIT_TEST(testUpdateCorrectValue);
......@@ -84,6 +85,10 @@ public:
* Delete inexistant value in the database
*/
void testDeleteInexistantValue();
/**
* Count the number of unread messages.
*/
void testCountUnreadMessages();
protected:
std::unique_ptr<lrc::Database> database_;
......
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