2020-09-02 23:49:15 +03:00
|
|
|
/*
|
|
|
|
Copyright (C) 2020 Sebastian J. Wolf
|
|
|
|
|
|
|
|
This file is part of Fernschreiber.
|
|
|
|
|
|
|
|
Fernschreiber is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Fernschreiber is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-08-19 10:55:13 +03:00
|
|
|
#include "chatlistmodel.h"
|
2020-10-03 23:58:45 +03:00
|
|
|
#include "fernschreiberutils.h"
|
2020-09-02 23:49:15 +03:00
|
|
|
#include <QDebug>
|
2020-08-19 10:55:13 +03:00
|
|
|
|
2020-09-27 17:18:14 +03:00
|
|
|
#define LOG(x) qDebug() << "[ChatListModel]" << x
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
const QString ID("id");
|
2020-10-01 00:59:12 +03:00
|
|
|
const QString DATE("date");
|
|
|
|
const QString TEXT("text");
|
|
|
|
const QString TITLE("title");
|
|
|
|
const QString PHOTO("photo");
|
|
|
|
const QString SMALL("small");
|
2020-09-27 17:18:14 +03:00
|
|
|
const QString ORDER("order");
|
|
|
|
const QString CHAT_ID("chat_id");
|
2020-10-01 00:59:12 +03:00
|
|
|
const QString CONTENT("content");
|
2020-09-27 17:18:14 +03:00
|
|
|
const QString LAST_MESSAGE("last_message");
|
2020-10-01 00:59:12 +03:00
|
|
|
const QString SENDER_USER_ID("sender_user_id");
|
2020-09-27 17:18:14 +03:00
|
|
|
const QString UNREAD_COUNT("unread_count");
|
|
|
|
const QString NOTIFICATION_SETTINGS("notification_settings");
|
|
|
|
const QString LAST_READ_INBOX_MESSAGE_ID("last_read_inbox_message_id");
|
|
|
|
const QString LAST_READ_OUTBOX_MESSAGE_ID("last_read_outbox_message_id");
|
2020-10-03 22:06:50 +03:00
|
|
|
const QString TYPE_MAP("type");
|
|
|
|
const QString IS_CHANNEL("is_channel");
|
2020-10-01 00:59:12 +03:00
|
|
|
|
|
|
|
const QString TYPE("@type");
|
|
|
|
const QString TYPE_MESSAGE_TEXT("messageText");
|
2020-09-27 17:18:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class ChatListModel::ChatData
|
|
|
|
{
|
|
|
|
public:
|
2020-10-01 00:59:12 +03:00
|
|
|
enum Role {
|
|
|
|
RoleDisplay = Qt::DisplayRole,
|
|
|
|
RoleChatId,
|
|
|
|
RoleTitle,
|
|
|
|
RolePhotoSmall,
|
|
|
|
RoleUnreadCount,
|
|
|
|
RoleLastReadInboxMessageId,
|
|
|
|
RoleLastMessageSenderId,
|
|
|
|
RoleLastMessageDate,
|
2020-10-03 22:06:50 +03:00
|
|
|
RoleLastMessageText,
|
|
|
|
RoleIsChannel
|
2020-10-01 00:59:12 +03:00
|
|
|
};
|
|
|
|
|
2020-10-03 23:58:45 +03:00
|
|
|
ChatData(const QVariantMap &data, const QVariantMap &userInformation);
|
2020-09-27 17:18:14 +03:00
|
|
|
|
|
|
|
int compareTo(const ChatData *chat) const;
|
|
|
|
bool setOrder(const QString &order);
|
2020-10-01 00:59:12 +03:00
|
|
|
const QVariant lastMessage(const QString &key) const;
|
|
|
|
QString title() const;
|
|
|
|
int unreadCount() const;
|
|
|
|
QVariant photoSmall() const;
|
|
|
|
qlonglong lastReadInboxMessageId() const;
|
|
|
|
qlonglong senderUserId() const;
|
|
|
|
qlonglong senderMessageDate() const;
|
|
|
|
QString senderMessageText() const;
|
2020-10-03 22:06:50 +03:00
|
|
|
bool isChannel() const;
|
2020-10-01 00:59:12 +03:00
|
|
|
bool updateUnreadCount(int unreadCount);
|
|
|
|
bool updateLastReadInboxMessageId(qlonglong messageId);
|
|
|
|
QVector<int> updateLastMessage(const QVariantMap &message);
|
2020-09-27 17:18:14 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
QVariantMap chatData;
|
|
|
|
QString chatId;
|
|
|
|
qlonglong order;
|
2020-10-03 23:58:45 +03:00
|
|
|
QVariantMap userInformation;
|
2020-09-27 17:18:14 +03:00
|
|
|
};
|
|
|
|
|
2020-10-03 23:58:45 +03:00
|
|
|
ChatListModel::ChatData::ChatData(const QVariantMap &data, const QVariantMap &userInformation) :
|
2020-09-27 17:18:14 +03:00
|
|
|
chatData(data),
|
|
|
|
chatId(data.value(ID).toString()),
|
|
|
|
order(data.value(ORDER).toLongLong())
|
|
|
|
{
|
2020-10-03 23:58:45 +03:00
|
|
|
this->userInformation = userInformation;
|
2020-09-27 17:18:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int ChatListModel::ChatData::compareTo(const ChatData *other) const
|
|
|
|
{
|
|
|
|
if (order == other->order) {
|
|
|
|
return chatId.compare(other->chatId);
|
|
|
|
} else {
|
|
|
|
// This puts most recent ones to the top of the list
|
|
|
|
return (order < other->order) ? 1 : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatListModel::ChatData::setOrder(const QString &newOrder)
|
|
|
|
{
|
|
|
|
if (!newOrder.isEmpty()) {
|
|
|
|
chatData.insert(ORDER, newOrder);
|
|
|
|
order = newOrder.toLongLong();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:59:12 +03:00
|
|
|
inline const QVariant ChatListModel::ChatData::lastMessage(const QString &key) const
|
|
|
|
{
|
|
|
|
return chatData.value(LAST_MESSAGE).toMap().value(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChatListModel::ChatData::title() const
|
|
|
|
{
|
|
|
|
return chatData.value(TITLE).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ChatListModel::ChatData::unreadCount() const
|
|
|
|
{
|
|
|
|
return chatData.value(UNREAD_COUNT).toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ChatListModel::ChatData::photoSmall() const
|
|
|
|
{
|
|
|
|
return chatData.value(PHOTO).toMap().value(SMALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
qlonglong ChatListModel::ChatData::lastReadInboxMessageId() const
|
|
|
|
{
|
|
|
|
return chatData.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong();
|
|
|
|
}
|
|
|
|
|
|
|
|
qlonglong ChatListModel::ChatData::senderUserId() const
|
|
|
|
{
|
|
|
|
return lastMessage(SENDER_USER_ID).toLongLong();
|
|
|
|
}
|
|
|
|
|
|
|
|
qlonglong ChatListModel::ChatData::senderMessageDate() const
|
|
|
|
{
|
|
|
|
return lastMessage(DATE).toLongLong();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChatListModel::ChatData::senderMessageText() const
|
|
|
|
{
|
2020-10-03 23:58:45 +03:00
|
|
|
return FernschreiberUtils::getMessageShortText(lastMessage(CONTENT).toMap(), this->userInformation.value(ID).toLongLong() == senderUserId());
|
2020-10-01 00:59:12 +03:00
|
|
|
}
|
|
|
|
|
2020-10-03 22:06:50 +03:00
|
|
|
bool ChatListModel::ChatData::isChannel() const
|
|
|
|
{
|
|
|
|
return chatData.value(TYPE_MAP).toMap().value(IS_CHANNEL).toBool();
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:59:12 +03:00
|
|
|
bool ChatListModel::ChatData::updateUnreadCount(int count)
|
|
|
|
{
|
|
|
|
const int prevUnreadCount(unreadCount());
|
|
|
|
chatData.insert(UNREAD_COUNT, count);
|
|
|
|
return prevUnreadCount != unreadCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatListModel::ChatData::updateLastReadInboxMessageId(qlonglong messageId)
|
|
|
|
{
|
|
|
|
const qlonglong prevLastReadInboxMessageId(lastReadInboxMessageId());
|
|
|
|
chatData.insert(LAST_READ_INBOX_MESSAGE_ID, messageId);
|
|
|
|
return prevLastReadInboxMessageId != lastReadInboxMessageId();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<int> ChatListModel::ChatData::updateLastMessage(const QVariantMap &message)
|
|
|
|
{
|
|
|
|
const qlonglong prevSenderUserId(senderUserId());
|
|
|
|
const qlonglong prevSenderMessageDate(senderMessageDate());
|
|
|
|
const QString prevSenderMessageText(senderMessageText());
|
|
|
|
|
|
|
|
chatData.insert(LAST_MESSAGE, message);
|
|
|
|
|
|
|
|
QVector<int> changedRoles;
|
|
|
|
changedRoles.append(RoleDisplay);
|
|
|
|
if (prevSenderUserId != senderUserId()) {
|
|
|
|
changedRoles.append(RoleLastMessageSenderId);
|
|
|
|
}
|
|
|
|
if (prevSenderMessageDate != senderMessageDate()) {
|
|
|
|
changedRoles.append(RoleLastMessageDate);
|
|
|
|
}
|
|
|
|
if (prevSenderMessageText != senderMessageText()) {
|
|
|
|
changedRoles.append(RoleLastMessageText);
|
|
|
|
}
|
|
|
|
return changedRoles;
|
|
|
|
}
|
|
|
|
|
2020-08-19 10:55:13 +03:00
|
|
|
ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper)
|
|
|
|
{
|
2020-09-28 21:47:03 +03:00
|
|
|
this->tdLibWrapper = tdLibWrapper;
|
|
|
|
connect(tdLibWrapper, SIGNAL(newChatDiscovered(QString, QVariantMap)), this, SLOT(handleChatDiscovered(QString, QVariantMap)));
|
|
|
|
connect(tdLibWrapper, SIGNAL(chatLastMessageUpdated(QString, QString, QVariantMap)), this, SLOT(handleChatLastMessageUpdated(QString, QString, QVariantMap)));
|
|
|
|
connect(tdLibWrapper, SIGNAL(chatOrderUpdated(QString, QString)), this, SLOT(handleChatOrderUpdated(QString, QString)));
|
|
|
|
connect(tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString, QString, int)), this, SLOT(handleChatReadInboxUpdated(QString, QString, int)));
|
|
|
|
connect(tdLibWrapper, SIGNAL(chatReadOutboxUpdated(QString, QString)), this, SLOT(handleChatReadOutboxUpdated(QString, QString)));
|
|
|
|
connect(tdLibWrapper, SIGNAL(messageSendSucceeded(QString, QString, QVariantMap)), this, SLOT(handleMessageSendSucceeded(QString, QString, QVariantMap)));
|
|
|
|
connect(tdLibWrapper, SIGNAL(chatNotificationSettingsUpdated(QString, QVariantMap)), this, SLOT(handleChatNotificationSettingsUpdated(QString, QVariantMap)));
|
2020-10-01 00:59:12 +03:00
|
|
|
|
|
|
|
// Don't start the timer until we have at least one chat
|
|
|
|
relativeTimeRefreshTimer = new QTimer(this);
|
|
|
|
relativeTimeRefreshTimer->setSingleShot(false);
|
|
|
|
relativeTimeRefreshTimer->setInterval(30000);
|
|
|
|
connect(relativeTimeRefreshTimer, SIGNAL(timeout()), SLOT(handleRelativeTimeRefreshTimer()));
|
2020-08-19 10:55:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ChatListModel::~ChatListModel()
|
|
|
|
{
|
2020-09-27 17:18:14 +03:00
|
|
|
LOG("Destroying myself...");
|
|
|
|
qDeleteAll(chatList);
|
2020-08-19 10:55:13 +03:00
|
|
|
}
|
|
|
|
|
2020-10-01 00:59:12 +03:00
|
|
|
QHash<int,QByteArray> ChatListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int,QByteArray> roles;
|
|
|
|
roles.insert(ChatData::RoleDisplay, "display");
|
|
|
|
roles.insert(ChatData::RoleChatId, "chat_id");
|
|
|
|
roles.insert(ChatData::RoleTitle, "title");
|
|
|
|
roles.insert(ChatData::RolePhotoSmall, "photo_small");
|
|
|
|
roles.insert(ChatData::RoleUnreadCount, "unread_count");
|
|
|
|
roles.insert(ChatData::RoleLastReadInboxMessageId, "last_read_inbox_message_id");
|
|
|
|
roles.insert(ChatData::RoleLastMessageSenderId, "last_message_sender_id");
|
|
|
|
roles.insert(ChatData::RoleLastMessageDate, "last_message_date");
|
|
|
|
roles.insert(ChatData::RoleLastMessageText, "last_message_text");
|
2020-10-03 22:06:50 +03:00
|
|
|
roles.insert(ChatData::RoleIsChannel, "is_channel");
|
2020-10-01 00:59:12 +03:00
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2020-08-19 10:55:13 +03:00
|
|
|
int ChatListModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return chatList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ChatListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2020-09-27 17:18:14 +03:00
|
|
|
const int row = index.row();
|
2020-10-01 00:59:12 +03:00
|
|
|
if (row >= 0 && row < chatList.size()) {
|
|
|
|
const ChatData *data = chatList.at(row);
|
|
|
|
switch ((ChatData::Role)role) {
|
|
|
|
case ChatData::RoleDisplay: return data->chatData;
|
|
|
|
case ChatData::RoleChatId: return data->chatId;
|
|
|
|
case ChatData::RoleTitle: return data->title();
|
|
|
|
case ChatData::RolePhotoSmall: return data->photoSmall();
|
|
|
|
case ChatData::RoleUnreadCount: return data->unreadCount();
|
|
|
|
case ChatData::RoleLastReadInboxMessageId: return data->lastReadInboxMessageId();
|
|
|
|
case ChatData::RoleLastMessageSenderId: return data->senderUserId();
|
|
|
|
case ChatData::RoleLastMessageText: return data->senderMessageText();
|
|
|
|
case ChatData::RoleLastMessageDate: return data->senderMessageDate();
|
2020-10-03 22:06:50 +03:00
|
|
|
case ChatData::RoleIsChannel: return data->isChannel();
|
2020-10-01 00:59:12 +03:00
|
|
|
}
|
2020-08-19 10:55:13 +03:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2020-09-20 01:13:42 +03:00
|
|
|
void ChatListModel::redrawModel()
|
|
|
|
{
|
2020-09-27 17:18:14 +03:00
|
|
|
LOG("Enforcing UI redraw...");
|
2020-09-20 01:13:42 +03:00
|
|
|
layoutChanged();
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:18:14 +03:00
|
|
|
int ChatListModel::updateChatOrder(int chatIndex)
|
2020-08-20 11:50:47 +03:00
|
|
|
{
|
2020-10-01 00:59:12 +03:00
|
|
|
ChatData *chat = chatList.at(chatIndex);
|
2020-09-27 17:18:14 +03:00
|
|
|
|
|
|
|
const int n = chatList.size();
|
|
|
|
int newIndex = chatIndex;
|
|
|
|
while (newIndex > 0 && chat->compareTo(chatList.at(newIndex-1)) < 0) {
|
|
|
|
newIndex--;
|
2020-08-20 11:50:47 +03:00
|
|
|
}
|
2020-09-27 17:18:14 +03:00
|
|
|
if (newIndex == chatIndex) {
|
|
|
|
while (newIndex < n-1 && chat->compareTo(chatList.at(newIndex+1)) > 0) {
|
|
|
|
newIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newIndex != chatIndex) {
|
|
|
|
LOG("Moving chat" << chat->chatId << "from position" << chatIndex << "to" << newIndex);
|
|
|
|
beginMoveRows(QModelIndex(), chatIndex, chatIndex, QModelIndex(), (newIndex < chatIndex) ? newIndex : (newIndex+1));
|
|
|
|
chatList.move(chatIndex, newIndex);
|
|
|
|
chatIndexMap.insert(chat->chatId, newIndex);
|
|
|
|
// Update damaged part of the map
|
|
|
|
const int last = qMax(chatIndex, newIndex);
|
|
|
|
if (newIndex < chatIndex) {
|
|
|
|
// First index is already correct
|
|
|
|
for (int i = newIndex + 1; i <= last; i++) {
|
|
|
|
chatIndexMap.insert(chatList.at(i)->chatId, i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Last index is already correct
|
|
|
|
for (int i = chatIndex; i < last; i++) {
|
|
|
|
chatIndexMap.insert(chatList.at(i)->chatId, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endMoveRows();
|
2020-08-20 11:50:47 +03:00
|
|
|
} else {
|
2020-09-27 17:18:14 +03:00
|
|
|
LOG("Chat" << chat->chatId << "stays at position" << chatIndex);
|
2020-08-20 11:50:47 +03:00
|
|
|
}
|
2020-09-27 17:18:14 +03:00
|
|
|
|
|
|
|
return newIndex;
|
2020-08-20 11:50:47 +03:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:18:14 +03:00
|
|
|
void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMap &chatToBeAdded)
|
2020-08-19 10:55:13 +03:00
|
|
|
{
|
2020-10-03 23:58:45 +03:00
|
|
|
ChatData *chat = new ChatData(chatToBeAdded, tdLibWrapper->getUserInformation());
|
2020-09-27 17:18:14 +03:00
|
|
|
const int n = chatList.size();
|
|
|
|
int chatIndex;
|
|
|
|
for (chatIndex = 0; chatIndex < n && chat->compareTo(chatList.at(chatIndex)) >= 0; chatIndex++);
|
|
|
|
LOG("Adding new chat" << chatId << "at" << chatIndex);
|
|
|
|
beginInsertRows(QModelIndex(), chatIndex, chatIndex);
|
|
|
|
chatList.insert(chatIndex, chat);
|
|
|
|
chatIndexMap.insert(chat->chatId, chatIndex);
|
|
|
|
// Update damaged part of the map
|
|
|
|
for (int i = chatIndex + 1; i <= n; i++) {
|
|
|
|
chatIndexMap.insert(chatList.at(i)->chatId, i);
|
|
|
|
}
|
|
|
|
endInsertRows();
|
2020-10-01 00:59:12 +03:00
|
|
|
|
|
|
|
// Start timestamp refresh timer when the first chat is discovered
|
|
|
|
if (!relativeTimeRefreshTimer->isActive()) {
|
|
|
|
relativeTimeRefreshTimer->start();
|
|
|
|
}
|
2020-08-19 10:55:13 +03:00
|
|
|
}
|
2020-08-20 01:24:24 +03:00
|
|
|
|
2020-08-20 15:58:32 +03:00
|
|
|
void ChatListModel::handleChatLastMessageUpdated(const QString &chatId, const QString &order, const QVariantMap &lastMessage)
|
2020-08-20 01:24:24 +03:00
|
|
|
{
|
2020-09-27 17:18:14 +03:00
|
|
|
if (chatIndexMap.contains(chatId)) {
|
|
|
|
int chatIndex = chatIndexMap.value(chatId);
|
|
|
|
LOG("Updating last message for chat" << chatId <<" at index" << chatIndex << "new order" << order);
|
2020-10-01 00:59:12 +03:00
|
|
|
ChatData *chat = chatList.at(chatIndex);
|
2020-09-27 17:18:14 +03:00
|
|
|
if (chat->setOrder(order)) {
|
|
|
|
chatIndex = updateChatOrder(chatIndex);
|
|
|
|
}
|
|
|
|
const QModelIndex modelIndex(index(chatIndex));
|
2020-10-01 00:59:12 +03:00
|
|
|
emit dataChanged(modelIndex, modelIndex, chat->updateLastMessage(lastMessage));
|
2020-09-27 17:18:14 +03:00
|
|
|
}
|
2020-08-20 01:24:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatListModel::handleChatOrderUpdated(const QString &chatId, const QString &order)
|
|
|
|
{
|
2020-09-27 17:18:14 +03:00
|
|
|
if (chatIndexMap.contains(chatId)) {
|
|
|
|
LOG("Updating chat order of" << chatId << "to" << order);
|
|
|
|
int chatIndex = chatIndexMap.value(chatId);
|
|
|
|
if (chatList.at(chatIndex)->setOrder(order)) {
|
2020-10-01 00:59:12 +03:00
|
|
|
updateChatOrder(chatIndex);
|
2020-09-27 17:18:14 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 01:24:24 +03:00
|
|
|
}
|
2020-08-20 18:06:47 +03:00
|
|
|
|
2020-10-01 00:59:12 +03:00
|
|
|
void ChatListModel::handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount)
|
2020-08-21 00:56:21 +03:00
|
|
|
{
|
2020-09-27 17:18:14 +03:00
|
|
|
if (chatIndexMap.contains(chatId)) {
|
|
|
|
LOG("Updating chat unread count for" << chatId << "unread messages" << unreadCount << ", last read message ID: " << lastReadInboxMessageId);
|
|
|
|
const int chatIndex = chatIndexMap.value(chatId);
|
2020-10-01 00:59:12 +03:00
|
|
|
ChatData *chat = chatList.at(chatIndex);
|
|
|
|
QVector<int> changedRoles;
|
|
|
|
changedRoles.append(ChatData::RoleDisplay);
|
|
|
|
if (chat->updateUnreadCount(unreadCount)) {
|
|
|
|
changedRoles.append(ChatData::RoleUnreadCount);
|
|
|
|
}
|
|
|
|
if (chat->updateLastReadInboxMessageId(lastReadInboxMessageId.toLongLong())) {
|
|
|
|
changedRoles.append(ChatData::RoleLastReadInboxMessageId);
|
|
|
|
}
|
2020-09-27 17:18:14 +03:00
|
|
|
const QModelIndex modelIndex(index(chatIndex));
|
2020-10-01 00:59:12 +03:00
|
|
|
emit dataChanged(modelIndex, modelIndex, changedRoles);
|
2020-09-27 17:18:14 +03:00
|
|
|
}
|
2020-08-21 00:56:21 +03:00
|
|
|
}
|
|
|
|
|
2020-08-31 22:51:52 +03:00
|
|
|
void ChatListModel::handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId)
|
|
|
|
{
|
2020-09-27 17:18:14 +03:00
|
|
|
if (chatIndexMap.contains(chatId)) {
|
|
|
|
LOG("Updating last read message for" << chatId << "last ID" << lastReadOutboxMessageId);
|
|
|
|
const int chatIndex = chatIndexMap.value(chatId);
|
2020-10-01 00:59:12 +03:00
|
|
|
ChatData *chat = chatList.at(chatIndex);
|
2020-09-27 17:18:14 +03:00
|
|
|
chat->chatData.insert(LAST_READ_OUTBOX_MESSAGE_ID, lastReadOutboxMessageId);
|
|
|
|
const QModelIndex modelIndex(index(chatIndex));
|
|
|
|
emit dataChanged(modelIndex, modelIndex);
|
|
|
|
}
|
2020-08-31 22:51:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatListModel::handleMessageSendSucceeded(const QString &messageId, const QString &oldMessageId, const QVariantMap &message)
|
|
|
|
{
|
2020-09-27 17:18:14 +03:00
|
|
|
const QString chatId(message.value(CHAT_ID).toString());
|
|
|
|
if (chatIndexMap.contains(chatId)) {
|
|
|
|
const int chatIndex = chatIndexMap.value(chatId);
|
|
|
|
LOG("Updating last message for chat" << chatId << "at index" << chatIndex << ", as message was sent, old ID:" << oldMessageId << ", new ID:" << messageId);
|
|
|
|
const QModelIndex modelIndex(index(chatIndex));
|
2020-10-01 00:59:12 +03:00
|
|
|
emit dataChanged(modelIndex, modelIndex, chatList.at(chatIndex)->updateLastMessage(message));
|
2020-09-27 17:18:14 +03:00
|
|
|
}
|
2020-08-31 22:51:52 +03:00
|
|
|
}
|
|
|
|
|
2020-09-16 21:43:36 +03:00
|
|
|
void ChatListModel::handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings)
|
|
|
|
{
|
2020-09-27 17:18:14 +03:00
|
|
|
if (chatIndexMap.contains(chatId)) {
|
|
|
|
const int chatIndex = chatIndexMap.value(chatId);
|
|
|
|
LOG("Updating notification settings for chat" << chatId << "at index" << chatIndex);
|
2020-10-01 00:59:12 +03:00
|
|
|
ChatData *chat = chatList.at(chatIndex);
|
2020-09-27 17:18:14 +03:00
|
|
|
chat->chatData.insert(NOTIFICATION_SETTINGS, chatNotificationSettings);
|
|
|
|
const QModelIndex modelIndex(index(chatIndex));
|
|
|
|
emit dataChanged(modelIndex, modelIndex);
|
2020-08-20 18:06:47 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-01 00:59:12 +03:00
|
|
|
|
|
|
|
void ChatListModel::handleRelativeTimeRefreshTimer()
|
|
|
|
{
|
|
|
|
LOG("Refreshing timestamps");
|
|
|
|
QVector<int> roles;
|
|
|
|
roles.append(ChatData::RoleLastMessageDate);
|
|
|
|
emit dataChanged(index(0), index(chatList.size() - 1), roles);
|
|
|
|
}
|