Try to fix messages getting stuck by sometimes sending them twice and never failing them

This commit is contained in:
Nicolas Werner 2020-04-17 01:02:32 +02:00
parent 076a1c3607
commit eff8af6fac
2 changed files with 9 additions and 23 deletions

View File

@ -144,15 +144,9 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj
connect( connect(
this, &TimelineModel::oldMessagesRetrieved, this, &TimelineModel::addBackwardsEvents); this, &TimelineModel::oldMessagesRetrieved, this, &TimelineModel::addBackwardsEvents);
connect(this, &TimelineModel::messageFailed, this, [this](QString txn_id) { connect(this, &TimelineModel::messageFailed, this, [this](QString txn_id) {
pending.removeOne(txn_id); nhlog::ui()->error("Failed to send {}, retrying", txn_id.toStdString());
failed.insert(txn_id);
int idx = idToIndex(txn_id); QTimer::singleShot(5000, this, [this]() { emit nextPendingMessage(); });
if (idx < 0) {
nhlog::ui()->warn("Failed index out of range");
return;
}
isProcessingPending = false;
emit dataChanged(index(idx, 0), index(idx, 0));
}); });
connect(this, &TimelineModel::messageSent, this, [this](QString txn_id, QString event_id) { connect(this, &TimelineModel::messageSent, this, [this](QString txn_id, QString event_id) {
pending.removeOne(txn_id); pending.removeOne(txn_id);
@ -181,7 +175,6 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj
// ask to be notified for read receipts // ask to be notified for read receipts
cache::addPendingReceipt(room_id_, event_id); cache::addPendingReceipt(room_id_, event_id);
isProcessingPending = false;
emit dataChanged(index(idx, 0), index(idx, 0)); emit dataChanged(index(idx, 0), index(idx, 0));
if (pending.size() > 0) if (pending.size() > 0)
@ -334,8 +327,6 @@ TimelineModel::data(const QString &id, int role) const
// only show read receipts for messages not from us // only show read receipts for messages not from us
if (acc::sender(event) != http::client()->user_id().to_string()) if (acc::sender(event) != http::client()->user_id().to_string())
return qml_mtx_events::Empty; return qml_mtx_events::Empty;
else if (failed.contains(id))
return qml_mtx_events::Failed;
else if (pending.contains(id)) else if (pending.contains(id))
return qml_mtx_events::Sent; return qml_mtx_events::Sent;
else if (read.contains(id) || cache::readReceipts(id, room_id_).size() > 1) else if (read.contains(id) || cache::readReceipts(id, room_id_).size() > 1)
@ -458,10 +449,11 @@ TimelineModel::fetchMore(const QModelIndex &)
http::client()->messages( http::client()->messages(
opts, [this, opts](const mtx::responses::Messages &res, mtx::http::RequestErr err) { opts, [this, opts](const mtx::responses::Messages &res, mtx::http::RequestErr err) {
if (err) { if (err) {
nhlog::net()->error("failed to call /messages ({}): {} - {}", nhlog::net()->error("failed to call /messages ({}): {} - {} - {}",
opts.room_id, opts.room_id,
mtx::errors::to_string(err->matrix_error.errcode), mtx::errors::to_string(err->matrix_error.errcode),
err->matrix_error.error); err->matrix_error.error,
err->parse_error);
paginationInProgress = false; paginationInProgress = false;
return; return;
} }
@ -1266,11 +1258,9 @@ struct SendMessageVisitor
void void
TimelineModel::processOnePendingMessage() TimelineModel::processOnePendingMessage()
{ {
if (isProcessingPending || pending.isEmpty()) if (pending.isEmpty())
return; return;
isProcessingPending = true;
QString txn_id_qstr = pending.first(); QString txn_id_qstr = pending.first();
auto event = events.value(txn_id_qstr); auto event = events.value(txn_id_qstr);
@ -1298,8 +1288,7 @@ TimelineModel::addPendingMessage(mtx::events::collections::TimelineEvents event)
endInsertRows(); endInsertRows();
updateLastMessage(); updateLastMessage();
if (!isProcessingPending) emit nextPendingMessage();
emit nextPendingMessage();
} }
bool bool

View File

@ -89,8 +89,6 @@ enum EventState
Sent, Sent,
//! When the message is loaded from cache or backfill. //! When the message is loaded from cache or backfill.
Empty, Empty,
//! When the message failed to send
Failed,
}; };
Q_ENUM_NS(EventState) Q_ENUM_NS(EventState)
} }
@ -262,7 +260,7 @@ private:
void readEvent(const std::string &id); void readEvent(const std::string &id);
QHash<QString, mtx::events::collections::TimelineEvents> events; QHash<QString, mtx::events::collections::TimelineEvents> events;
QSet<QString> failed, read; QSet<QString> read;
QList<QString> pending; QList<QString> pending;
std::vector<QString> eventOrder; std::vector<QString> eventOrder;
@ -271,7 +269,6 @@ private:
bool isInitialSync = true; bool isInitialSync = true;
bool paginationInProgress = false; bool paginationInProgress = false;
bool isProcessingPending = false;
QString currentId; QString currentId;
QString reply_; QString reply_;