Fix autocompletion

Now it will save a map from display name to id and use that to generate
clean matrix.to links. This also make sure the colors used for the
preview are correct by using NeoChatUser and fix the bug with the regex
by simply removing the regex.

Fix #234
This commit is contained in:
Carl Schwan
2021-01-11 02:18:00 +01:00
parent ed26e87c96
commit a3e1e1d0a4
4 changed files with 31 additions and 6 deletions

View File

@@ -138,6 +138,7 @@ ToolBar {
keyNavigationWraps: true keyNavigationWraps: true
delegate: Control { delegate: Control {
readonly property string userId: modelData.id ?? ""
readonly property string displayText: modelData.displayName ?? modelData.unicode readonly property string displayText: modelData.displayName ?? modelData.unicode
readonly property bool isEmoji: modelData.unicode != null readonly property bool isEmoji: modelData.unicode != null
readonly property bool highlighted: autoCompleteListView.currentIndex == index readonly property bool highlighted: autoCompleteListView.currentIndex == index
@@ -468,9 +469,14 @@ ToolBar {
autoCompleteEndPosition = cursorPosition autoCompleteEndPosition = cursorPosition
} }
// store each user we autoComplete here, this will be helpful later to generate
// the matrix.to links.
// This use an hack to define: https://doc.qt.io/qt-5/qml-var.html#property-value-initialization-semantics
property var userAutocompleted: ({})
function postMessage() { function postMessage() {
roomManager.actionsHandler.postMessage(inputField.text.trim(), attachmentPath, roomManager.actionsHandler.postMessage(inputField.text.trim(), attachmentPath,
replyEventID, editEventId); replyEventID, editEventId, inputField.userAutocompleted);
clearAttachment(); clearAttachment();
currentRoom.markAllMessagesAsRead(); currentRoom.markAllMessagesAsRead();
clear(); clear();
@@ -481,6 +487,10 @@ ToolBar {
function autoComplete() { function autoComplete() {
documentHandler.replaceAutoComplete(autoCompleteListView.currentItem.displayText) documentHandler.replaceAutoComplete(autoCompleteListView.currentItem.displayText)
// Unfortunally it doesn't
if (!autoCompleteListView.currentItem.isEmoji) {
inputField.userAutocompleted[autoCompleteListView.currentItem.displayText] = autoCompleteListView.currentItem.userId;
}
} }
} }
} }
@@ -573,6 +583,7 @@ ToolBar {
function clear() { function clear() {
inputField.clear() inputField.clear()
inputField.userAutocompleted = {};
} }
function clearEditReply() { function clearEditReply() {

View File

@@ -159,12 +159,17 @@ void ActionsHandler::createRoom(const QString &name, const QString &topic)
} }
void ActionsHandler::postMessage(const QString &text, void ActionsHandler::postMessage(const QString &text,
const QString &attachementPath, const QString &replyEventId, const QString &editEventId) const QString &attachementPath, const QString &replyEventId, const QString &editEventId,
const QVariantMap usernames)
{ {
QString rawText = text; QString rawText = text;
QString cleanedText = text; QString cleanedText = text;
cleanedText = cleanedText.replace(QRegularExpression("@([^: ]*):([^ ]*\\.[^ ]*)"),
"[@$1:$2](https://matrix.to/#/@$1:$2)"); for (const auto username : usernames.keys()) {
const auto replacement = usernames.value(username);
cleanedText = cleanedText.replace(username,
"[" + username + "](https://matrix.to/#/" + replacement.toString() + ")");
}
if (attachementPath.length() > 0) { if (attachementPath.length() > 0) {
m_room->uploadFile(attachementPath, cleanedText); m_room->uploadFile(attachementPath, cleanedText);

View File

@@ -70,7 +70,8 @@ public Q_SLOTS:
/// ///
/// This also interprets commands if any. /// This also interprets commands if any.
void postMessage(const QString &text, const QString &attachementPath, void postMessage(const QString &text, const QString &attachementPath,
const QString &replyEventId, const QString &editEventId); const QString &replyEventId, const QString &editEventId,
const QVariantMap usernames);
private: private:
Connection *m_connection = nullptr; Connection *m_connection = nullptr;

View File

@@ -266,7 +266,15 @@ QVariantList NeoChatRoom::getUsers(const QString &keyword) const
QVariantList matchedList; QVariantList matchedList;
for (const auto u : userList) { for (const auto u : userList) {
if (u->displayname(this).contains(keyword, Qt::CaseInsensitive)) { if (u->displayname(this).contains(keyword, Qt::CaseInsensitive)) {
matchedList.append(QVariant::fromValue(u)); NeoChatUser user(u->id(), u->connection());
QVariantMap userVariant {
{ QStringLiteral("id"), user.id() },
{ QStringLiteral("displayName"), user.displayname(this) },
{ QStringLiteral("avatarMediaId"), user.avatarMediaId(this) },
{ QStringLiteral("color"), user.color() }
};
matchedList.append(QVariant::fromValue(userVariant));
} }
} }