2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-11-01 01:24:07 +03:00
|
|
|
#include "InputBar.h"
|
|
|
|
|
|
|
|
#include <QClipboard>
|
2020-11-25 19:02:23 +03:00
|
|
|
#include <QDropEvent>
|
2020-11-15 06:52:49 +03:00
|
|
|
#include <QFileDialog>
|
2020-11-01 01:24:07 +03:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QMimeData>
|
2020-11-15 06:52:49 +03:00
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QStandardPaths>
|
2021-09-02 04:15:07 +03:00
|
|
|
#include <QTextBoundaryFinder>
|
2020-11-15 06:52:49 +03:00
|
|
|
#include <QUrl>
|
2020-11-01 01:24:07 +03:00
|
|
|
|
2021-03-28 18:37:36 +03:00
|
|
|
#include <QRegularExpression>
|
2020-11-09 05:12:37 +03:00
|
|
|
#include <mtx/responses/common.hpp>
|
2020-11-15 06:52:49 +03:00
|
|
|
#include <mtx/responses/media.hpp>
|
2020-11-09 05:12:37 +03:00
|
|
|
|
|
|
|
#include "Cache.h"
|
|
|
|
#include "ChatPage.h"
|
2021-07-21 02:03:38 +03:00
|
|
|
#include "CombinedImagePackModel.h"
|
2021-05-29 00:25:57 +03:00
|
|
|
#include "Config.h"
|
2020-11-01 01:24:07 +03:00
|
|
|
#include "Logging.h"
|
2020-11-16 01:14:47 +03:00
|
|
|
#include "MainWindow.h"
|
2020-11-09 05:12:37 +03:00
|
|
|
#include "MatrixClient.h"
|
|
|
|
#include "TimelineModel.h"
|
2020-12-13 18:23:04 +03:00
|
|
|
#include "TimelineViewManager.h"
|
2020-11-09 05:12:37 +03:00
|
|
|
#include "UserSettingsPage.h"
|
|
|
|
#include "Utils.h"
|
2020-11-15 06:52:49 +03:00
|
|
|
#include "dialogs/PreviewUploadOverlay.h"
|
|
|
|
|
|
|
|
#include "blurhash.hpp"
|
2020-11-09 05:12:37 +03:00
|
|
|
|
|
|
|
static constexpr size_t INPUT_HISTORY_SIZE = 10;
|
2020-11-01 01:24:07 +03:00
|
|
|
|
2020-11-09 05:12:37 +03:00
|
|
|
void
|
2020-11-01 01:24:07 +03:00
|
|
|
InputBar::paste(bool fromMouse)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
const QMimeData *md = nullptr;
|
2020-11-01 01:24:07 +03:00
|
|
|
|
2021-09-28 00:19:43 +03:00
|
|
|
if (fromMouse && QGuiApplication::clipboard()->supportsSelection()) {
|
|
|
|
md = QGuiApplication::clipboard()->mimeData(QClipboard::Selection);
|
2021-09-18 01:22:33 +03:00
|
|
|
} else {
|
|
|
|
md = QGuiApplication::clipboard()->mimeData(QClipboard::Clipboard);
|
|
|
|
}
|
2020-11-01 01:24:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (md)
|
|
|
|
insertMimeData(md);
|
2020-11-25 19:02:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InputBar::insertMimeData(const QMimeData *md)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!md)
|
|
|
|
return;
|
|
|
|
|
2021-12-29 08:01:38 +03:00
|
|
|
nhlog::ui()->debug("Got mime formats: {}",
|
|
|
|
md->formats().join(QStringLiteral(", ")).toStdString());
|
2021-12-29 06:28:08 +03:00
|
|
|
const auto formats = md->formats().filter(QStringLiteral("/"));
|
|
|
|
const auto image = formats.filter(QStringLiteral("image/"), Qt::CaseInsensitive);
|
|
|
|
const auto audio = formats.filter(QStringLiteral("audio/"), Qt::CaseInsensitive);
|
|
|
|
const auto video = formats.filter(QStringLiteral("video/"), Qt::CaseInsensitive);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2021-09-28 00:19:43 +03:00
|
|
|
if (md->hasImage()) {
|
2021-12-29 06:28:08 +03:00
|
|
|
if (formats.contains(QStringLiteral("image/svg+xml"), Qt::CaseInsensitive)) {
|
|
|
|
showPreview(*md, QLatin1String(""), QStringList(QStringLiteral("image/svg+xml")));
|
2021-09-28 02:42:35 +03:00
|
|
|
} else {
|
2021-12-29 06:28:08 +03:00
|
|
|
showPreview(*md, QLatin1String(""), image);
|
2021-09-28 02:42:35 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} else if (!audio.empty()) {
|
2021-12-29 06:28:08 +03:00
|
|
|
showPreview(*md, QLatin1String(""), audio);
|
2021-09-18 01:22:33 +03:00
|
|
|
} else if (!video.empty()) {
|
2021-12-29 06:28:08 +03:00
|
|
|
showPreview(*md, QLatin1String(""), video);
|
2021-09-18 01:22:33 +03:00
|
|
|
} else if (md->hasUrls()) {
|
|
|
|
// Generic file path for any platform.
|
|
|
|
QString path;
|
|
|
|
for (auto &&u : md->urls()) {
|
|
|
|
if (u.isLocalFile()) {
|
|
|
|
path = u.toLocalFile();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-12-03 03:01:24 +03:00
|
|
|
if (!path.isEmpty() && QFileInfo::exists(path)) {
|
2021-09-18 01:22:33 +03:00
|
|
|
showPreview(*md, path, formats);
|
|
|
|
} else {
|
|
|
|
nhlog::ui()->warn("Clipboard does not contain any valid file paths.");
|
|
|
|
}
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (md->hasFormat(QStringLiteral("x-special/gnome-copied-files"))) {
|
2021-09-18 01:22:33 +03:00
|
|
|
// Special case for X11 users. See "Notes for X11 Users" in md.
|
|
|
|
// Source: http://doc.qt.io/qt-5/qclipboard.html
|
|
|
|
|
|
|
|
// This MIME type returns a string with multiple lines separated by '\n'. The first
|
|
|
|
// line is the command to perform with the clipboard (not useful to us). The
|
|
|
|
// following lines are the file URIs.
|
|
|
|
//
|
|
|
|
// Source: the nautilus source code in file 'src/nautilus-clipboard.c' in function
|
|
|
|
// nautilus_clipboard_get_uri_list_from_selection_data()
|
|
|
|
// https://github.com/GNOME/nautilus/blob/master/src/nautilus-clipboard.c
|
|
|
|
|
2021-12-29 06:28:08 +03:00
|
|
|
auto data = md->data(QStringLiteral("x-special/gnome-copied-files")).split('\n');
|
2021-09-18 01:22:33 +03:00
|
|
|
if (data.size() < 2) {
|
|
|
|
nhlog::ui()->warn("MIME format is malformed, cannot perform paste.");
|
|
|
|
return;
|
|
|
|
}
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QString path;
|
|
|
|
for (int i = 1; i < data.size(); ++i) {
|
|
|
|
QUrl url{data[i]};
|
|
|
|
if (url.isLocalFile()) {
|
|
|
|
path = url.toLocalFile();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!path.isEmpty()) {
|
|
|
|
showPreview(*md, path, formats);
|
2020-11-01 01:24:07 +03:00
|
|
|
} else {
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::ui()->warn("Clipboard does not contain any valid file paths: {}",
|
|
|
|
data.join(", ").toStdString());
|
2020-11-01 01:24:07 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} else if (md->hasText()) {
|
|
|
|
emit insertText(md->text());
|
|
|
|
} else {
|
2021-12-29 06:28:08 +03:00
|
|
|
nhlog::ui()->debug("formats: {}", md->formats().join(QStringLiteral(", ")).toStdString());
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-11-01 01:24:07 +03:00
|
|
|
}
|
|
|
|
|
2021-09-02 04:15:07 +03:00
|
|
|
void
|
|
|
|
InputBar::updateAtRoom(const QString &t)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
bool roomMention = false;
|
|
|
|
|
|
|
|
if (t.size() > 4) {
|
|
|
|
QTextBoundaryFinder finder(QTextBoundaryFinder::BoundaryType::Word, t);
|
|
|
|
|
|
|
|
finder.toStart();
|
|
|
|
do {
|
|
|
|
auto start = finder.position();
|
|
|
|
finder.toNextBoundary();
|
|
|
|
auto end = finder.position();
|
2021-12-29 08:01:38 +03:00
|
|
|
if (start > 0 && end - start >= 4 &&
|
|
|
|
t.mid(start, end - start) == QLatin1String("room") &&
|
2021-09-18 01:22:33 +03:00
|
|
|
t.at(start - 1) == QChar('@')) {
|
|
|
|
roomMention = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (finder.position() < t.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (roomMention != this->containsAtRoom_) {
|
|
|
|
this->containsAtRoom_ = roomMention;
|
|
|
|
emit containsAtRoomChanged();
|
|
|
|
}
|
2021-09-02 04:15:07 +03:00
|
|
|
}
|
|
|
|
|
2021-02-25 02:50:17 +03:00
|
|
|
void
|
2021-12-03 03:54:43 +03:00
|
|
|
InputBar::setText(const QString &newText)
|
2021-02-25 02:50:17 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (history_.empty())
|
|
|
|
history_.push_front(newText);
|
|
|
|
else
|
|
|
|
history_.front() = newText;
|
|
|
|
history_index_ = 0;
|
2021-02-25 02:50:17 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (history_.size() == INPUT_HISTORY_SIZE)
|
|
|
|
history_.pop_back();
|
2021-02-25 02:50:17 +03:00
|
|
|
|
2021-12-29 06:28:08 +03:00
|
|
|
updateAtRoom(QLatin1String(""));
|
2021-09-18 01:22:33 +03:00
|
|
|
emit textChanged(newText);
|
2021-02-25 02:50:17 +03:00
|
|
|
}
|
2020-11-01 01:24:07 +03:00
|
|
|
void
|
2021-12-03 03:54:43 +03:00
|
|
|
InputBar::updateState(int selectionStart_,
|
|
|
|
int selectionEnd_,
|
|
|
|
int cursorPosition_,
|
|
|
|
const QString &text_)
|
2020-11-01 01:24:07 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (text_.isEmpty())
|
|
|
|
stopTyping();
|
|
|
|
else
|
|
|
|
startTyping();
|
2020-11-17 04:37:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (text_ != text()) {
|
|
|
|
if (history_.empty())
|
|
|
|
history_.push_front(text_);
|
|
|
|
else
|
|
|
|
history_.front() = text_;
|
|
|
|
history_index_ = 0;
|
2021-09-02 04:15:07 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
updateAtRoom(text_);
|
|
|
|
}
|
2020-11-17 15:25:16 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
selectionStart = selectionStart_;
|
|
|
|
selectionEnd = selectionEnd_;
|
|
|
|
cursorPosition = cursorPosition_;
|
2020-11-17 15:25:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
InputBar::text() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (history_index_ < history_.size())
|
|
|
|
return history_.at(history_index_);
|
2020-11-17 15:25:16 +03:00
|
|
|
|
2021-12-29 06:28:08 +03:00
|
|
|
return QString();
|
2020-11-17 15:25:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
InputBar::previousText()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
history_index_++;
|
|
|
|
if (history_index_ >= INPUT_HISTORY_SIZE)
|
|
|
|
history_index_ = INPUT_HISTORY_SIZE;
|
|
|
|
else if (text().isEmpty())
|
|
|
|
history_index_--;
|
|
|
|
|
|
|
|
updateAtRoom(text());
|
|
|
|
return text();
|
2020-11-17 15:25:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
InputBar::nextText()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
history_index_--;
|
|
|
|
if (history_index_ >= INPUT_HISTORY_SIZE)
|
|
|
|
history_index_ = 0;
|
2020-11-17 15:25:16 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
updateAtRoom(text());
|
|
|
|
return text();
|
2020-11-01 01:24:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InputBar::send()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (text().trimmed().isEmpty())
|
|
|
|
return;
|
2020-11-09 05:12:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::ui()->debug("Send: {}", text().toStdString());
|
|
|
|
|
|
|
|
auto wasEdit = !room->edit().isEmpty();
|
|
|
|
|
|
|
|
if (text().startsWith('/')) {
|
2021-12-29 06:28:08 +03:00
|
|
|
int command_end = text().indexOf(QRegularExpression(QStringLiteral("\\s")));
|
2021-09-18 01:22:33 +03:00
|
|
|
if (command_end == -1)
|
|
|
|
command_end = text().size();
|
|
|
|
auto name = text().mid(1, command_end - 1);
|
|
|
|
auto args = text().mid(command_end + 1);
|
2021-12-29 06:28:08 +03:00
|
|
|
if (name.isEmpty() || name == QLatin1String("/")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
message(args);
|
|
|
|
} else {
|
|
|
|
command(name, args);
|
2021-02-25 02:50:17 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
} else {
|
|
|
|
message(text());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wasEdit) {
|
2021-12-29 06:28:08 +03:00
|
|
|
history_.push_front(QLatin1String(""));
|
|
|
|
setText(QLatin1String(""));
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-11-01 01:24:07 +03:00
|
|
|
}
|
2020-11-09 05:12:37 +03:00
|
|
|
|
2020-11-15 06:52:49 +03:00
|
|
|
void
|
|
|
|
InputBar::openFileSelection()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
const QString homeFolder = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
|
2022-01-12 21:09:46 +03:00
|
|
|
const auto fileName =
|
|
|
|
QFileDialog::getOpenFileName(nullptr, tr("Select a file"), homeFolder, tr("All Files (*)"));
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (fileName.isEmpty())
|
|
|
|
return;
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QMimeDatabase db;
|
|
|
|
QMimeType mime = db.mimeTypeForFile(fileName, QMimeDatabase::MatchContent);
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QFile file{fileName};
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
emit ChatPage::instance()->showNotification(
|
2021-12-29 06:28:08 +03:00
|
|
|
QStringLiteral("Error while reading media: %1").arg(file.errorString()));
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
setUploading(true);
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto bin = file.readAll();
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QMimeData data;
|
|
|
|
data.setData(mime.name(), bin);
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
showPreview(data, fileName, QStringList{mime.name()});
|
2020-11-15 06:52:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-09 05:12:37 +03:00
|
|
|
void
|
2021-12-03 03:54:43 +03:00
|
|
|
InputBar::message(const QString &msg, MarkdownOverride useMarkdown, bool rainbowify)
|
2020-11-09 05:12:37 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::msg::Text text = {};
|
|
|
|
text.body = msg.trimmed().toStdString();
|
|
|
|
|
|
|
|
if ((ChatPage::instance()->userSettings()->markdown() &&
|
|
|
|
useMarkdown == MarkdownOverride::NOT_SPECIFIED) ||
|
|
|
|
useMarkdown == MarkdownOverride::ON) {
|
|
|
|
text.formatted_body = utils::markdownToHtml(msg, rainbowify).toStdString();
|
|
|
|
// Remove markdown links by completer
|
|
|
|
text.body = msg.trimmed().replace(conf::strings::matrixToMarkdownLink, "\\1").toStdString();
|
|
|
|
|
|
|
|
// Don't send formatted_body, when we don't need to
|
2021-12-03 03:14:02 +03:00
|
|
|
if (text.formatted_body.find('<') == std::string::npos)
|
2021-09-18 01:22:33 +03:00
|
|
|
text.formatted_body = "";
|
|
|
|
else
|
|
|
|
text.format = "org.matrix.custom.html";
|
|
|
|
}
|
2020-11-09 05:12:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!room->edit().isEmpty()) {
|
|
|
|
if (!room->reply().isEmpty()) {
|
|
|
|
text.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::InReplyTo, room->reply().toStdString()});
|
2020-11-09 05:12:37 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
text.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::Replace, room->edit().toStdString()});
|
|
|
|
|
|
|
|
} else if (!room->reply().isEmpty()) {
|
|
|
|
auto related = room->relatedInfo(room->reply());
|
|
|
|
|
|
|
|
QString body;
|
|
|
|
bool firstLine = true;
|
2021-12-29 00:30:12 +03:00
|
|
|
auto lines = related.quoted_body.splitRef(u'\n');
|
|
|
|
for (auto line : qAsConst(lines)) {
|
2021-09-18 01:22:33 +03:00
|
|
|
if (firstLine) {
|
|
|
|
firstLine = false;
|
2021-12-29 06:28:08 +03:00
|
|
|
body = QStringLiteral("> <%1> %2\n").arg(related.quoted_user, line);
|
2021-09-18 01:22:33 +03:00
|
|
|
} else {
|
2021-12-29 06:28:08 +03:00
|
|
|
body += QStringLiteral("> %1\n").arg(line);
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-09 05:12:37 +03:00
|
|
|
|
2021-12-29 06:28:08 +03:00
|
|
|
text.body = QStringLiteral("%1\n%2").arg(body, msg).toStdString();
|
2020-11-09 05:12:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// NOTE(Nico): rich replies always need a formatted_body!
|
|
|
|
text.format = "org.matrix.custom.html";
|
|
|
|
if ((ChatPage::instance()->userSettings()->markdown() &&
|
|
|
|
useMarkdown == MarkdownOverride::NOT_SPECIFIED) ||
|
|
|
|
useMarkdown == MarkdownOverride::ON)
|
|
|
|
text.formatted_body =
|
|
|
|
utils::getFormattedQuoteBody(related, utils::markdownToHtml(msg, rainbowify))
|
|
|
|
.toStdString();
|
|
|
|
else
|
|
|
|
text.formatted_body =
|
|
|
|
utils::getFormattedQuoteBody(related, msg.toHtmlEscaped()).toStdString();
|
2020-11-09 05:12:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
text.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::InReplyTo, related.related_event});
|
|
|
|
}
|
2020-11-09 05:12:37 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
room->sendMessageEvent(text, mtx::events::EventType::RoomMessage);
|
2020-11-09 05:12:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-12-03 03:54:43 +03:00
|
|
|
InputBar::emote(const QString &msg, bool rainbowify)
|
2020-11-09 05:12:37 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto html = utils::markdownToHtml(msg, rainbowify);
|
|
|
|
|
|
|
|
mtx::events::msg::Emote emote;
|
|
|
|
emote.body = msg.trimmed().toStdString();
|
|
|
|
|
|
|
|
if (html != msg.trimmed().toHtmlEscaped() && ChatPage::instance()->userSettings()->markdown()) {
|
|
|
|
emote.formatted_body = html.toStdString();
|
|
|
|
emote.format = "org.matrix.custom.html";
|
|
|
|
// Remove markdown links by completer
|
|
|
|
emote.body =
|
|
|
|
msg.trimmed().replace(conf::strings::matrixToMarkdownLink, "\\1").toStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!room->reply().isEmpty()) {
|
|
|
|
emote.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::InReplyTo, room->reply().toStdString()});
|
|
|
|
}
|
|
|
|
if (!room->edit().isEmpty()) {
|
|
|
|
emote.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::Replace, room->edit().toStdString()});
|
|
|
|
}
|
|
|
|
|
|
|
|
room->sendMessageEvent(emote, mtx::events::EventType::RoomMessage);
|
2020-11-09 05:12:37 +03:00
|
|
|
}
|
|
|
|
|
2021-04-11 22:47:20 +03:00
|
|
|
void
|
2021-12-03 03:54:43 +03:00
|
|
|
InputBar::notice(const QString &msg, bool rainbowify)
|
2021-04-11 22:47:20 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto html = utils::markdownToHtml(msg, rainbowify);
|
|
|
|
|
|
|
|
mtx::events::msg::Notice notice;
|
|
|
|
notice.body = msg.trimmed().toStdString();
|
|
|
|
|
|
|
|
if (html != msg.trimmed().toHtmlEscaped() && ChatPage::instance()->userSettings()->markdown()) {
|
|
|
|
notice.formatted_body = html.toStdString();
|
|
|
|
notice.format = "org.matrix.custom.html";
|
|
|
|
// Remove markdown links by completer
|
|
|
|
notice.body =
|
|
|
|
msg.trimmed().replace(conf::strings::matrixToMarkdownLink, "\\1").toStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!room->reply().isEmpty()) {
|
|
|
|
notice.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::InReplyTo, room->reply().toStdString()});
|
|
|
|
}
|
|
|
|
if (!room->edit().isEmpty()) {
|
|
|
|
notice.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::Replace, room->edit().toStdString()});
|
|
|
|
}
|
|
|
|
|
|
|
|
room->sendMessageEvent(notice, mtx::events::EventType::RoomMessage);
|
2021-04-11 22:47:20 +03:00
|
|
|
}
|
|
|
|
|
2020-11-15 06:52:49 +03:00
|
|
|
void
|
|
|
|
InputBar::image(const QString &filename,
|
|
|
|
const std::optional<mtx::crypto::EncryptedFile> &file,
|
|
|
|
const QString &url,
|
|
|
|
const QString &mime,
|
|
|
|
uint64_t dsize,
|
|
|
|
const QSize &dimensions,
|
|
|
|
const QString &blurhash)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::msg::Image image;
|
|
|
|
image.info.mimetype = mime.toStdString();
|
|
|
|
image.info.size = dsize;
|
|
|
|
image.info.blurhash = blurhash.toStdString();
|
|
|
|
image.body = filename.toStdString();
|
|
|
|
image.info.h = dimensions.height();
|
|
|
|
image.info.w = dimensions.width();
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
image.file = file;
|
|
|
|
else
|
|
|
|
image.url = url.toStdString();
|
|
|
|
|
|
|
|
if (!room->reply().isEmpty()) {
|
|
|
|
image.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::InReplyTo, room->reply().toStdString()});
|
|
|
|
}
|
|
|
|
if (!room->edit().isEmpty()) {
|
|
|
|
image.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::Replace, room->edit().toStdString()});
|
|
|
|
}
|
|
|
|
|
|
|
|
room->sendMessageEvent(image, mtx::events::EventType::RoomMessage);
|
2020-11-15 06:52:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InputBar::file(const QString &filename,
|
|
|
|
const std::optional<mtx::crypto::EncryptedFile> &encryptedFile,
|
|
|
|
const QString &url,
|
|
|
|
const QString &mime,
|
|
|
|
uint64_t dsize)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::msg::File file;
|
|
|
|
file.info.mimetype = mime.toStdString();
|
|
|
|
file.info.size = dsize;
|
|
|
|
file.body = filename.toStdString();
|
|
|
|
|
|
|
|
if (encryptedFile)
|
|
|
|
file.file = encryptedFile;
|
|
|
|
else
|
|
|
|
file.url = url.toStdString();
|
|
|
|
|
|
|
|
if (!room->reply().isEmpty()) {
|
|
|
|
file.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::InReplyTo, room->reply().toStdString()});
|
|
|
|
}
|
|
|
|
if (!room->edit().isEmpty()) {
|
|
|
|
file.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::Replace, room->edit().toStdString()});
|
|
|
|
}
|
|
|
|
|
|
|
|
room->sendMessageEvent(file, mtx::events::EventType::RoomMessage);
|
2020-11-15 06:52:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InputBar::audio(const QString &filename,
|
|
|
|
const std::optional<mtx::crypto::EncryptedFile> &file,
|
|
|
|
const QString &url,
|
|
|
|
const QString &mime,
|
|
|
|
uint64_t dsize)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::msg::Audio audio;
|
|
|
|
audio.info.mimetype = mime.toStdString();
|
|
|
|
audio.info.size = dsize;
|
|
|
|
audio.body = filename.toStdString();
|
|
|
|
audio.url = url.toStdString();
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
audio.file = file;
|
|
|
|
else
|
|
|
|
audio.url = url.toStdString();
|
|
|
|
|
|
|
|
if (!room->reply().isEmpty()) {
|
|
|
|
audio.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::InReplyTo, room->reply().toStdString()});
|
|
|
|
}
|
|
|
|
if (!room->edit().isEmpty()) {
|
|
|
|
audio.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::Replace, room->edit().toStdString()});
|
|
|
|
}
|
|
|
|
|
|
|
|
room->sendMessageEvent(audio, mtx::events::EventType::RoomMessage);
|
2020-11-15 06:52:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InputBar::video(const QString &filename,
|
|
|
|
const std::optional<mtx::crypto::EncryptedFile> &file,
|
|
|
|
const QString &url,
|
|
|
|
const QString &mime,
|
|
|
|
uint64_t dsize)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::msg::Video video;
|
|
|
|
video.info.mimetype = mime.toStdString();
|
|
|
|
video.info.size = dsize;
|
|
|
|
video.body = filename.toStdString();
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
video.file = file;
|
|
|
|
else
|
|
|
|
video.url = url.toStdString();
|
|
|
|
|
|
|
|
if (!room->reply().isEmpty()) {
|
|
|
|
video.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::InReplyTo, room->reply().toStdString()});
|
|
|
|
}
|
|
|
|
if (!room->edit().isEmpty()) {
|
|
|
|
video.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::Replace, room->edit().toStdString()});
|
|
|
|
}
|
|
|
|
|
|
|
|
room->sendMessageEvent(video, mtx::events::EventType::RoomMessage);
|
2020-11-15 06:52:49 +03:00
|
|
|
}
|
|
|
|
|
2021-07-15 21:37:52 +03:00
|
|
|
void
|
2021-07-21 02:03:38 +03:00
|
|
|
InputBar::sticker(CombinedImagePackModel *model, int row)
|
2021-07-15 21:37:52 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!model || row < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto img = model->imageAt(row);
|
|
|
|
|
|
|
|
mtx::events::msg::StickerImage sticker{};
|
|
|
|
sticker.info = img.info.value_or(mtx::common::ImageInfo{});
|
|
|
|
sticker.url = img.url;
|
|
|
|
sticker.body = img.body;
|
|
|
|
|
|
|
|
// workaround for https://github.com/vector-im/element-ios/issues/2353
|
|
|
|
sticker.info.thumbnail_url = sticker.url;
|
|
|
|
sticker.info.thumbnail_info.mimetype = sticker.info.mimetype;
|
|
|
|
sticker.info.thumbnail_info.size = sticker.info.size;
|
|
|
|
sticker.info.thumbnail_info.h = sticker.info.h;
|
|
|
|
sticker.info.thumbnail_info.w = sticker.info.w;
|
|
|
|
|
|
|
|
if (!room->reply().isEmpty()) {
|
|
|
|
sticker.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::InReplyTo, room->reply().toStdString()});
|
|
|
|
}
|
|
|
|
if (!room->edit().isEmpty()) {
|
|
|
|
sticker.relations.relations.push_back(
|
|
|
|
{mtx::common::RelationType::Replace, room->edit().toStdString()});
|
|
|
|
}
|
|
|
|
|
|
|
|
room->sendMessageEvent(sticker, mtx::events::EventType::Sticker);
|
2021-07-15 21:37:52 +03:00
|
|
|
}
|
|
|
|
|
2020-11-09 05:12:37 +03:00
|
|
|
void
|
2021-12-03 03:54:43 +03:00
|
|
|
InputBar::command(const QString &command, QString args)
|
2020-11-09 05:12:37 +03:00
|
|
|
{
|
2021-12-29 06:28:08 +03:00
|
|
|
if (command == QLatin1String("me")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
emote(args, false);
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("react")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
auto eventId = room->reply();
|
|
|
|
if (!eventId.isEmpty())
|
|
|
|
reaction(eventId, args.trimmed());
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("join")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
ChatPage::instance()->joinRoom(args);
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("part") || command == QLatin1String("leave")) {
|
2021-09-24 04:18:48 +03:00
|
|
|
ChatPage::instance()->timelineManager()->openLeaveRoomDialog(room->roomId());
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("invite")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
ChatPage::instance()->inviteUser(args.section(' ', 0, 0), args.section(' ', 1, -1));
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("kick")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
ChatPage::instance()->kickUser(args.section(' ', 0, 0), args.section(' ', 1, -1));
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("ban")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
ChatPage::instance()->banUser(args.section(' ', 0, 0), args.section(' ', 1, -1));
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("unban")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
ChatPage::instance()->unbanUser(args.section(' ', 0, 0), args.section(' ', 1, -1));
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("roomnick")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
mtx::events::state::Member member;
|
|
|
|
member.display_name = args.toStdString();
|
|
|
|
member.avatar_url =
|
|
|
|
cache::avatarUrl(room->roomId(),
|
|
|
|
QString::fromStdString(http::client()->user_id().to_string()))
|
|
|
|
.toStdString();
|
|
|
|
member.membership = mtx::events::state::Membership::Join;
|
|
|
|
|
2021-12-03 03:54:43 +03:00
|
|
|
http::client()->send_state_event(
|
|
|
|
room->roomId().toStdString(),
|
|
|
|
http::client()->user_id().to_string(),
|
|
|
|
member,
|
|
|
|
[](const mtx::responses::EventId &, mtx::http::RequestErr err) {
|
|
|
|
if (err)
|
|
|
|
nhlog::net()->error("Failed to set room displayname: {}",
|
|
|
|
err->matrix_error.error);
|
|
|
|
});
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("shrug")) {
|
|
|
|
message("¯\\_(ツ)_/¯" + (args.isEmpty() ? QLatin1String("") : " " + args));
|
|
|
|
} else if (command == QLatin1String("fliptable")) {
|
|
|
|
message(QStringLiteral("(╯°□°)╯︵ ┻━┻"));
|
|
|
|
} else if (command == QLatin1String("unfliptable")) {
|
|
|
|
message(QStringLiteral(" ┯━┯╭( º _ º╭)"));
|
|
|
|
} else if (command == QLatin1String("sovietflip")) {
|
|
|
|
message(QStringLiteral("ノ┬─┬ノ ︵ ( \\o°o)\\"));
|
|
|
|
} else if (command == QLatin1String("clear-timeline")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
room->clearTimeline();
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("rotate-megolm-session")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
cache::dropOutboundMegolmSession(room->roomId().toStdString());
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("md")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
message(args, MarkdownOverride::ON);
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("plain")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
message(args, MarkdownOverride::OFF);
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("rainbow")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
message(args, MarkdownOverride::ON, true);
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("rainbowme")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
emote(args, true);
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("notice")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
notice(args, false);
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("rainbownotice")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
notice(args, true);
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("goto")) {
|
2021-09-18 01:22:33 +03:00
|
|
|
// Goto has three different modes:
|
|
|
|
// 1 - Going directly to a given event ID
|
|
|
|
if (args[0] == '$') {
|
|
|
|
room->showEvent(args);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 2 - Going directly to a given message index
|
|
|
|
if (args[0] >= '0' && args[0] <= '9') {
|
|
|
|
room->showEvent(args);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 3 - Matrix URI handler, as if you clicked the URI
|
|
|
|
if (ChatPage::instance()->handleMatrixUri(args)) {
|
|
|
|
return;
|
2020-11-09 05:12:37 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::net()->error("Could not resolve goto: {}", args.toStdString());
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("converttodm")) {
|
2021-11-21 07:23:38 +03:00
|
|
|
utils::markRoomAsDirect(this->room->roomId(),
|
|
|
|
cache::getMembers(this->room->roomId().toStdString(), 0, -1));
|
2021-12-29 06:28:08 +03:00
|
|
|
} else if (command == QLatin1String("converttoroom")) {
|
2021-11-21 07:23:38 +03:00
|
|
|
utils::removeDirectFromRoom(this->room->roomId());
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-11-09 05:12:37 +03:00
|
|
|
}
|
2020-11-15 06:52:49 +03:00
|
|
|
|
|
|
|
void
|
2021-12-03 03:54:43 +03:00
|
|
|
InputBar::showPreview(const QMimeData &source, const QString &path, const QStringList &formats)
|
2020-11-15 06:52:49 +03:00
|
|
|
{
|
2022-01-12 21:09:46 +03:00
|
|
|
auto *previewDialog_ = new dialogs::PreviewUploadOverlay(nullptr);
|
2021-09-18 01:22:33 +03:00
|
|
|
previewDialog_->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
2021-09-28 02:42:35 +03:00
|
|
|
// Force SVG to _not_ be handled as an image, but as raw data
|
2021-12-29 08:01:38 +03:00
|
|
|
if (source.hasImage() &&
|
|
|
|
(formats.empty() || formats.front() != QLatin1String("image/svg+xml"))) {
|
2021-12-29 06:28:08 +03:00
|
|
|
if (!formats.empty() && formats.front().startsWith(QLatin1String("image/"))) {
|
2021-09-28 00:19:43 +03:00
|
|
|
// known format, keep as-is
|
|
|
|
previewDialog_->setPreview(qvariant_cast<QImage>(source.imageData()), formats.front());
|
|
|
|
} else {
|
|
|
|
// unknown image format, default to image/png
|
2021-12-29 08:01:38 +03:00
|
|
|
previewDialog_->setPreview(qvariant_cast<QImage>(source.imageData()),
|
|
|
|
QStringLiteral("image/png"));
|
2021-09-28 00:19:43 +03:00
|
|
|
}
|
|
|
|
} else if (!path.isEmpty())
|
2021-09-18 01:22:33 +03:00
|
|
|
previewDialog_->setPreview(path);
|
|
|
|
else if (!formats.isEmpty()) {
|
2021-12-03 03:54:43 +03:00
|
|
|
const auto &mime = formats.first();
|
2021-09-18 01:22:33 +03:00
|
|
|
previewDialog_->setPreview(source.data(mime), mime);
|
|
|
|
} else {
|
|
|
|
setUploading(false);
|
|
|
|
previewDialog_->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(previewDialog_, &dialogs::PreviewUploadOverlay::aborted, this, [this]() {
|
|
|
|
setUploading(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(
|
|
|
|
previewDialog_,
|
|
|
|
&dialogs::PreviewUploadOverlay::confirmUpload,
|
|
|
|
this,
|
2021-12-03 03:54:43 +03:00
|
|
|
[this](const QByteArray &data, const QString &mime, const QString &fn) {
|
2021-09-28 02:42:35 +03:00
|
|
|
if (!data.size()) {
|
|
|
|
nhlog::ui()->warn("Attempted to upload zero-byte file?! Mimetype {}, filename {}",
|
|
|
|
mime.toStdString(),
|
|
|
|
fn.toStdString());
|
|
|
|
return;
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
setUploading(true);
|
|
|
|
|
2021-12-29 06:28:08 +03:00
|
|
|
setText(QLatin1String(""));
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
auto payload = std::string(data.data(), data.size());
|
|
|
|
std::optional<mtx::crypto::EncryptedFile> encryptedFile;
|
|
|
|
if (cache::isRoomEncrypted(room->roomId().toStdString())) {
|
|
|
|
mtx::crypto::BinaryBuf buf;
|
|
|
|
std::tie(buf, encryptedFile) = mtx::crypto::encrypt_file(payload);
|
|
|
|
payload = mtx::crypto::to_string(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize dimensions;
|
|
|
|
QString blurhash;
|
2021-12-30 04:10:33 +03:00
|
|
|
auto mimeClass = mime.left(mime.indexOf(u'/'));
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::ui()->debug("Mime: {}", mime.toStdString());
|
2021-12-28 22:09:08 +03:00
|
|
|
if (mimeClass == u"image") {
|
2021-09-18 01:22:33 +03:00
|
|
|
QImage img = utils::readImage(data);
|
|
|
|
|
|
|
|
dimensions = img.size();
|
|
|
|
if (img.height() > 200 && img.width() > 360)
|
|
|
|
img = img.scaled(360, 200, Qt::KeepAspectRatioByExpanding);
|
|
|
|
std::vector<unsigned char> data_;
|
|
|
|
for (int y = 0; y < img.height(); y++) {
|
|
|
|
for (int x = 0; x < img.width(); x++) {
|
|
|
|
auto p = img.pixel(x, y);
|
|
|
|
data_.push_back(static_cast<unsigned char>(qRed(p)));
|
|
|
|
data_.push_back(static_cast<unsigned char>(qGreen(p)));
|
|
|
|
data_.push_back(static_cast<unsigned char>(qBlue(p)));
|
2020-11-15 06:52:49 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
blurhash = QString::fromStdString(
|
|
|
|
blurhash::encode(data_.data(), img.width(), img.height(), 4, 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
http::client()->upload(
|
|
|
|
payload,
|
|
|
|
encryptedFile ? "application/octet-stream" : mime.toStdString(),
|
|
|
|
QFileInfo(fn).fileName().toStdString(),
|
|
|
|
[this,
|
|
|
|
filename = fn,
|
|
|
|
encryptedFile = std::move(encryptedFile),
|
|
|
|
mimeClass,
|
|
|
|
mime,
|
|
|
|
size = payload.size(),
|
|
|
|
dimensions,
|
|
|
|
blurhash](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) mutable {
|
|
|
|
if (err) {
|
|
|
|
emit ChatPage::instance()->showNotification(
|
|
|
|
tr("Failed to upload media. Please try again."));
|
|
|
|
nhlog::net()->warn("failed to upload media: {} {} ({})",
|
|
|
|
err->matrix_error.error,
|
|
|
|
to_string(err->matrix_error.errcode),
|
|
|
|
static_cast<int>(err->status_code));
|
|
|
|
setUploading(false);
|
|
|
|
return;
|
|
|
|
}
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto url = QString::fromStdString(res.content_uri);
|
|
|
|
if (encryptedFile)
|
|
|
|
encryptedFile->url = res.content_uri;
|
|
|
|
|
2021-12-30 04:10:33 +03:00
|
|
|
if (mimeClass == u"image")
|
2021-09-18 01:22:33 +03:00
|
|
|
image(filename, encryptedFile, url, mime, size, dimensions, blurhash);
|
2021-12-30 04:10:33 +03:00
|
|
|
else if (mimeClass == u"audio")
|
2021-09-18 01:22:33 +03:00
|
|
|
audio(filename, encryptedFile, url, mime, size);
|
2021-12-30 04:10:33 +03:00
|
|
|
else if (mimeClass == u"video")
|
2021-09-18 01:22:33 +03:00
|
|
|
video(filename, encryptedFile, url, mime, size);
|
|
|
|
else
|
|
|
|
file(filename, encryptedFile, url, mime, size);
|
2020-11-15 06:52:49 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
setUploading(false);
|
|
|
|
});
|
|
|
|
});
|
2020-11-15 06:52:49 +03:00
|
|
|
}
|
2020-11-16 01:14:47 +03:00
|
|
|
|
2020-11-17 04:37:43 +03:00
|
|
|
void
|
|
|
|
InputBar::startTyping()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!typingRefresh_.isActive()) {
|
|
|
|
typingRefresh_.start();
|
|
|
|
|
|
|
|
if (ChatPage::instance()->userSettings()->typingNotifications()) {
|
|
|
|
http::client()->start_typing(
|
|
|
|
room->roomId().toStdString(), 10'000, [](mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to send typing notification: {}",
|
|
|
|
err->matrix_error.error);
|
|
|
|
}
|
|
|
|
});
|
2020-11-17 04:37:43 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
typingTimeout_.start();
|
2020-11-17 04:37:43 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
InputBar::stopTyping()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
typingRefresh_.stop();
|
|
|
|
typingTimeout_.stop();
|
2020-11-17 04:37:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!ChatPage::instance()->userSettings()->typingNotifications())
|
|
|
|
return;
|
2020-11-17 04:37:43 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
http::client()->stop_typing(room->roomId().toStdString(), [](mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
nhlog::net()->warn("failed to stop typing notifications: {}", err->matrix_error.error);
|
|
|
|
}
|
|
|
|
});
|
2020-11-17 04:37:43 +03:00
|
|
|
}
|
2021-05-28 23:14:59 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
InputBar::reaction(const QString &reactedEvent, const QString &reactionKey)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto reactions = room->reactions(reactedEvent.toStdString());
|
2021-05-28 23:14:59 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QString selfReactedEvent;
|
|
|
|
for (const auto &reaction : reactions) {
|
|
|
|
if (reactionKey == reaction.key_) {
|
|
|
|
selfReactedEvent = reaction.selfReactedEvent_;
|
|
|
|
break;
|
2021-05-28 23:14:59 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
|
2021-12-29 06:28:08 +03:00
|
|
|
if (selfReactedEvent.startsWith(QLatin1String("m")))
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
// If selfReactedEvent is empty, that means we haven't previously reacted
|
|
|
|
if (selfReactedEvent.isEmpty()) {
|
|
|
|
mtx::events::msg::Reaction reaction;
|
|
|
|
mtx::common::Relation rel;
|
|
|
|
rel.rel_type = mtx::common::RelationType::Annotation;
|
|
|
|
rel.event_id = reactedEvent.toStdString();
|
|
|
|
rel.key = reactionKey.toStdString();
|
|
|
|
reaction.relations.relations.push_back(rel);
|
|
|
|
|
|
|
|
room->sendMessageEvent(reaction, mtx::events::EventType::Reaction);
|
2021-12-13 02:43:05 +03:00
|
|
|
|
|
|
|
auto recents = UserSettings::instance()->recentReactions();
|
|
|
|
if (recents.contains(reactionKey))
|
|
|
|
recents.removeOne(reactionKey);
|
|
|
|
else if (recents.size() >= 6)
|
|
|
|
recents.removeLast();
|
|
|
|
recents.push_front(reactionKey);
|
|
|
|
UserSettings::instance()->setRecentReactions(recents);
|
2021-09-18 01:22:33 +03:00
|
|
|
// Otherwise, we have previously reacted and the reaction should be redacted
|
|
|
|
} else {
|
|
|
|
room->redactEvent(selfReactedEvent);
|
|
|
|
}
|
2021-05-28 23:14:59 +03:00
|
|
|
}
|