mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-29 14:18:49 +03:00
parent
a44a4f36af
commit
b5ae84c3c3
11 changed files with 747 additions and 686 deletions
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include <QtNetwork/QNetworkAccessManager>
|
#include <QtNetwork/QNetworkAccessManager>
|
||||||
|
|
||||||
|
#include "MessageEvent.h"
|
||||||
#include "Profile.h"
|
#include "Profile.h"
|
||||||
#include "RoomMessages.h"
|
#include "RoomMessages.h"
|
||||||
#include "Sync.h"
|
#include "Sync.h"
|
||||||
|
@ -36,9 +37,13 @@ public:
|
||||||
// Client API.
|
// Client API.
|
||||||
void initialSync() noexcept;
|
void initialSync() noexcept;
|
||||||
void sync() noexcept;
|
void sync() noexcept;
|
||||||
void sendTextMessage(const QString &roomid, const QString &msg) noexcept;
|
void sendRoomMessage(matrix::events::MessageEventType ty,
|
||||||
|
const QString &roomid,
|
||||||
|
const QString &msg) noexcept;
|
||||||
void login(const QString &username, const QString &password) noexcept;
|
void login(const QString &username, const QString &password) noexcept;
|
||||||
void registerUser(const QString &username, const QString &password, const QString &server) noexcept;
|
void registerUser(const QString &username,
|
||||||
|
const QString &password,
|
||||||
|
const QString &server) noexcept;
|
||||||
void versions() noexcept;
|
void versions() noexcept;
|
||||||
void fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url);
|
void fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url);
|
||||||
void fetchUserAvatar(const QString &userId, const QUrl &avatarUrl);
|
void fetchUserAvatar(const QString &userId, const QUrl &avatarUrl);
|
||||||
|
@ -68,7 +73,9 @@ signals:
|
||||||
void loggedOut();
|
void loggedOut();
|
||||||
|
|
||||||
void loginSuccess(const QString &userid, const QString &homeserver, const QString &token);
|
void loginSuccess(const QString &userid, const QString &homeserver, const QString &token);
|
||||||
void registerSuccess(const QString &userid, const QString &homeserver, const QString &token);
|
void registerSuccess(const QString &userid,
|
||||||
|
const QString &homeserver,
|
||||||
|
const QString &token);
|
||||||
void versionSuccess();
|
void versionSuccess();
|
||||||
|
|
||||||
void roomAvatarRetrieved(const QString &roomid, const QPixmap &img);
|
void roomAvatarRetrieved(const QString &roomid, const QPixmap &img);
|
||||||
|
@ -82,6 +89,7 @@ signals:
|
||||||
void syncCompleted(const SyncResponse &response);
|
void syncCompleted(const SyncResponse &response);
|
||||||
void syncFailed(const QString &msg);
|
void syncFailed(const QString &msg);
|
||||||
void messageSent(const QString &event_id, const QString &roomid, const int txn_id);
|
void messageSent(const QString &event_id, const QString &roomid, const int txn_id);
|
||||||
|
void emoteSent(const QString &event_id, const QString &roomid, const int txn_id);
|
||||||
void messagesRetrieved(const QString &room_id, const RoomMessages &msgs);
|
void messagesRetrieved(const QString &room_id, const RoomMessages &msgs);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
@ -99,26 +107,26 @@ private:
|
||||||
Messages,
|
Messages,
|
||||||
Register,
|
Register,
|
||||||
RoomAvatar,
|
RoomAvatar,
|
||||||
UserAvatar,
|
SendRoomMessage,
|
||||||
SendTextMessage,
|
|
||||||
Sync,
|
Sync,
|
||||||
|
UserAvatar,
|
||||||
Versions,
|
Versions,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Response handlers.
|
// Response handlers.
|
||||||
|
void onGetOwnAvatarResponse(QNetworkReply *reply);
|
||||||
|
void onGetOwnProfileResponse(QNetworkReply *reply);
|
||||||
|
void onImageResponse(QNetworkReply *reply);
|
||||||
|
void onInitialSyncResponse(QNetworkReply *reply);
|
||||||
void onLoginResponse(QNetworkReply *reply);
|
void onLoginResponse(QNetworkReply *reply);
|
||||||
void onLogoutResponse(QNetworkReply *reply);
|
void onLogoutResponse(QNetworkReply *reply);
|
||||||
void onRegisterResponse(QNetworkReply *reply);
|
|
||||||
void onVersionsResponse(QNetworkReply *reply);
|
|
||||||
void onGetOwnProfileResponse(QNetworkReply *reply);
|
|
||||||
void onGetOwnAvatarResponse(QNetworkReply *reply);
|
|
||||||
void onSendTextMessageResponse(QNetworkReply *reply);
|
|
||||||
void onInitialSyncResponse(QNetworkReply *reply);
|
|
||||||
void onSyncResponse(QNetworkReply *reply);
|
|
||||||
void onRoomAvatarResponse(QNetworkReply *reply);
|
|
||||||
void onUserAvatarResponse(QNetworkReply *reply);
|
|
||||||
void onImageResponse(QNetworkReply *reply);
|
|
||||||
void onMessagesResponse(QNetworkReply *reply);
|
void onMessagesResponse(QNetworkReply *reply);
|
||||||
|
void onRegisterResponse(QNetworkReply *reply);
|
||||||
|
void onRoomAvatarResponse(QNetworkReply *reply);
|
||||||
|
void onSendRoomMessage(QNetworkReply *reply);
|
||||||
|
void onSyncResponse(QNetworkReply *reply);
|
||||||
|
void onUserAvatarResponse(QNetworkReply *reply);
|
||||||
|
void onVersionsResponse(QNetworkReply *reply);
|
||||||
|
|
||||||
// Client API prefix.
|
// Client API prefix.
|
||||||
QString api_url_;
|
QString api_url_;
|
||||||
|
|
|
@ -25,6 +25,8 @@
|
||||||
#include "EmojiPickButton.h"
|
#include "EmojiPickButton.h"
|
||||||
#include "FlatButton.h"
|
#include "FlatButton.h"
|
||||||
|
|
||||||
|
static const QString EMOTE_COMMAND("/me ");
|
||||||
|
|
||||||
class FilteredTextEdit : public QTextEdit
|
class FilteredTextEdit : public QTextEdit
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -36,7 +38,7 @@ signals:
|
||||||
void enterPressed();
|
void enterPressed();
|
||||||
};
|
};
|
||||||
|
|
||||||
class TextInputWidget : public QWidget
|
class TextInputWidget : public QFrame
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
@ -53,11 +55,11 @@ private slots:
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void sendTextMessage(QString msg);
|
void sendTextMessage(QString msg);
|
||||||
|
void sendEmoteMessage(QString msg);
|
||||||
protected:
|
|
||||||
void paintEvent(QPaintEvent *event) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QString parseEmoteCommand(const QString &cmd);
|
||||||
|
|
||||||
QHBoxLayout *top_layout_;
|
QHBoxLayout *top_layout_;
|
||||||
FilteredTextEdit *input_;
|
FilteredTextEdit *input_;
|
||||||
|
|
||||||
|
|
|
@ -50,8 +50,11 @@ public:
|
||||||
QWidget *parent = 0);
|
QWidget *parent = 0);
|
||||||
|
|
||||||
// For local messages.
|
// For local messages.
|
||||||
TimelineItem(const QString &userid, QString body, QWidget *parent = 0);
|
TimelineItem(events::MessageEventType ty,
|
||||||
TimelineItem(QString body, QWidget *parent = 0);
|
const QString &userid,
|
||||||
|
QString body,
|
||||||
|
bool withSender,
|
||||||
|
QWidget *parent = 0);
|
||||||
|
|
||||||
TimelineItem(ImageItem *img,
|
TimelineItem(ImageItem *img,
|
||||||
const events::MessageEvent<msgs::Image> &e,
|
const events::MessageEvent<msgs::Image> &e,
|
||||||
|
|
|
@ -27,8 +27,9 @@
|
||||||
#include "Sync.h"
|
#include "Sync.h"
|
||||||
#include "TimelineItem.h"
|
#include "TimelineItem.h"
|
||||||
|
|
||||||
#include "Image.h"
|
|
||||||
#include "Emote.h"
|
#include "Emote.h"
|
||||||
|
#include "Image.h"
|
||||||
|
#include "MessageEvent.h"
|
||||||
#include "Notice.h"
|
#include "Notice.h"
|
||||||
#include "RoomInfoListItem.h"
|
#include "RoomInfoListItem.h"
|
||||||
#include "Text.h"
|
#include "Text.h"
|
||||||
|
@ -83,7 +84,7 @@ public:
|
||||||
|
|
||||||
// Add new events at the end of the timeline.
|
// Add new events at the end of the timeline.
|
||||||
int addEvents(const Timeline &timeline);
|
int addEvents(const Timeline &timeline);
|
||||||
void addUserTextMessage(const QString &msg, int txn_id);
|
void addUserMessage(matrix::events::MessageEventType ty, const QString &msg, int txn_id);
|
||||||
void updatePendingMessage(int txn_id, QString event_id);
|
void updatePendingMessage(int txn_id, QString event_id);
|
||||||
void scrollDown();
|
void scrollDown();
|
||||||
|
|
||||||
|
@ -100,14 +101,19 @@ signals:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init();
|
void init();
|
||||||
void removePendingMessage(const events::MessageEvent<msgs::Text> &e);
|
|
||||||
void addTimelineItem(TimelineItem *item, TimelineDirection direction);
|
void addTimelineItem(TimelineItem *item, TimelineDirection direction);
|
||||||
void updateLastSender(const QString &user_id, TimelineDirection direction);
|
void updateLastSender(const QString &user_id, TimelineDirection direction);
|
||||||
void notifyForLastEvent();
|
void notifyForLastEvent();
|
||||||
|
|
||||||
// Used to determine whether or not we should prefix a message with the sender's name.
|
// Used to determine whether or not we should prefix a message with the sender's name.
|
||||||
bool isSenderRendered(const QString &user_id, TimelineDirection direction);
|
bool isSenderRendered(const QString &user_id, TimelineDirection direction);
|
||||||
bool isPendingMessage(const events::MessageEvent<msgs::Text> &e, const QString &userid);
|
|
||||||
|
template<class T>
|
||||||
|
bool isPendingMessage(const events::MessageEvent<T> &e, const QString &userid);
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void removePendingMessage(const events::MessageEvent<T> &e);
|
||||||
|
|
||||||
inline bool isDuplicate(const QString &event_id);
|
inline bool isDuplicate(const QString &event_id);
|
||||||
|
|
||||||
// Return nullptr if the event couldn't be parsed.
|
// Return nullptr if the event couldn't be parsed.
|
||||||
|
@ -153,3 +159,32 @@ TimelineView::isDuplicate(const QString &event_id)
|
||||||
{
|
{
|
||||||
return eventIds_.contains(event_id);
|
return eventIds_.contains(event_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
bool
|
||||||
|
TimelineView::isPendingMessage(const events::MessageEvent<T> &e, const QString &local_userid)
|
||||||
|
{
|
||||||
|
if (e.sender() != local_userid)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (const auto &msg : pending_msgs_) {
|
||||||
|
if (msg.event_id == e.eventId() || msg.body == e.content().body())
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void
|
||||||
|
TimelineView::removePendingMessage(const events::MessageEvent<T> &e)
|
||||||
|
{
|
||||||
|
for (auto it = pending_msgs_.begin(); it != pending_msgs_.end(); it++) {
|
||||||
|
int index = std::distance(pending_msgs_.begin(), it);
|
||||||
|
|
||||||
|
if (it->event_id == e.eventId() || it->body == e.content().body()) {
|
||||||
|
pending_msgs_.removeAt(index);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
#include "MatrixClient.h"
|
#include "MatrixClient.h"
|
||||||
|
#include "MessageEvent.h"
|
||||||
#include "RoomInfoListItem.h"
|
#include "RoomInfoListItem.h"
|
||||||
#include "Sync.h"
|
#include "Sync.h"
|
||||||
#include "TimelineView.h"
|
#include "TimelineView.h"
|
||||||
|
@ -54,6 +55,7 @@ signals:
|
||||||
public slots:
|
public slots:
|
||||||
void setHistoryView(const QString &room_id);
|
void setHistoryView(const QString &room_id);
|
||||||
void sendTextMessage(const QString &msg);
|
void sendTextMessage(const QString &msg);
|
||||||
|
void sendEmoteMessage(const QString &msg);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void messageSent(const QString &eventid, const QString &roomid, int txnid);
|
void messageSent(const QString &eventid, const QString &roomid, int txnid);
|
||||||
|
|
|
@ -148,6 +148,11 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
|
||||||
view_manager_,
|
view_manager_,
|
||||||
SLOT(sendTextMessage(const QString &)));
|
SLOT(sendTextMessage(const QString &)));
|
||||||
|
|
||||||
|
connect(text_input_,
|
||||||
|
SIGNAL(sendEmoteMessage(const QString &)),
|
||||||
|
view_manager_,
|
||||||
|
SLOT(sendEmoteMessage(const QString &)));
|
||||||
|
|
||||||
connect(client_.data(),
|
connect(client_.data(),
|
||||||
SIGNAL(roomAvatarRetrieved(const QString &, const QPixmap &)),
|
SIGNAL(roomAvatarRetrieved(const QString &, const QPixmap &)),
|
||||||
this,
|
this,
|
||||||
|
|
|
@ -63,7 +63,8 @@ MatrixClient::onVersionsResponse(QNetworkReply *reply)
|
||||||
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
|
||||||
if (status_code == 404) {
|
if (status_code == 404) {
|
||||||
emit versionError("Versions endpoint was not found on the server. Possibly not a Matrix server");
|
emit versionError(
|
||||||
|
"Versions endpoint was not found on the server. Possibly not a Matrix server");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +166,8 @@ MatrixClient::onRegisterResponse(QNetworkReply *reply)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response.deserialize(json);
|
response.deserialize(json);
|
||||||
emit registerSuccess(response.getUserId(), response.getHomeServer(), response.getAccessToken());
|
emit registerSuccess(
|
||||||
|
response.getUserId(), response.getHomeServer(), response.getAccessToken());
|
||||||
} catch (DeserializationException &e) {
|
} catch (DeserializationException &e) {
|
||||||
qWarning() << "Register" << e.what();
|
qWarning() << "Register" << e.what();
|
||||||
emit registerError("Received malformed response.");
|
emit registerError("Received malformed response.");
|
||||||
|
@ -258,7 +260,7 @@ MatrixClient::onSyncResponse(QNetworkReply *reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
MatrixClient::onSendTextMessageResponse(QNetworkReply *reply)
|
MatrixClient::onSendRoomMessage(QNetworkReply *reply)
|
||||||
{
|
{
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
|
@ -445,8 +447,8 @@ MatrixClient::onResponse(QNetworkReply *reply)
|
||||||
case Endpoint::Sync:
|
case Endpoint::Sync:
|
||||||
onSyncResponse(reply);
|
onSyncResponse(reply);
|
||||||
break;
|
break;
|
||||||
case Endpoint::SendTextMessage:
|
case Endpoint::SendRoomMessage:
|
||||||
onSendTextMessageResponse(reply);
|
onSendRoomMessage(reply);
|
||||||
break;
|
break;
|
||||||
case Endpoint::RoomAvatar:
|
case Endpoint::RoomAvatar:
|
||||||
onRoomAvatarResponse(reply);
|
onRoomAvatarResponse(reply);
|
||||||
|
@ -522,7 +524,8 @@ MatrixClient::registerUser(const QString &user, const QString &pass, const QStri
|
||||||
void
|
void
|
||||||
MatrixClient::sync() noexcept
|
MatrixClient::sync() noexcept
|
||||||
{
|
{
|
||||||
QJsonObject filter{ { "room", QJsonObject{ { "ephemeral", QJsonObject{ { "limit", 0 } } } } },
|
QJsonObject filter{ { "room",
|
||||||
|
QJsonObject{ { "ephemeral", QJsonObject{ { "limit", 0 } } } } },
|
||||||
{ "presence", QJsonObject{ { "limit", 0 } } } };
|
{ "presence", QJsonObject{ { "limit", 0 } } } };
|
||||||
|
|
||||||
QUrlQuery query;
|
QUrlQuery query;
|
||||||
|
@ -532,7 +535,8 @@ MatrixClient::sync() noexcept
|
||||||
query.addQueryItem("access_token", token_);
|
query.addQueryItem("access_token", token_);
|
||||||
|
|
||||||
if (next_batch_.isEmpty()) {
|
if (next_batch_.isEmpty()) {
|
||||||
qDebug() << "Sync requires a valid next_batch token. Initial sync should be performed.";
|
qDebug()
|
||||||
|
<< "Sync requires a valid next_batch token. Initial sync should be performed.";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -549,23 +553,39 @@ MatrixClient::sync() noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
MatrixClient::sendTextMessage(const QString &roomid, const QString &msg) noexcept
|
MatrixClient::sendRoomMessage(matrix::events::MessageEventType ty,
|
||||||
|
const QString &roomid,
|
||||||
|
const QString &msg) noexcept
|
||||||
{
|
{
|
||||||
QUrlQuery query;
|
QUrlQuery query;
|
||||||
query.addQueryItem("access_token", token_);
|
query.addQueryItem("access_token", token_);
|
||||||
|
|
||||||
QUrl endpoint(server_);
|
QUrl endpoint(server_);
|
||||||
endpoint.setPath(api_url_ + QString("/rooms/%1/send/m.room.message/%2").arg(roomid).arg(txn_id_));
|
endpoint.setPath(api_url_ +
|
||||||
|
QString("/rooms/%1/send/m.room.message/%2").arg(roomid).arg(txn_id_));
|
||||||
endpoint.setQuery(query);
|
endpoint.setQuery(query);
|
||||||
|
|
||||||
QJsonObject body{ { "msgtype", "m.text" }, { "body", msg } };
|
QString msgType("");
|
||||||
|
|
||||||
|
switch (ty) {
|
||||||
|
case matrix::events::MessageEventType::Text:
|
||||||
|
msgType = "m.text";
|
||||||
|
break;
|
||||||
|
case matrix::events::MessageEventType::Emote:
|
||||||
|
msgType = "m.emote";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
msgType = "m.text";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject body{ { "msgtype", msgType }, { "body", msg } };
|
||||||
|
|
||||||
QNetworkRequest request(QString(endpoint.toEncoded()));
|
QNetworkRequest request(QString(endpoint.toEncoded()));
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||||
|
|
||||||
QNetworkReply *reply = put(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
|
QNetworkReply *reply = put(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
|
||||||
|
reply->setProperty("endpoint", static_cast<int>(Endpoint::SendRoomMessage));
|
||||||
reply->setProperty("endpoint", static_cast<int>(Endpoint::SendTextMessage));
|
|
||||||
reply->setProperty("txn_id", txn_id_);
|
reply->setProperty("txn_id", txn_id_);
|
||||||
reply->setProperty("roomid", roomid);
|
reply->setProperty("roomid", roomid);
|
||||||
|
|
||||||
|
@ -615,7 +635,8 @@ MatrixClient::versions() noexcept
|
||||||
void
|
void
|
||||||
MatrixClient::getOwnProfile() noexcept
|
MatrixClient::getOwnProfile() noexcept
|
||||||
{
|
{
|
||||||
// FIXME: Remove settings from the matrix client. The class should store the user's matrix ID.
|
// FIXME: Remove settings from the matrix client. The class should store the user's matrix
|
||||||
|
// ID.
|
||||||
QSettings settings;
|
QSettings settings;
|
||||||
auto userid = settings.value("auth/user_id", "").toString();
|
auto userid = settings.value("auth/user_id", "").toString();
|
||||||
|
|
||||||
|
@ -647,7 +668,8 @@ MatrixClient::fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url)
|
||||||
query.addQueryItem("height", "512");
|
query.addQueryItem("height", "512");
|
||||||
query.addQueryItem("method", "crop");
|
query.addQueryItem("method", "crop");
|
||||||
|
|
||||||
QString media_url = QString("%1/_matrix/media/r0/thumbnail/%2").arg(getHomeServer().toString(), url_parts[1]);
|
QString media_url =
|
||||||
|
QString("%1/_matrix/media/r0/thumbnail/%2").arg(getHomeServer().toString(), url_parts[1]);
|
||||||
|
|
||||||
QUrl endpoint(media_url);
|
QUrl endpoint(media_url);
|
||||||
endpoint.setQuery(query);
|
endpoint.setQuery(query);
|
||||||
|
@ -674,7 +696,8 @@ MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl)
|
||||||
query.addQueryItem("height", "128");
|
query.addQueryItem("height", "128");
|
||||||
query.addQueryItem("method", "crop");
|
query.addQueryItem("method", "crop");
|
||||||
|
|
||||||
QString media_url = QString("%1/_matrix/media/r0/thumbnail/%2").arg(getHomeServer().toString(), url_parts[1]);
|
QString media_url =
|
||||||
|
QString("%1/_matrix/media/r0/thumbnail/%2").arg(getHomeServer().toString(), url_parts[1]);
|
||||||
|
|
||||||
QUrl endpoint(media_url);
|
QUrl endpoint(media_url);
|
||||||
endpoint.setQuery(query);
|
endpoint.setQuery(query);
|
||||||
|
@ -711,7 +734,8 @@ MatrixClient::fetchOwnAvatar(const QUrl &avatar_url)
|
||||||
query.addQueryItem("height", "512");
|
query.addQueryItem("height", "512");
|
||||||
query.addQueryItem("method", "crop");
|
query.addQueryItem("method", "crop");
|
||||||
|
|
||||||
QString media_url = QString("%1/_matrix/media/r0/thumbnail/%2").arg(getHomeServer().toString(), url_parts[1]);
|
QString media_url =
|
||||||
|
QString("%1/_matrix/media/r0/thumbnail/%2").arg(getHomeServer().toString(), url_parts[1]);
|
||||||
|
|
||||||
QUrl endpoint(media_url);
|
QUrl endpoint(media_url);
|
||||||
endpoint.setQuery(query);
|
endpoint.setQuery(query);
|
||||||
|
|
|
@ -39,7 +39,7 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
|
||||||
}
|
}
|
||||||
|
|
||||||
TextInputWidget::TextInputWidget(QWidget *parent)
|
TextInputWidget::TextInputWidget(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QFrame(parent)
|
||||||
{
|
{
|
||||||
setFont(QFont("Emoji One"));
|
setFont(QFont("Emoji One"));
|
||||||
|
|
||||||
|
@ -72,7 +72,8 @@ TextInputWidget::TextInputWidget(QWidget *parent)
|
||||||
send_message_button_->setForegroundColor(QColor("#acc7dc"));
|
send_message_button_->setForegroundColor(QColor("#acc7dc"));
|
||||||
|
|
||||||
QIcon send_message_icon;
|
QIcon send_message_icon;
|
||||||
send_message_icon.addFile(":/icons/icons/share-dark.png", QSize(), QIcon::Normal, QIcon::Off);
|
send_message_icon.addFile(
|
||||||
|
":/icons/icons/share-dark.png", QSize(), QIcon::Normal, QIcon::Off);
|
||||||
send_message_button_->setIcon(send_message_icon);
|
send_message_button_->setIcon(send_message_icon);
|
||||||
send_message_button_->setIconSize(QSize(24, 24));
|
send_message_button_->setIconSize(QSize(24, 24));
|
||||||
|
|
||||||
|
@ -93,7 +94,10 @@ TextInputWidget::TextInputWidget(QWidget *parent)
|
||||||
|
|
||||||
connect(send_message_button_, SIGNAL(clicked()), this, SLOT(onSendButtonClicked()));
|
connect(send_message_button_, SIGNAL(clicked()), this, SLOT(onSendButtonClicked()));
|
||||||
connect(input_, SIGNAL(enterPressed()), send_message_button_, SIGNAL(clicked()));
|
connect(input_, SIGNAL(enterPressed()), send_message_button_, SIGNAL(clicked()));
|
||||||
connect(emoji_button_, SIGNAL(emojiSelected(const QString &)), this, SLOT(addSelectedEmoji(const QString &)));
|
connect(emoji_button_,
|
||||||
|
SIGNAL(emojiSelected(const QString &)),
|
||||||
|
this,
|
||||||
|
SLOT(addSelectedEmoji(const QString &)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -123,26 +127,32 @@ TextInputWidget::addSelectedEmoji(const QString &emoji)
|
||||||
void
|
void
|
||||||
TextInputWidget::onSendButtonClicked()
|
TextInputWidget::onSendButtonClicked()
|
||||||
{
|
{
|
||||||
auto msg_text = input_->document()->toPlainText().trimmed();
|
auto msgText = input_->document()->toPlainText().trimmed();
|
||||||
|
|
||||||
if (msg_text.isEmpty())
|
if (msgText.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
emit sendTextMessage(msg_text);
|
if (msgText.startsWith(EMOTE_COMMAND)) {
|
||||||
|
auto text = parseEmoteCommand(msgText);
|
||||||
|
|
||||||
|
if (!text.isEmpty())
|
||||||
|
emit sendEmoteMessage(text);
|
||||||
|
} else {
|
||||||
|
emit sendTextMessage(msgText);
|
||||||
|
}
|
||||||
|
|
||||||
input_->clear();
|
input_->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
QString
|
||||||
TextInputWidget::paintEvent(QPaintEvent *event)
|
TextInputWidget::parseEmoteCommand(const QString &cmd)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event);
|
auto text = cmd.right(cmd.size() - EMOTE_COMMAND.size()).trimmed();
|
||||||
|
|
||||||
QStyleOption option;
|
if (!text.isEmpty())
|
||||||
option.initFrom(this);
|
return text;
|
||||||
|
|
||||||
QPainter painter(this);
|
return QString("");
|
||||||
style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TextInputWidget::~TextInputWidget()
|
TextInputWidget::~TextInputWidget()
|
||||||
|
|
|
@ -67,46 +67,42 @@ TimelineItem::init()
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* For messages created locally. The avatar and the username are displayed.
|
* For messages created locally.
|
||||||
*/
|
*/
|
||||||
TimelineItem::TimelineItem(const QString &userid, QString body, QWidget *parent)
|
TimelineItem::TimelineItem(events::MessageEventType ty,
|
||||||
|
const QString &userid,
|
||||||
|
QString body,
|
||||||
|
bool withSender,
|
||||||
|
QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
descriptionMsg_ = { "You: ", userid, body, descriptiveTime(QDateTime::currentDateTime()) };
|
|
||||||
|
auto displayName = TimelineViewManager::displayName(userid);
|
||||||
|
auto timestamp = QDateTime::currentDateTime();
|
||||||
|
|
||||||
|
if (ty == events::MessageEventType::Emote) {
|
||||||
|
body = QString("* %1 %2").arg(displayName).arg(body);
|
||||||
|
descriptionMsg_ = { "", userid, body, descriptiveTime(timestamp) };
|
||||||
|
} else {
|
||||||
|
descriptionMsg_ = {
|
||||||
|
"You: ", userid, body, descriptiveTime(QDateTime::currentDateTime())
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
body.replace(URL_REGEX, URL_HTML);
|
body.replace(URL_REGEX, URL_HTML);
|
||||||
auto displayName = TimelineViewManager::displayName(userid);
|
generateTimestamp(timestamp);
|
||||||
|
|
||||||
generateTimestamp(QDateTime::currentDateTime());
|
if (withSender) {
|
||||||
generateBody(displayName, body);
|
generateBody(displayName, body);
|
||||||
|
|
||||||
setupAvatarLayout(displayName);
|
setupAvatarLayout(displayName);
|
||||||
|
|
||||||
mainLayout_->addLayout(headerLayout_);
|
mainLayout_->addLayout(headerLayout_);
|
||||||
mainLayout_->addWidget(body_);
|
|
||||||
|
|
||||||
AvatarProvider::resolve(userid, this);
|
AvatarProvider::resolve(userid, this);
|
||||||
}
|
} else {
|
||||||
|
|
||||||
/*
|
|
||||||
* For messages created locally. Only the text is displayed.
|
|
||||||
*/
|
|
||||||
TimelineItem::TimelineItem(QString body, QWidget *parent)
|
|
||||||
: QWidget(parent)
|
|
||||||
{
|
|
||||||
QSettings settings;
|
|
||||||
auto userid = settings.value("auth/user_id").toString();
|
|
||||||
|
|
||||||
init();
|
|
||||||
descriptionMsg_ = { "You: ", userid, body, descriptiveTime(QDateTime::currentDateTime()) };
|
|
||||||
|
|
||||||
body.replace(URL_REGEX, URL_HTML);
|
|
||||||
|
|
||||||
generateTimestamp(QDateTime::currentDateTime());
|
|
||||||
generateBody(body);
|
generateBody(body);
|
||||||
|
|
||||||
setupSimpleLayout();
|
setupSimpleLayout();
|
||||||
|
}
|
||||||
|
|
||||||
mainLayout_->addWidget(body_);
|
mainLayout_->addWidget(body_);
|
||||||
}
|
}
|
||||||
|
|
|
@ -289,7 +289,10 @@ TimelineView::parseMessageEvent(const QJsonObject &event, TimelineDirection dire
|
||||||
|
|
||||||
eventIds_[emote.eventId()] = true;
|
eventIds_[emote.eventId()] = true;
|
||||||
|
|
||||||
// TODO Check if it's a message waiting for validation
|
if (isPendingMessage(emote, local_user_)) {
|
||||||
|
removePendingMessage(emote);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
auto with_sender = isSenderRendered(emote.sender(), direction);
|
auto with_sender = isSenderRendered(emote.sender(), direction);
|
||||||
|
|
||||||
|
@ -452,55 +455,19 @@ TimelineView::updatePendingMessage(int txn_id, QString event_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
|
||||||
TimelineView::isPendingMessage(const events::MessageEvent<msgs::Text> &e,
|
|
||||||
const QString &local_userid)
|
|
||||||
{
|
|
||||||
if (e.sender() != local_userid)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
for (const auto &msg : pending_msgs_) {
|
|
||||||
if (msg.event_id == e.eventId() || msg.body == e.content().body())
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
TimelineView::removePendingMessage(const events::MessageEvent<msgs::Text> &e)
|
TimelineView::addUserMessage(matrix::events::MessageEventType ty, const QString &body, int txn_id)
|
||||||
{
|
|
||||||
for (auto it = pending_msgs_.begin(); it != pending_msgs_.end(); it++) {
|
|
||||||
int index = std::distance(pending_msgs_.begin(), it);
|
|
||||||
|
|
||||||
if (it->event_id == e.eventId() || it->body == e.content().body()) {
|
|
||||||
pending_msgs_.removeAt(index);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
TimelineView::addUserTextMessage(const QString &body, int txn_id)
|
|
||||||
{
|
{
|
||||||
QSettings settings;
|
QSettings settings;
|
||||||
auto user_id = settings.value("auth/user_id").toString();
|
auto user_id = settings.value("auth/user_id").toString();
|
||||||
|
|
||||||
auto with_sender = lastSender_ != user_id;
|
auto with_sender = lastSender_ != user_id;
|
||||||
|
|
||||||
TimelineItem *view_item;
|
TimelineItem *view_item = new TimelineItem(ty, user_id, body, with_sender, scroll_widget_);
|
||||||
|
|
||||||
if (with_sender)
|
|
||||||
view_item = new TimelineItem(user_id, body, scroll_widget_);
|
|
||||||
else
|
|
||||||
view_item = new TimelineItem(body, scroll_widget_);
|
|
||||||
|
|
||||||
scroll_layout_->addWidget(view_item);
|
scroll_layout_->addWidget(view_item);
|
||||||
|
|
||||||
lastSender_ = user_id;
|
lastSender_ = user_id;
|
||||||
|
|
||||||
PendingMessage message(txn_id, body, "", view_item);
|
PendingMessage message(txn_id, body, "", view_item);
|
||||||
|
|
||||||
pending_msgs_.push_back(message);
|
pending_msgs_.push_back(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,10 +32,8 @@ TimelineViewManager::TimelineViewManager(QSharedPointer<MatrixClient> client, QW
|
||||||
{
|
{
|
||||||
setStyleSheet("QWidget { background: #f8fbfe; color: #e8e8e8; border: none;}");
|
setStyleSheet("QWidget { background: #f8fbfe; color: #e8e8e8; border: none;}");
|
||||||
|
|
||||||
connect(client_.data(),
|
connect(
|
||||||
SIGNAL(messageSent(const QString &, const QString &, int)),
|
client_.data(), &MatrixClient::messageSent, this, &TimelineViewManager::messageSent);
|
||||||
this,
|
|
||||||
SLOT(messageSent(const QString &, const QString &, int)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TimelineViewManager::~TimelineViewManager()
|
TimelineViewManager::~TimelineViewManager()
|
||||||
|
@ -59,8 +57,19 @@ TimelineViewManager::sendTextMessage(const QString &msg)
|
||||||
auto room_id = active_room_;
|
auto room_id = active_room_;
|
||||||
auto view = views_[room_id];
|
auto view = views_[room_id];
|
||||||
|
|
||||||
view->addUserTextMessage(msg, client_->transactionId());
|
view->addUserMessage(matrix::events::MessageEventType::Text, msg, client_->transactionId());
|
||||||
client_->sendTextMessage(room_id, msg);
|
client_->sendRoomMessage(matrix::events::MessageEventType::Text, room_id, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TimelineViewManager::sendEmoteMessage(const QString &msg)
|
||||||
|
{
|
||||||
|
auto room_id = active_room_;
|
||||||
|
auto view = views_[room_id];
|
||||||
|
|
||||||
|
view->addUserMessage(
|
||||||
|
matrix::events::MessageEventType::Emote, msg, client_->transactionId());
|
||||||
|
client_->sendRoomMessage(matrix::events::MessageEventType::Emote, room_id, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Reference in a new issue