Sort chat messages from received date and not send date.

This commit is contained in:
Julien Wadel 2023-03-06 09:10:11 +01:00
parent 7374e541e8
commit 2fb69d9fd6
15 changed files with 78 additions and 12 deletions

View file

@ -31,10 +31,19 @@ ChatCallModel::ChatCallModel ( std::shared_ptr<linphone::CallLog> callLog, const
App::getInstance()->getEngine()->setObjectOwnership(this, QQmlEngine::CppOwnership);// Avoid QML to destroy it when passing by Q_INVOKABLE
mCallLog = callLog;
mIsStart = isStart;
bool hasReceived = mCallLog->dataExists("receivedTime");
if(isStart){
mTimestamp = QDateTime::fromMSecsSinceEpoch(callLog->getStartDate() * 1000);
if(hasReceived)
mReceivedTimestamp = QDateTime::fromMSecsSinceEpoch(mCallLog->getData<time_t>("receivedTime"));
else
mReceivedTimestamp = mTimestamp;
}else{
mTimestamp = QDateTime::fromMSecsSinceEpoch((callLog->getStartDate() + callLog->getDuration()) * 1000);
if(hasReceived)
mReceivedTimestamp = QDateTime::fromMSecsSinceEpoch((mCallLog->getData<time_t>("receivedTime") + callLog->getDuration()) * 1000);
else
mReceivedTimestamp = mTimestamp;
}
mIsOutgoing = (mCallLog->getDir() == linphone::Call::Dir::Outgoing);
mStatus = (LinphoneEnums::fromLinphone(mCallLog->getStatus()));

View file

@ -37,6 +37,7 @@ public:
Q_PROPERTY(ChatRoomModel::EntryType type MEMBER mType CONSTANT)
Q_PROPERTY(QDateTime timestamp MEMBER mTimestamp CONSTANT)
Q_PROPERTY(QDateTime receivedTimestamp MEMBER mReceivedTimestamp CONSTANT)
Q_PROPERTY(bool isStart MEMBER mIsStart WRITE setIsStart NOTIFY isStartChanged)
Q_PROPERTY(LinphoneEnums::CallStatus status MEMBER mStatus WRITE setStatus NOTIFY statusChanged)

View file

@ -35,9 +35,18 @@ ChatEvent::~ChatEvent(){
QDateTime ChatEvent::getTimestamp() const{
return mTimestamp;
}
QDateTime ChatEvent::getReceivedTimestamp() const{
return mReceivedTimestamp;
}
void ChatEvent::setTimestamp(const QDateTime& timestamp){
mTimestamp = timestamp;
}
void ChatEvent::setReceivedTimestamp(const QDateTime& timestamp){
mReceivedTimestamp = timestamp;
}
void ChatEvent::deleteEvent(){
}

View file

@ -34,12 +34,16 @@ public:
ChatRoomModel::EntryType mType;
virtual QDateTime getTimestamp() const;
virtual QDateTime getReceivedTimestamp() const;
virtual void setTimestamp(const QDateTime& timestamp = QDateTime::currentDateTime());
virtual void setReceivedTimestamp(const QDateTime& timestamp = QDateTime::currentDateTime());
virtual void deleteEvent();
protected:
QDateTime mTimestamp;
QDateTime mReceivedTimestamp;
};
Q_DECLARE_METATYPE(ChatEvent*)
#endif

View file

@ -109,6 +109,13 @@ ChatMessageModel::ChatMessageModel ( std::shared_ptr<linphone::ChatMessage> chat
mContent = txt;
mTimestamp = QDateTime::fromMSecsSinceEpoch(chatMessage->getTime() * 1000);
auto appdata = ChatMessageModel::AppDataManager(QString::fromStdString(chatMessage->getAppdata()));
if(!appdata.mData.contains("receivedTime")){
appdata.mData["receivedTime"] = QString::number(mTimestamp.toSecsSinceEpoch());
chatMessage->setAppdata(Utils::appStringToCoreString(appdata.toString()));
mReceivedTimestamp = mTimestamp;
}else
mReceivedTimestamp = QDateTime::fromMSecsSinceEpoch(appdata.mData["receivedTime"].toULongLong() * 1000);
}
mWasDownloaded = false;
@ -235,6 +242,10 @@ void ChatMessageModel::setTimestamp(const QDateTime& timestamp) {
mTimestamp = timestamp;
}
void ChatMessageModel::setReceivedTimestamp(const QDateTime& timestamp) {
mReceivedTimestamp = timestamp;
}
//-----------------------------------------------------------------------------------------------------------------------
void ChatMessageModel::resendMessage (){

View file

@ -83,6 +83,7 @@ public:
Q_PROPERTY(bool wasDownloaded MEMBER mWasDownloaded WRITE setWasDownloaded NOTIFY wasDownloadedChanged)
Q_PROPERTY(ChatRoomModel::EntryType type MEMBER mType CONSTANT)
Q_PROPERTY(QDateTime timestamp MEMBER mTimestamp CONSTANT)
Q_PROPERTY(QDateTime receivedTimestamp MEMBER mReceivedTimestamp CONSTANT)
Q_PROPERTY(QString content MEMBER mContent NOTIFY contentChanged)
@ -125,6 +126,7 @@ public:
void setWasDownloaded(bool wasDownloaded);
virtual void setTimestamp(const QDateTime& timestamp = QDateTime::currentDateTime()) override;
virtual void setReceivedTimestamp(const QDateTime& timestamp = QDateTime::currentDateTime()) override;
//----------------------------------------------------------------------------

View file

@ -34,14 +34,19 @@ ChatNoticeModel::ChatNoticeModel ( std::shared_ptr<linphone::EventLog> eventLog,
mEventLogType = LinphoneEnums::EventLogType::EventLogTypeNone;
setEventLogType(LinphoneEnums::fromLinphone(mEventLog->getType()));
mTimestamp = QDateTime::fromMSecsSinceEpoch(eventLog->getCreationTime() * 1000);
if(mEventLog->dataExists("receivedTime"))
mReceivedTimestamp = QDateTime::fromMSecsSinceEpoch(mEventLog->getData<time_t>("receivedTime") * 1000);
else
mReceivedTimestamp = mTimestamp;
}
ChatNoticeModel::ChatNoticeModel ( NoticeType noticeType, const QDateTime& timestamp, const QString& txt, QObject * parent) : ChatEvent(ChatRoomModel::EntryType::NoticeEntry, parent) {
ChatNoticeModel::ChatNoticeModel ( NoticeType noticeType, const QDateTime& timestamp, const QDateTime& receivedTimestamp, const QString& txt, QObject * parent) : ChatEvent(ChatRoomModel::EntryType::NoticeEntry, parent) {
App::getInstance()->getEngine()->setObjectOwnership(this, QQmlEngine::CppOwnership);// Avoid QML to destroy it when passing by Q_INVOKABLE
mEventLogType = LinphoneEnums::EventLogType::EventLogTypeNone;
setStatus(noticeType);
setName(txt);
mTimestamp = timestamp;
mReceivedTimestamp = receivedTimestamp;
}
ChatNoticeModel::~ChatNoticeModel(){
@ -55,8 +60,8 @@ QSharedPointer<ChatNoticeModel> ChatNoticeModel::create(std::shared_ptr<linphone
return nullptr;
}
QSharedPointer<ChatNoticeModel> ChatNoticeModel::create(NoticeType noticeType, const QDateTime& timestamp, const QString& txt, QObject * parent){
auto model = QSharedPointer<ChatNoticeModel>::create(noticeType, timestamp, txt, parent);
QSharedPointer<ChatNoticeModel> ChatNoticeModel::create(NoticeType noticeType, const QDateTime& timestamp, const QDateTime& receivedTimestamp, const QString& txt, QObject * parent){
auto model = QSharedPointer<ChatNoticeModel>::create(noticeType, timestamp, receivedTimestamp, txt, parent);
if(model ){
return model;
}else

View file

@ -39,13 +39,14 @@ public:
Q_ENUM(NoticeType);
static QSharedPointer<ChatNoticeModel> create(std::shared_ptr<linphone::EventLog> eventLog, QObject * parent = nullptr);// Call it instead constructor
static QSharedPointer<ChatNoticeModel> create(NoticeType noticeType, const QDateTime& timestamp,const QString& txt, QObject * parent = nullptr);
static QSharedPointer<ChatNoticeModel> create(NoticeType noticeType, const QDateTime& timestamp, const QDateTime& receivedTimestamp,const QString& txt, QObject * parent = nullptr);
ChatNoticeModel (std::shared_ptr<linphone::EventLog> eventLog, QObject * parent = nullptr);
ChatNoticeModel (NoticeType noticeType, const QDateTime& timestamp, const QString& txt, QObject * parent = nullptr);
ChatNoticeModel (NoticeType noticeType, const QDateTime& timestamp, const QDateTime& receivedTimestamp, const QString& txt, QObject * parent = nullptr);
virtual ~ChatNoticeModel();
Q_PROPERTY(ChatRoomModel::EntryType type MEMBER mType CONSTANT)// NoticeEntry
Q_PROPERTY(QDateTime timestamp MEMBER mTimestamp CONSTANT)
Q_PROPERTY(QDateTime receivedTimestamp MEMBER mReceivedTimestamp CONSTANT)
Q_PROPERTY(QString name MEMBER mName WRITE setName NOTIFY nameChanged)
Q_PROPERTY(NoticeType status MEMBER mStatus WRITE setStatus NOTIFY statusChanged)
Q_PROPERTY(LinphoneEnums::EventLogType eventLogType MEMBER mEventLogType WRITE setEventLogType NOTIFY eventLogTypeChanged)

View file

@ -232,7 +232,7 @@ QVariant ChatRoomModel::data (const QModelIndex &index, int role) const {
switch (role) {
case Roles::ChatEntry: return QVariant::fromValue(mList[row].get());
case Roles::SectionDate: return QVariant::fromValue(mList[row].objectCast<ChatEvent>()->getTimestamp().date());
case Roles::SectionDate: return QVariant::fromValue(mList[row].objectCast<ChatEvent>()->getReceivedTimestamp().date());
}
return QVariant();
@ -856,7 +856,7 @@ void ChatRoomModel::updateNewMessageNotice(const int& count){
lastUnreadMessage = min(lastUnreadMessage, QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000 - 1 )); //-1 to be sure that event will be before the message
}
}
mUnreadMessageNotice = ChatNoticeModel::create(ChatNoticeModel::NoticeType::NoticeUnreadMessages, lastUnreadMessage, QString::number(count));
mUnreadMessageNotice = ChatNoticeModel::create(ChatNoticeModel::NoticeType::NoticeUnreadMessages, lastUnreadMessage,lastUnreadMessage, QString::number(count));
beginInsertRows(QModelIndex(), 0, 0);
mList.prepend(mUnreadMessageNotice);
endInsertRows();

View file

@ -181,7 +181,7 @@ bool ChatRoomProxyModel::lessThan (const QModelIndex &left, const QModelIndex &r
return true;
if(!a)
return false;
return a->getTimestamp() < b->getTimestamp();
return a->getReceivedTimestamp() < b->getReceivedTimestamp();
}
// -----------------------------------------------------------------------------

View file

@ -230,7 +230,12 @@ void ContentModel::removeThumbnail(){
QFile(thumbnailPath).remove();
}
}
QString backup;
if( mAppData.mData.contains("receivedTime"))
backup = mAppData.mData["receivedTime"];
mAppData.mData.clear();
if( !backup.isEmpty())
mAppData.mData["receivedTime"] = backup;
}
void ContentModel::removeDownloadedFile(){

View file

@ -26,6 +26,7 @@
#include "app/App.hpp"
#include "components/call/CallModel.hpp"
#include "components/chat-events/ChatMessageModel.hpp"
#include "components/contact/ContactModel.hpp"
#include "components/notifier/Notifier.hpp"
#include "components/settings/AccountSettingsModel.hpp"
@ -228,6 +229,13 @@ void CoreHandlers::onMessagesReceived (
appSettings.beginGroup("chatrooms");
for(auto message : messages){
if(message){
auto chatRoom = message->getChatRoom();
auto dbMessage = chatRoom->findMessage(message->getMessageId());
auto appdata = ChatMessageModel::AppDataManager(QString::fromStdString(dbMessage->getAppdata()));
appdata.mData["receivedTime"] = QString::number(QDateTime::currentMSecsSinceEpoch()/1000);
dbMessage->setAppdata(Utils::appStringToCoreString(appdata.toString()));
}
if( !message || message->isOutgoing() )
continue;

View file

@ -50,6 +50,10 @@ using namespace std;
static inline void fillCallStartEntry (QVariantMap &dest, const shared_ptr<linphone::CallLog> &callLog) {
dest["type"] = HistoryModel::CallEntry;
dest["timestamp"] = QDateTime::fromMSecsSinceEpoch(callLog->getStartDate() * 1000);
if(callLog->dataExists("receivedTime"))
dest["receivedTimestamp"] = QDateTime::fromMSecsSinceEpoch(callLog->getData<time_t>("receivedTime") * 1000);
else
dest["receivedTimestamp"] = dest["timestamp"];
dest["isOutgoing"] = callLog->getDir() == linphone::Call::Dir::Outgoing;
dest["status"] = static_cast<HistoryModel::CallStatus>(callLog->getStatus());
dest["isStart"] = true;
@ -63,6 +67,10 @@ static inline void fillCallStartEntry (QVariantMap &dest, const shared_ptr<linph
static inline void fillCallEndEntry (QVariantMap &dest, const shared_ptr<linphone::CallLog> &callLog) {
dest["type"] = HistoryModel::CallEntry;
dest["timestamp"] = QDateTime::fromMSecsSinceEpoch((callLog->getStartDate() + callLog->getDuration()) * 1000);
if(callLog->dataExists("receivedTime"))
dest["receivedTimestamp"] = QDateTime::fromMSecsSinceEpoch((callLog->getData<time_t>("receivedTime") + callLog->getDuration()) * 1000);
else
dest["receivedTimestamp"] = dest["timestamp"];
dest["isOutgoing"] = callLog->getDir() == linphone::Call::Dir::Outgoing;
dest["status"] = static_cast<HistoryModel::CallStatus>(callLog->getStatus());
dest["isStart"] = false;
@ -112,7 +120,7 @@ QVariant HistoryModel::data (const QModelIndex &index, int role) const {
return QVariant::fromValue(data);
}
case Roles::SectionDate:
return QVariant::fromValue(mEntries[row].first["timestamp"].toDate());
return QVariant::fromValue(mEntries[row].first["receivedTimestamp"].toDate());
}
return QVariant();
@ -231,7 +239,7 @@ void HistoryModel::insertCall (const shared_ptr<linphone::CallLog> &callLog) {
const QList<HistoryEntryData>::iterator *start = nullptr
) {
auto it = lower_bound(start ? *start : mEntries.begin(), mEntries.end(), entry, [](const HistoryEntryData &a, const HistoryEntryData &b) {
return a.first["timestamp"] < b.first["timestamp"];
return a.first["receivedTimestamp"] < b.first["receivedTimestamp"];
});
int row = int(distance(mEntries.begin(), it));

View file

@ -418,7 +418,7 @@ void SipAddressesModel::handleLastEntryRemoved (ChatRoomModel *chatRoomModel) {
).toMap();
// Update the timestamp with the new last chat message timestamp.
it2->timestamp = map["timestamp"].toDateTime();
it2->timestamp = map["receivedTimestamp"].toDateTime();
emit dataChanged(index(row, 0), index(row, 0));
}

View file

@ -274,7 +274,10 @@ QtObject {
property QtObject time: QtObject {
property var colorModel: ColorsList.add(sectionName+'_time', 'd')
property int pointSize: Units.dp * 10
property int width: 44
property int width: 70
}
property QtObject date: QtObject {
property int pointSize: Units.dp * 8
}
}
}