2020-08-11 22:49:51 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "messagefeed.h"
|
2021-05-15 22:07:49 +00:00
|
|
|
|
|
|
|
#include <ui/models/element.h>
|
|
|
|
#include <ui/models/room.h>
|
2023-08-15 15:28:25 +00:00
|
|
|
#include <shared/defines.h>
|
2020-08-11 22:49:51 +00:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
const QHash<int, QByteArray> Models::MessageFeed::roles = {
|
2020-08-12 16:55:01 +00:00
|
|
|
{Text, "text"},
|
|
|
|
{Sender, "sender"},
|
|
|
|
{Date, "date"},
|
|
|
|
{DeliveryState, "deliveryState"},
|
2023-11-06 23:57:08 +00:00
|
|
|
{Encryption, "encryption"},
|
2020-08-12 16:55:01 +00:00
|
|
|
{Correction, "correction"},
|
2020-08-20 21:32:30 +00:00
|
|
|
{SentByMe,"sentByMe"},
|
2021-01-07 21:50:12 +00:00
|
|
|
{Avatar, "avatar"},
|
|
|
|
{Attach, "attach"},
|
2021-02-01 22:55:15 +00:00
|
|
|
{Id, "id"},
|
2021-04-22 22:41:32 +00:00
|
|
|
{Error, "error"},
|
2021-01-07 21:50:12 +00:00
|
|
|
{Bulk, "bulk"}
|
2020-08-12 16:55:01 +00:00
|
|
|
};
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
Models::MessageFeed::MessageFeed(const Element* ri, QObject* parent):
|
2020-08-11 22:49:51 +00:00
|
|
|
QAbstractListModel(parent),
|
|
|
|
storage(),
|
|
|
|
indexById(storage.get<id>()),
|
|
|
|
indexByTime(storage.get<time>()),
|
2020-08-20 21:32:30 +00:00
|
|
|
rosterItem(ri),
|
2021-01-14 11:22:02 +00:00
|
|
|
syncState(incomplete),
|
|
|
|
uploads(),
|
2021-04-27 19:29:15 +00:00
|
|
|
downloads(),
|
2021-05-14 19:49:38 +00:00
|
|
|
failedDownloads(),
|
|
|
|
failedUploads(),
|
2021-04-27 19:29:15 +00:00
|
|
|
unreadMessages(new std::set<QString>()),
|
|
|
|
observersAmount(0)
|
2023-11-06 23:57:08 +00:00
|
|
|
{}
|
2020-08-11 22:49:51 +00:00
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
Models::MessageFeed::~MessageFeed() {
|
2021-04-27 19:29:15 +00:00
|
|
|
delete unreadMessages;
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
for (Shared::Message* message : storage)
|
2020-08-11 22:49:51 +00:00
|
|
|
delete message;
|
|
|
|
}
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
void Models::MessageFeed::addMessage(const Shared::Message& msg) {
|
2020-08-11 22:49:51 +00:00
|
|
|
QString id = msg.getId();
|
|
|
|
StorageById::const_iterator itr = indexById.find(id);
|
|
|
|
if (itr != indexById.end()) {
|
|
|
|
qDebug() << "received more then one message with the same id, skipping yet the new one";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Shared::Message* copy = new Shared::Message(msg);
|
|
|
|
StorageByTime::const_iterator tItr = indexByTime.upper_bound(msg.getTime());
|
|
|
|
int position;
|
2023-11-06 23:57:08 +00:00
|
|
|
if (tItr == indexByTime.end())
|
2020-08-11 22:49:51 +00:00
|
|
|
position = storage.size();
|
2023-11-06 23:57:08 +00:00
|
|
|
else
|
2020-08-11 22:49:51 +00:00
|
|
|
position = indexByTime.rank(tItr);
|
2023-11-06 23:57:08 +00:00
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
beginInsertRows(QModelIndex(), position, position);
|
|
|
|
storage.insert(copy);
|
|
|
|
endInsertRows();
|
2021-04-27 19:29:15 +00:00
|
|
|
|
|
|
|
emit newMessage(msg);
|
|
|
|
|
|
|
|
if (observersAmount == 0 && !msg.getForwarded()) { //not to notify when the message is delivered by the carbon copy
|
2022-01-15 12:36:49 +00:00
|
|
|
unreadMessages->insert(id); //cuz it could be my own one or the one I read on another device
|
2021-04-27 19:29:15 +00:00
|
|
|
emit unreadMessagesCountChanged();
|
|
|
|
emit unnoticedMessage(msg);
|
|
|
|
}
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
void Models::MessageFeed::changeMessage(const QString& id, const QMap<QString, QVariant>& data) {
|
2021-04-19 21:49:24 +00:00
|
|
|
StorageById::iterator itr = indexById.find(id);
|
|
|
|
if (itr == indexById.end()) {
|
|
|
|
qDebug() << "received a command to change a message, but the message couldn't be found, skipping";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Shared::Message* msg = *itr;
|
2021-04-23 11:53:48 +00:00
|
|
|
std::set<MessageRoles> changeRoles = detectChanges(*msg, data);
|
2021-04-19 21:49:24 +00:00
|
|
|
QModelIndex index = modelIndexByTime(id, msg->getTime());
|
|
|
|
Shared::Message::Change functor(data);
|
|
|
|
bool success = indexById.modify(itr, functor);
|
|
|
|
if (!success) {
|
|
|
|
qDebug() << "received a command to change a message, but something went wrong modifying message in the feed, throwing error";
|
|
|
|
throw 872;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (functor.hasIdBeenModified()) {
|
2021-04-23 11:53:48 +00:00
|
|
|
changeRoles.insert(MessageRoles::Id);
|
2021-04-27 19:29:15 +00:00
|
|
|
std::set<QString>::const_iterator umi = unreadMessages->find(id);
|
|
|
|
if (umi != unreadMessages->end()) {
|
|
|
|
unreadMessages->erase(umi);
|
|
|
|
unreadMessages->insert(msg->getId());
|
|
|
|
}
|
2021-04-19 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:26:19 +00:00
|
|
|
if (changeRoles.size() > 0) {
|
|
|
|
//change message is a final event in download/upload event train
|
|
|
|
//only after changeMessage we can consider the download is done
|
|
|
|
Progress::const_iterator dItr = downloads.find(id);
|
|
|
|
bool attachOrError = changeRoles.count(MessageRoles::Attach) > 0 || changeRoles.count(MessageRoles::Error);
|
|
|
|
if (dItr != downloads.end()) {
|
2021-04-23 11:53:48 +00:00
|
|
|
if (attachOrError) {
|
2021-04-28 20:26:19 +00:00
|
|
|
downloads.erase(dItr);
|
2021-04-23 11:53:48 +00:00
|
|
|
} else if (changeRoles.count(MessageRoles::Id) > 0) {
|
|
|
|
qreal progress = dItr->second;
|
2021-04-28 20:26:19 +00:00
|
|
|
downloads.erase(dItr);
|
|
|
|
downloads.insert(std::make_pair(msg->getId(), progress));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dItr = uploads.find(id);
|
|
|
|
if (dItr != uploads.end()) {
|
|
|
|
if (attachOrError) {
|
|
|
|
uploads.erase(dItr);
|
|
|
|
} else if (changeRoles.count(MessageRoles::Id) > 0) {
|
|
|
|
qreal progress = dItr->second;
|
|
|
|
uploads.erase(dItr);
|
|
|
|
uploads.insert(std::make_pair(msg->getId(), progress));
|
|
|
|
}
|
2021-04-19 21:49:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-28 20:26:19 +00:00
|
|
|
|
2021-05-14 19:49:38 +00:00
|
|
|
Err::const_iterator eitr = failedDownloads.find(id);
|
|
|
|
if (eitr != failedDownloads.end()) {
|
|
|
|
failedDownloads.erase(eitr);
|
|
|
|
changeRoles.insert(MessageRoles::Attach);
|
|
|
|
} else {
|
|
|
|
eitr = failedUploads.find(id);
|
|
|
|
if (eitr != failedUploads.end()) {
|
|
|
|
failedUploads.erase(eitr);
|
|
|
|
changeRoles.insert(MessageRoles::Attach);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:26:19 +00:00
|
|
|
QVector<int> cr;
|
2023-11-06 23:57:08 +00:00
|
|
|
for (MessageRoles role : changeRoles)
|
2021-04-28 20:26:19 +00:00
|
|
|
cr.push_back(role);
|
|
|
|
|
|
|
|
emit dataChanged(index, index, cr);
|
2022-04-19 17:24:41 +00:00
|
|
|
|
|
|
|
if (observersAmount == 0 && !msg->getForwarded() && changeRoles.count(MessageRoles::Text) > 0) {
|
|
|
|
unreadMessages->insert(id);
|
|
|
|
emit unreadMessagesCountChanged();
|
|
|
|
emit unnoticedMessage(*msg);
|
|
|
|
}
|
2021-04-19 21:49:24 +00:00
|
|
|
}
|
2021-04-22 22:41:32 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
std::set<Models::MessageFeed::MessageRoles> Models::MessageFeed::detectChanges(
|
|
|
|
const Shared::Message& msg,
|
|
|
|
const QMap<QString, QVariant>& data
|
|
|
|
) const {
|
2021-04-23 11:53:48 +00:00
|
|
|
std::set<MessageRoles> roles;
|
2021-04-22 22:41:32 +00:00
|
|
|
Shared::Message::State state = msg.getState();
|
|
|
|
QMap<QString, QVariant>::const_iterator itr = data.find("state");
|
2023-11-06 23:57:08 +00:00
|
|
|
if (itr != data.end() && static_cast<Shared::Message::State>(itr.value().toUInt()) != state)
|
2021-04-23 11:53:48 +00:00
|
|
|
roles.insert(MessageRoles::DeliveryState);
|
2021-04-22 22:41:32 +00:00
|
|
|
|
|
|
|
itr = data.find("outOfBandUrl");
|
|
|
|
bool att = false;
|
|
|
|
if (itr != data.end() && itr.value().toString() != msg.getOutOfBandUrl()) {
|
2021-04-23 11:53:48 +00:00
|
|
|
roles.insert(MessageRoles::Attach);
|
2021-04-22 22:41:32 +00:00
|
|
|
att = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!att) {
|
|
|
|
itr = data.find("attachPath");
|
|
|
|
if (itr != data.end() && itr.value().toString() != msg.getAttachPath()) {
|
2021-04-23 11:53:48 +00:00
|
|
|
roles.insert(MessageRoles::Attach);
|
2021-04-22 22:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == Shared::Message::State::error) {
|
|
|
|
itr = data.find("errorText");
|
2021-04-28 20:26:19 +00:00
|
|
|
if (itr != data.end() && itr.value().toString() != msg.getErrorText()) {
|
2021-04-23 11:53:48 +00:00
|
|
|
roles.insert(MessageRoles::Error);
|
2021-04-22 22:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
itr = data.find("body");
|
2021-04-28 20:26:19 +00:00
|
|
|
if (itr != data.end() && itr.value().toString() != msg.getBody()) {
|
2021-04-22 22:41:32 +00:00
|
|
|
QMap<QString, QVariant>::const_iterator dItr = data.find("stamp");
|
|
|
|
QDateTime correctionDate;
|
|
|
|
if (dItr != data.end()) {
|
|
|
|
correctionDate = dItr.value().toDateTime();
|
|
|
|
} else {
|
|
|
|
correctionDate = QDateTime::currentDateTimeUtc(); //in case there is no information about time of this correction it's applied
|
|
|
|
}
|
|
|
|
if (!msg.getEdited() || msg.getLastModified() < correctionDate) {
|
2021-04-23 11:53:48 +00:00
|
|
|
roles.insert(MessageRoles::Text);
|
|
|
|
roles.insert(MessageRoles::Correction);
|
2021-04-22 22:41:32 +00:00
|
|
|
}
|
2021-05-07 18:26:02 +00:00
|
|
|
} else {
|
|
|
|
QMap<QString, QVariant>::const_iterator dItr = data.find("stamp");
|
|
|
|
if (dItr != data.end()) {
|
|
|
|
QDateTime ntime = dItr.value().toDateTime();
|
|
|
|
if (msg.getTime() != ntime) {
|
|
|
|
roles.insert(MessageRoles::Date);
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 22:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return roles;
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::removeMessage(const QString& id) {
|
2023-11-05 19:29:44 +00:00
|
|
|
SHARED_UNUSED(id);
|
2022-03-27 19:05:31 +00:00
|
|
|
//todo;
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
Shared::Message Models::MessageFeed::getMessage(const QString& id) {
|
2022-03-27 19:05:31 +00:00
|
|
|
StorageById::iterator itr = indexById.find(id);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (itr == indexById.end())
|
2022-03-27 19:05:31 +00:00
|
|
|
throw NotFound(id.toStdString(), rosterItem->getJid().toStdString(), rosterItem->getAccountName().toStdString());
|
|
|
|
|
|
|
|
return **itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const {
|
2020-08-11 22:49:51 +00:00
|
|
|
int i = index.row();
|
|
|
|
QVariant answer;
|
2020-08-12 16:55:01 +00:00
|
|
|
|
|
|
|
StorageByTime::const_iterator itr = indexByTime.nth(i);
|
|
|
|
if (itr != indexByTime.end()) {
|
|
|
|
const Shared::Message* msg = *itr;
|
|
|
|
|
|
|
|
switch (role) {
|
2020-08-15 21:48:28 +00:00
|
|
|
case Qt::DisplayRole:
|
2021-02-01 22:55:15 +00:00
|
|
|
case Text: {
|
|
|
|
QString body = msg->getBody();
|
2023-11-06 23:57:08 +00:00
|
|
|
if (body != msg->getOutOfBandUrl())
|
2021-02-01 22:55:15 +00:00
|
|
|
answer = body;
|
|
|
|
}
|
2020-08-12 16:55:01 +00:00
|
|
|
break;
|
|
|
|
case Sender:
|
2021-01-07 21:50:12 +00:00
|
|
|
if (sentByMe(*msg)) {
|
|
|
|
answer = rosterItem->getAccountName();
|
2020-08-20 21:32:30 +00:00
|
|
|
} else {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (rosterItem->isRoom())
|
2021-01-07 21:50:12 +00:00
|
|
|
answer = msg->getFromResource();
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2020-08-20 21:32:30 +00:00
|
|
|
answer = rosterItem->getDisplayedName();
|
|
|
|
}
|
2020-08-12 16:55:01 +00:00
|
|
|
break;
|
|
|
|
case Date:
|
|
|
|
answer = msg->getTime();
|
|
|
|
break;
|
|
|
|
case DeliveryState:
|
|
|
|
answer = static_cast<unsigned int>(msg->getState());
|
|
|
|
break;
|
2023-11-06 23:57:08 +00:00
|
|
|
case Encryption:
|
|
|
|
answer = QVariant::fromValue(msg->getEncryption());
|
|
|
|
break;
|
2020-08-12 16:55:01 +00:00
|
|
|
case Correction:
|
2021-05-24 22:06:05 +00:00
|
|
|
answer.setValue(fillCorrection(*msg));;
|
2020-08-12 16:55:01 +00:00
|
|
|
break;
|
|
|
|
case SentByMe:
|
2020-08-20 21:32:30 +00:00
|
|
|
answer = sentByMe(*msg);
|
2020-08-12 16:55:01 +00:00
|
|
|
break;
|
2020-08-20 21:32:30 +00:00
|
|
|
case Avatar: {
|
|
|
|
QString path;
|
|
|
|
if (sentByMe(*msg)) {
|
|
|
|
path = rosterItem->getAccountAvatarPath();
|
|
|
|
} else if (!rosterItem->isRoom()) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (rosterItem->getAvatarState() != Shared::Avatar::empty)
|
2020-08-20 21:32:30 +00:00
|
|
|
path = rosterItem->getAvatarPath();
|
|
|
|
} else {
|
|
|
|
const Room* room = static_cast<const Room*>(rosterItem);
|
|
|
|
path = room->getParticipantIconPath(msg->getFromResource());
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (path.size() == 0)
|
2020-08-20 21:32:30 +00:00
|
|
|
answer = Shared::iconPath("user", true);
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2020-08-20 21:32:30 +00:00
|
|
|
answer = path;
|
|
|
|
}
|
2021-01-12 17:10:24 +00:00
|
|
|
break;
|
2021-01-14 11:22:02 +00:00
|
|
|
case Attach:
|
|
|
|
answer.setValue(fillAttach(*msg));
|
2020-08-12 16:55:01 +00:00
|
|
|
break;
|
2021-02-01 22:55:15 +00:00
|
|
|
case Id:
|
|
|
|
answer.setValue(msg->getId());
|
|
|
|
break;
|
2021-04-22 22:41:32 +00:00
|
|
|
break;
|
|
|
|
case Error:
|
|
|
|
answer.setValue(msg->getErrorText());
|
|
|
|
break;
|
2021-01-07 21:50:12 +00:00
|
|
|
case Bulk: {
|
|
|
|
FeedItem item;
|
2021-02-01 22:55:15 +00:00
|
|
|
item.id = msg->getId();
|
2022-04-24 15:52:29 +00:00
|
|
|
markMessageAsRead(item.id);
|
2021-04-27 19:29:15 +00:00
|
|
|
|
2021-01-07 21:50:12 +00:00
|
|
|
item.sentByMe = sentByMe(*msg);
|
|
|
|
item.date = msg->getTime();
|
|
|
|
item.state = msg->getState();
|
2023-11-06 23:57:08 +00:00
|
|
|
item.encryption = msg->getEncryption();
|
2021-04-22 22:41:32 +00:00
|
|
|
item.error = msg->getErrorText();
|
2021-05-24 22:06:05 +00:00
|
|
|
item.correction = fillCorrection(*msg);
|
2021-02-01 22:55:15 +00:00
|
|
|
|
|
|
|
QString body = msg->getBody();
|
2023-11-06 23:57:08 +00:00
|
|
|
if (body != msg->getOutOfBandUrl())
|
2021-02-01 22:55:15 +00:00
|
|
|
item.text = body;
|
|
|
|
|
2021-01-07 21:50:12 +00:00
|
|
|
item.avatar.clear();
|
|
|
|
if (item.sentByMe) {
|
|
|
|
item.sender = rosterItem->getAccountName();
|
|
|
|
item.avatar = rosterItem->getAccountAvatarPath();
|
|
|
|
} else {
|
|
|
|
if (rosterItem->isRoom()) {
|
|
|
|
item.sender = msg->getFromResource();
|
|
|
|
const Room* room = static_cast<const Room*>(rosterItem);
|
|
|
|
item.avatar = room->getParticipantIconPath(msg->getFromResource());
|
|
|
|
} else {
|
|
|
|
item.sender = rosterItem->getDisplayedName();
|
2023-08-15 15:28:25 +00:00
|
|
|
if (rosterItem->getAvatarState() != Shared::Avatar::empty)
|
2021-01-07 21:50:12 +00:00
|
|
|
item.avatar = rosterItem->getAvatarPath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (item.avatar.size() == 0)
|
2021-01-07 21:50:12 +00:00
|
|
|
item.avatar = Shared::iconPath("user", true);
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2021-01-14 11:22:02 +00:00
|
|
|
item.attach = fillAttach(*msg);
|
2021-01-07 21:50:12 +00:00
|
|
|
answer.setValue(item);
|
|
|
|
}
|
2021-01-12 17:10:24 +00:00
|
|
|
break;
|
2020-08-12 16:55:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
int Models::MessageFeed::rowCount(const QModelIndex& parent) const {
|
|
|
|
SHARED_UNUSED(parent);
|
2020-08-20 21:32:30 +00:00
|
|
|
return storage.size();
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
bool Models::MessageFeed::markMessageAsRead(const QString& id) const {
|
2022-04-24 15:52:29 +00:00
|
|
|
std::set<QString>::const_iterator umi = unreadMessages->find(id);
|
|
|
|
if (umi != unreadMessages->end()) {
|
|
|
|
unreadMessages->erase(umi);
|
|
|
|
emit unreadMessagesCountChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
unsigned int Models::MessageFeed::unreadMessagesCount() const {
|
2021-04-27 19:29:15 +00:00
|
|
|
return unreadMessages->size();
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
bool Models::MessageFeed::canFetchMore(const QModelIndex& parent) const {
|
|
|
|
SHARED_UNUSED(parent);
|
2020-08-11 22:49:51 +00:00
|
|
|
return syncState == incomplete;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::fetchMore(const QModelIndex& parent) {
|
|
|
|
SHARED_UNUSED(parent);
|
2020-08-11 22:49:51 +00:00
|
|
|
if (syncState == incomplete) {
|
2020-08-21 20:57:48 +00:00
|
|
|
syncState = syncing;
|
2021-04-30 20:07:00 +00:00
|
|
|
emit syncStateChange(syncState);
|
2020-08-11 22:49:51 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (storage.size() == 0)
|
2020-08-11 22:49:51 +00:00
|
|
|
emit requestArchive("");
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2020-08-12 16:55:01 +00:00
|
|
|
emit requestArchive((*indexByTime.rbegin())->getId());
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::responseArchive(const std::list<Shared::Message> list, bool last) {
|
2020-08-12 16:55:01 +00:00
|
|
|
Storage::size_type size = storage.size();
|
2020-08-11 22:49:51 +00:00
|
|
|
|
2020-08-12 16:55:01 +00:00
|
|
|
beginInsertRows(QModelIndex(), size, size + list.size() - 1);
|
2020-08-11 22:49:51 +00:00
|
|
|
for (const Shared::Message& msg : list) {
|
|
|
|
Shared::Message* copy = new Shared::Message(msg);
|
|
|
|
storage.insert(copy);
|
|
|
|
}
|
|
|
|
endInsertRows();
|
2020-08-21 20:57:48 +00:00
|
|
|
|
|
|
|
if (syncState == syncing) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (last)
|
2020-08-21 20:57:48 +00:00
|
|
|
syncState = complete;
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2020-08-21 20:57:48 +00:00
|
|
|
syncState = incomplete;
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2021-04-30 20:07:00 +00:00
|
|
|
emit syncStateChange(syncState);
|
2020-08-21 20:57:48 +00:00
|
|
|
}
|
2020-08-11 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QModelIndex Models::MessageFeed::index(int row, int column, const QModelIndex& parent) const{
|
|
|
|
if (!hasIndex(row, column, parent))
|
2021-05-04 14:09:41 +00:00
|
|
|
return QModelIndex();
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2021-05-04 14:09:41 +00:00
|
|
|
StorageByTime::iterator itr = indexByTime.nth(row);
|
2023-11-06 23:57:08 +00:00
|
|
|
if (itr == indexByTime.end())
|
2021-05-04 14:09:41 +00:00
|
|
|
return QModelIndex();
|
2023-11-06 23:57:08 +00:00
|
|
|
|
|
|
|
Shared::Message* msg = *itr;
|
|
|
|
return createIndex(row, column, msg);
|
2021-05-04 14:09:41 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 15:27:03 +00:00
|
|
|
QHash<int, QByteArray> Models::MessageFeed::roleNames() const {return roles;}
|
2020-08-20 21:32:30 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
bool Models::MessageFeed::sentByMe(const Shared::Message& msg) const {
|
2020-08-20 21:32:30 +00:00
|
|
|
if (rosterItem->isRoom()) {
|
|
|
|
const Room* room = static_cast<const Room*>(rosterItem);
|
|
|
|
return room->getNick().toLower() == msg.getFromResource().toLower();
|
|
|
|
} else {
|
|
|
|
return msg.getOutgoing();
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 11:22:02 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
Models::Attachment Models::MessageFeed::fillAttach(const Shared::Message& msg) const {
|
2021-01-14 11:22:02 +00:00
|
|
|
::Models::Attachment att;
|
2021-05-14 19:49:38 +00:00
|
|
|
QString id = msg.getId();
|
2021-01-14 11:22:02 +00:00
|
|
|
|
|
|
|
att.localPath = msg.getAttachPath();
|
|
|
|
att.remotePath = msg.getOutOfBandUrl();
|
|
|
|
|
|
|
|
if (att.remotePath.size() == 0) {
|
|
|
|
if (att.localPath.size() == 0) {
|
|
|
|
att.state = none;
|
|
|
|
} else {
|
2021-05-14 19:49:38 +00:00
|
|
|
Err::const_iterator eitr = failedUploads.find(id);
|
|
|
|
if (eitr != failedUploads.end()) {
|
|
|
|
att.state = errorUpload;
|
|
|
|
att.error = eitr->second;
|
2021-01-14 11:22:02 +00:00
|
|
|
} else {
|
2021-05-14 19:49:38 +00:00
|
|
|
Progress::const_iterator itr = uploads.find(id);
|
|
|
|
if (itr == uploads.end()) {
|
|
|
|
att.state = local;
|
|
|
|
} else {
|
|
|
|
att.state = uploading;
|
|
|
|
att.progress = itr->second;
|
|
|
|
}
|
2021-01-14 11:22:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (att.localPath.size() == 0) {
|
2021-05-14 19:49:38 +00:00
|
|
|
Err::const_iterator eitr = failedDownloads.find(id);
|
|
|
|
if (eitr != failedDownloads.end()) {
|
|
|
|
att.state = errorDownload;
|
|
|
|
att.error = eitr->second;
|
2021-01-14 11:22:02 +00:00
|
|
|
} else {
|
2021-05-14 19:49:38 +00:00
|
|
|
Progress::const_iterator itr = downloads.find(id);
|
|
|
|
if (itr == downloads.end()) {
|
|
|
|
att.state = remote;
|
|
|
|
} else {
|
|
|
|
att.state = downloading;
|
|
|
|
att.progress = itr->second;
|
|
|
|
}
|
2021-01-14 11:22:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
att.state = ready;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return att;
|
|
|
|
}
|
2021-02-06 11:02:42 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
Models::Edition Models::MessageFeed::fillCorrection(const Shared::Message& msg) const {
|
2021-05-24 22:06:05 +00:00
|
|
|
::Models::Edition ed({msg.getEdited(), msg.getOriginalBody(), msg.getLastModified()});
|
|
|
|
|
|
|
|
return ed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::downloadAttachment(const QString& messageId) {
|
2021-05-14 19:49:38 +00:00
|
|
|
bool notify = false;
|
|
|
|
Err::const_iterator eitr = failedDownloads.find(messageId);
|
|
|
|
if (eitr != failedDownloads.end()) {
|
|
|
|
failedDownloads.erase(eitr);
|
|
|
|
notify = true;
|
|
|
|
}
|
|
|
|
|
2021-02-27 12:21:27 +00:00
|
|
|
QModelIndex ind = modelIndexById(messageId);
|
|
|
|
if (ind.isValid()) {
|
|
|
|
std::pair<Progress::iterator, bool> progressPair = downloads.insert(std::make_pair(messageId, 0));
|
2021-03-22 18:04:26 +00:00
|
|
|
if (progressPair.second) { //Only to take action if we weren't already downloading it
|
2021-02-27 12:21:27 +00:00
|
|
|
Shared::Message* msg = static_cast<Shared::Message*>(ind.internalPointer());
|
2021-05-14 19:49:38 +00:00
|
|
|
notify = true;
|
2021-04-18 12:49:20 +00:00
|
|
|
emit fileDownloadRequest(msg->getOutOfBandUrl());
|
2021-02-27 12:21:27 +00:00
|
|
|
} else {
|
|
|
|
qDebug() << "Attachment download for message with id" << messageId << "is already in progress, skipping";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qDebug() << "An attempt to download an attachment for the message that doesn't exist. ID:" << messageId;
|
|
|
|
}
|
2021-05-14 19:49:38 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (notify)
|
2021-05-14 19:49:38 +00:00
|
|
|
emit dataChanged(ind, ind, {MessageRoles::Attach});
|
2021-02-06 11:02:42 +00:00
|
|
|
}
|
2021-02-27 12:21:27 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
bool Models::MessageFeed::registerUpload(const QString& messageId) {
|
2021-05-14 19:49:38 +00:00
|
|
|
bool success = uploads.insert(std::make_pair(messageId, 0)).second;
|
|
|
|
|
|
|
|
QVector<int> roles({});
|
|
|
|
Err::const_iterator eitr = failedUploads.find(messageId);
|
|
|
|
if (eitr != failedUploads.end()) {
|
|
|
|
failedUploads.erase(eitr);
|
|
|
|
roles.push_back(MessageRoles::Attach);
|
|
|
|
} else if (success) {
|
|
|
|
roles.push_back(MessageRoles::Attach);
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex ind = modelIndexById(messageId);
|
|
|
|
emit dataChanged(ind, ind, roles);
|
|
|
|
|
|
|
|
return success;
|
2021-04-23 11:53:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::fileProgress(const QString& messageId, qreal value, bool up) {
|
2021-04-18 12:49:20 +00:00
|
|
|
Progress* pr = 0;
|
2021-05-14 19:49:38 +00:00
|
|
|
Err* err = 0;
|
2021-04-18 12:49:20 +00:00
|
|
|
if (up) {
|
|
|
|
pr = &uploads;
|
2021-05-14 19:49:38 +00:00
|
|
|
err = &failedUploads;
|
2021-04-18 12:49:20 +00:00
|
|
|
} else {
|
|
|
|
pr = &downloads;
|
2021-05-14 19:49:38 +00:00
|
|
|
err = &failedDownloads;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<int> roles({});
|
|
|
|
Err::const_iterator eitr = err->find(messageId);
|
|
|
|
if (eitr != err->end() && value != 1) { //like I want to clear this state when the download is started anew
|
|
|
|
err->erase(eitr);
|
|
|
|
roles.push_back(MessageRoles::Attach);
|
2021-04-18 12:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Progress::iterator itr = pr->find(messageId);
|
|
|
|
if (itr != pr->end()) {
|
2021-02-27 12:21:27 +00:00
|
|
|
itr->second = value;
|
|
|
|
QModelIndex ind = modelIndexById(messageId);
|
2021-05-14 19:49:38 +00:00
|
|
|
emit dataChanged(ind, ind, roles);
|
2021-02-27 12:21:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::fileComplete(const QString& messageId, bool up) {
|
2021-04-19 21:49:24 +00:00
|
|
|
fileProgress(messageId, 1, up);
|
2021-04-18 12:49:20 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::fileError(const QString& messageId, const QString& error, bool up) {
|
2021-05-14 19:49:38 +00:00
|
|
|
Err* failed;
|
|
|
|
Progress* loads;
|
|
|
|
if (up) {
|
|
|
|
failed = &failedUploads;
|
|
|
|
loads = &uploads;
|
|
|
|
} else {
|
|
|
|
failed = &failedDownloads;
|
|
|
|
loads = &downloads;
|
|
|
|
}
|
|
|
|
|
|
|
|
Progress::iterator pitr = loads->find(messageId);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (pitr != loads->end())
|
2021-05-14 19:49:38 +00:00
|
|
|
loads->erase(pitr);
|
|
|
|
|
|
|
|
std::pair<Err::iterator, bool> pair = failed->insert(std::make_pair(messageId, error));
|
2023-08-15 15:28:25 +00:00
|
|
|
if (!pair.second)
|
2021-05-14 19:49:38 +00:00
|
|
|
pair.first->second = error;
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2021-05-14 19:49:38 +00:00
|
|
|
QModelIndex ind = modelIndexById(messageId);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (ind.isValid())
|
2021-05-14 19:49:38 +00:00
|
|
|
emit dataChanged(ind, ind, {MessageRoles::Attach});
|
2021-04-18 12:49:20 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::incrementObservers() {
|
2021-04-27 19:29:15 +00:00
|
|
|
++observersAmount;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::decrementObservers() {
|
2021-04-27 19:29:15 +00:00
|
|
|
--observersAmount;
|
|
|
|
}
|
|
|
|
|
2021-04-18 12:49:20 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QModelIndex Models::MessageFeed::modelIndexById(const QString& id) const {
|
2021-02-27 12:21:27 +00:00
|
|
|
StorageById::const_iterator itr = indexById.find(id);
|
|
|
|
if (itr != indexById.end()) {
|
|
|
|
Shared::Message* msg = *itr;
|
2021-04-19 21:49:24 +00:00
|
|
|
return modelIndexByTime(id, msg->getTime());
|
2021-02-27 12:21:27 +00:00
|
|
|
}
|
2021-04-19 21:49:24 +00:00
|
|
|
|
|
|
|
return QModelIndex();
|
2021-02-27 12:21:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QModelIndex Models::MessageFeed::modelIndexByTime(const QString& id, const QDateTime& time) const {
|
2021-05-10 21:06:40 +00:00
|
|
|
if (indexByTime.size() > 0) {
|
2021-05-14 19:49:38 +00:00
|
|
|
StorageByTime::const_iterator tItr = indexByTime.lower_bound(time);
|
|
|
|
StorageByTime::const_iterator tEnd = indexByTime.upper_bound(time);
|
2021-05-10 21:06:40 +00:00
|
|
|
bool found = false;
|
2021-05-14 19:49:38 +00:00
|
|
|
while (tItr != tEnd) {
|
|
|
|
if (id == (*tItr)->getId()) {
|
2021-05-10 21:06:40 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-14 19:49:38 +00:00
|
|
|
++tItr;
|
2021-05-10 21:06:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:49:38 +00:00
|
|
|
if (found) {
|
2021-05-10 21:06:40 +00:00
|
|
|
int position = indexByTime.rank(tItr);
|
|
|
|
return createIndex(position, 0, *tItr);
|
2021-04-19 21:49:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
2021-04-28 20:26:19 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::reportLocalPathInvalid(const QString& messageId) {
|
2021-04-28 20:26:19 +00:00
|
|
|
StorageById::iterator itr = indexById.find(messageId);
|
|
|
|
if (itr == indexById.end()) {
|
|
|
|
qDebug() << "received a command to change a message, but the message couldn't be found, skipping";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Shared::Message* msg = *itr;
|
|
|
|
|
|
|
|
emit localPathInvalid(msg->getAttachPath());
|
|
|
|
|
2021-05-14 19:49:38 +00:00
|
|
|
//gonna change the message in current model right away, to prevent spam on each attempt to draw element
|
2021-04-28 20:26:19 +00:00
|
|
|
QModelIndex index = modelIndexByTime(messageId, msg->getTime());
|
|
|
|
msg->setAttachPath("");
|
|
|
|
|
|
|
|
emit dataChanged(index, index, {MessageRoles::Attach});
|
|
|
|
}
|
2021-04-30 20:07:00 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
Models::MessageFeed::SyncState Models::MessageFeed::getSyncState() const {
|
2021-04-30 20:07:00 +00:00
|
|
|
return syncState;
|
|
|
|
}
|
2021-05-16 21:52:59 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void Models::MessageFeed::requestLatestMessages() {
|
2021-05-16 21:52:59 +00:00
|
|
|
if (syncState != syncing) {
|
|
|
|
syncState = syncing;
|
|
|
|
emit syncStateChange(syncState);
|
|
|
|
|
|
|
|
emit requestArchive("");
|
|
|
|
}
|
|
|
|
}
|