diff --git a/src/timeline2/TimelineModel.h b/src/timeline2/TimelineModel.h index ca8d4ad6..59321119 100644 --- a/src/timeline2/TimelineModel.h +++ b/src/timeline2/TimelineModel.h @@ -7,6 +7,9 @@ #include #include +#include "Logging.h" +#include "MatrixClient.h" + namespace qml_mtx_events { Q_NAMESPACE @@ -110,6 +113,8 @@ public: Q_INVOKABLE void viewRawMessage(QString id) const; void addEvents(const mtx::responses::Timeline &events); + template + void sendMessage(const T &msg); public slots: void fetchHistory(); @@ -121,6 +126,8 @@ private slots: signals: void oldMessagesRetrieved(const mtx::responses::Messages &res); + void messageFailed(const std::string txn_id); + void messageSent(const std::string txn_id, std::string event_id); private: DecryptionResult decryptEvent( @@ -139,3 +146,25 @@ private: QHash userColors; }; + +template +void +TimelineModel::sendMessage(const T &msg) +{ + auto txn_id = http::client()->generate_txn_id(); + http::client()->send_room_message( + room_id_.toStdString(), + txn_id, + msg, + [this, txn_id](const mtx::responses::EventId &res, mtx::http::RequestErr err) { + if (err) { + const int status_code = static_cast(err->status_code); + nhlog::net()->warn("[{}] failed to send message: {} {}", + txn_id, + err->matrix_error.error, + status_code); + emit messageFailed(txn_id); + } + emit messageSent(txn_id, res.event_id.to_string()); + }); +} diff --git a/src/timeline2/TimelineViewManager.cpp b/src/timeline2/TimelineViewManager.cpp index eb9bea54..6aa2ff43 100644 --- a/src/timeline2/TimelineViewManager.cpp +++ b/src/timeline2/TimelineViewManager.cpp @@ -62,3 +62,41 @@ TimelineViewManager::initWithMessages(const std::mapaddEvents(e.second); } } + +void +TimelineViewManager::queueTextMessage(const QString &msg) +{ + mtx::events::msg::Text text = {}; + text.body = msg.trimmed().toStdString(); + text.format = "org.matrix.custom.html"; + text.formatted_body = utils::markdownToHtml(msg).toStdString(); + + if (timeline_) + timeline_->sendMessage(text); +} + +void +TimelineViewManager::queueReplyMessage(const QString &reply, const RelatedInfo &related) +{ + mtx::events::msg::Text text = {}; + + QString body; + bool firstLine = true; + for (const auto &line : related.quoted_body.splitRef("\n")) { + if (firstLine) { + firstLine = false; + body = QString("> <%1> %2\n").arg(related.quoted_user).arg(line); + } else { + body = QString("%1\n> %2\n").arg(body).arg(line); + } + } + + text.body = QString("%1\n%2").arg(body).arg(reply).toStdString(); + text.format = "org.matrix.custom.html"; + text.formatted_body = + utils::getFormattedQuoteBody(related, utils::markdownToHtml(reply)).toStdString(); + text.relates_to.in_reply_to.event_id = related.related_event; + + if (timeline_) + timeline_->sendMessage(text); +} diff --git a/src/timeline2/TimelineViewManager.h b/src/timeline2/TimelineViewManager.h index 1bec8746..7ec0da5f 100644 --- a/src/timeline2/TimelineViewManager.h +++ b/src/timeline2/TimelineViewManager.h @@ -46,8 +46,8 @@ public slots: void setHistoryView(const QString &room_id); - void queueTextMessage(const QString &msg) {} - void queueReplyMessage(const QString &reply, const RelatedInfo &related) {} + void queueTextMessage(const QString &msg); + void queueReplyMessage(const QString &reply, const RelatedInfo &related); void queueEmoteMessage(const QString &msg) {} void queueImageMessage(const QString &roomid, const QString &filename,