2017-04-08 13:27:53 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-04-08 13:27:53 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-04-08 13:27:53 +00:00
|
|
|
*/
|
|
|
|
#include "chat_helpers/message_field.h"
|
|
|
|
|
2017-06-29 10:27:09 +00:00
|
|
|
#include "history/history_widget.h"
|
2019-06-05 18:40:21 +00:00
|
|
|
#include "history/history.h" // History::session
|
|
|
|
#include "history/history_item.h" // HistoryItem::originalText
|
2017-04-08 13:27:53 +00:00
|
|
|
#include "base/qthelp_regex.h"
|
2018-07-31 19:51:06 +00:00
|
|
|
#include "base/qthelp_url.h"
|
2019-09-19 09:28:36 +00:00
|
|
|
#include "base/event_filter.h"
|
2018-05-26 14:58:21 +00:00
|
|
|
#include "boxes/abstract_box.h"
|
2019-12-23 15:14:12 +00:00
|
|
|
#include "core/shortcuts.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2018-05-26 14:58:21 +00:00
|
|
|
#include "ui/wrap/vertical_layout.h"
|
2019-07-26 16:06:22 +00:00
|
|
|
#include "ui/widgets/popup_menu.h"
|
2019-09-13 12:22:54 +00:00
|
|
|
#include "ui/ui_utility.h"
|
2019-01-03 12:36:01 +00:00
|
|
|
#include "data/data_session.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2018-11-21 10:09:46 +00:00
|
|
|
#include "chat_helpers/emoji_suggestions_widget.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2018-05-26 14:58:21 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-04-08 13:27:53 +00:00
|
|
|
#include "mainwindow.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2018-05-26 14:58:21 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2020-10-10 09:15:37 +00:00
|
|
|
#include "styles/style_chat.h"
|
2021-10-19 13:00:21 +00:00
|
|
|
#include "base/qt_adapters.h"
|
2017-04-08 13:27:53 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QMimeData>
|
|
|
|
#include <QtCore/QStack>
|
|
|
|
#include <QtGui/QGuiApplication>
|
|
|
|
#include <QtGui/QTextBlock>
|
|
|
|
#include <QtGui/QClipboard>
|
2019-12-24 10:27:36 +00:00
|
|
|
#include <QtWidgets/QApplication>
|
2019-09-04 07:19:15 +00:00
|
|
|
|
2017-04-08 13:27:53 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-06-04 06:53:59 +00:00
|
|
|
using namespace Ui::Text;
|
|
|
|
|
2018-05-26 14:58:21 +00:00
|
|
|
using EditLinkAction = Ui::InputField::EditLinkAction;
|
|
|
|
using EditLinkSelection = Ui::InputField::EditLinkSelection;
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kParseLinksTimeout = crl::time(1000);
|
2018-05-21 21:31:46 +00:00
|
|
|
|
2017-04-08 13:27:53 +00:00
|
|
|
// For mention tags save and validate userId, ignore tags for different userId.
|
2018-05-21 21:31:46 +00:00
|
|
|
class FieldTagMimeProcessor : public Ui::InputField::TagMimeProcessor {
|
2017-04-08 13:27:53 +00:00
|
|
|
public:
|
2020-06-08 09:06:50 +00:00
|
|
|
explicit FieldTagMimeProcessor(
|
|
|
|
not_null<Window::SessionController*> controller);
|
|
|
|
|
|
|
|
QString tagFromMimeTag(const QString &mimeTag) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const not_null<Window::SessionController*> _controller;
|
2017-04-08 13:27:53 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-09-18 11:19:05 +00:00
|
|
|
class EditLinkBox : public Ui::BoxContent {
|
2018-05-26 14:58:21 +00:00
|
|
|
public:
|
|
|
|
EditLinkBox(
|
|
|
|
QWidget*,
|
2020-06-10 18:08:17 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2018-05-26 14:58:21 +00:00
|
|
|
const QString &text,
|
|
|
|
const QString &link,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(QString, QString)> callback);
|
2018-05-26 14:58:21 +00:00
|
|
|
|
|
|
|
void setInnerFocus() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void prepare() override;
|
|
|
|
|
|
|
|
private:
|
2020-06-10 18:08:17 +00:00
|
|
|
const not_null<Window::SessionController*> _controller;
|
2018-05-26 14:58:21 +00:00
|
|
|
QString _startText;
|
|
|
|
QString _startLink;
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(QString, QString)> _callback;
|
|
|
|
Fn<void()> _setInnerFocus;
|
2018-05-26 14:58:21 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
FieldTagMimeProcessor::FieldTagMimeProcessor(
|
|
|
|
not_null<Window::SessionController*> controller)
|
|
|
|
: _controller(controller) {
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FieldTagMimeProcessor::tagFromMimeTag(const QString &mimeTag) {
|
|
|
|
if (TextUtilities::IsMentionLink(mimeTag)) {
|
|
|
|
const auto userId = _controller->session().userId();
|
|
|
|
auto match = QRegularExpression(":(\\d+)$").match(mimeTag);
|
|
|
|
if (!match.hasMatch()
|
2021-10-19 13:00:21 +00:00
|
|
|
|| match.capturedView(1).toULongLong() != userId.bare) {
|
2020-06-08 09:06:50 +00:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
return mimeTag.mid(0, mimeTag.size() - match.capturedLength());
|
|
|
|
}
|
|
|
|
return mimeTag;
|
|
|
|
}
|
|
|
|
|
2018-05-26 14:58:21 +00:00
|
|
|
//bool ValidateUrl(const QString &value) {
|
2018-08-08 20:11:28 +00:00
|
|
|
// const auto match = qthelp::RegExpDomain().match(value);
|
2018-05-26 14:58:21 +00:00
|
|
|
// if (!match.hasMatch() || match.capturedStart() != 0) {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// const auto protocolMatch = RegExpProtocol().match(value);
|
|
|
|
// return protocolMatch.hasMatch()
|
|
|
|
// && IsGoodProtocol(protocolMatch.captured(1));
|
|
|
|
//}
|
|
|
|
|
|
|
|
EditLinkBox::EditLinkBox(
|
|
|
|
QWidget*,
|
2020-06-10 18:08:17 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2018-05-26 14:58:21 +00:00
|
|
|
const QString &text,
|
|
|
|
const QString &link,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(QString, QString)> callback)
|
2020-06-10 18:08:17 +00:00
|
|
|
: _controller(controller)
|
2019-08-02 10:40:35 +00:00
|
|
|
, _startText(text)
|
2018-05-26 14:58:21 +00:00
|
|
|
, _startLink(link)
|
|
|
|
, _callback(std::move(callback)) {
|
|
|
|
Expects(_callback != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditLinkBox::setInnerFocus() {
|
|
|
|
Expects(_setInnerFocus != nullptr);
|
|
|
|
|
|
|
|
_setInnerFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditLinkBox::prepare() {
|
|
|
|
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
|
|
|
|
|
2020-06-10 18:08:17 +00:00
|
|
|
const auto session = &_controller->session();
|
2018-05-26 14:58:21 +00:00
|
|
|
const auto text = content->add(
|
|
|
|
object_ptr<Ui::InputField>(
|
|
|
|
content,
|
|
|
|
st::defaultInputField,
|
2019-06-18 14:07:45 +00:00
|
|
|
tr::lng_formatting_link_text(),
|
2018-05-26 14:58:21 +00:00
|
|
|
_startText),
|
|
|
|
st::markdownLinkFieldPadding);
|
|
|
|
text->setInstantReplaces(Ui::InstantReplaces::Default());
|
2019-08-02 10:40:35 +00:00
|
|
|
text->setInstantReplacesEnabled(
|
2020-06-18 18:04:16 +00:00
|
|
|
Core::App().settings().replaceEmojiValue());
|
2018-11-21 10:09:46 +00:00
|
|
|
Ui::Emoji::SuggestionsController::Init(
|
|
|
|
getDelegate()->outerContainer(),
|
2019-08-02 10:40:35 +00:00
|
|
|
text,
|
2020-06-10 18:08:17 +00:00
|
|
|
session);
|
|
|
|
InitSpellchecker(_controller, text);
|
2018-05-26 14:58:21 +00:00
|
|
|
|
2020-09-25 11:12:36 +00:00
|
|
|
const auto placeholder = content->add(
|
|
|
|
object_ptr<Ui::RpWidget>(content),
|
|
|
|
st::markdownLinkFieldPadding);
|
|
|
|
placeholder->setAttribute(Qt::WA_TransparentForMouseEvents);
|
|
|
|
const auto url = Ui::AttachParentChild(
|
|
|
|
content,
|
|
|
|
object_ptr<Ui::MaskedInputField>(
|
2018-05-26 14:58:21 +00:00
|
|
|
content,
|
|
|
|
st::defaultInputField,
|
2019-06-18 14:07:45 +00:00
|
|
|
tr::lng_formatting_link_url(),
|
2020-09-25 11:12:36 +00:00
|
|
|
_startLink.trimmed()));
|
|
|
|
url->heightValue(
|
|
|
|
) | rpl::start_with_next([placeholder](int height) {
|
|
|
|
placeholder->resize(placeholder->width(), height);
|
|
|
|
}, placeholder->lifetime());
|
|
|
|
placeholder->widthValue(
|
|
|
|
) | rpl::start_with_next([=](int width) {
|
|
|
|
url->resize(width, url->height());
|
|
|
|
}, placeholder->lifetime());
|
|
|
|
url->move(placeholder->pos());
|
2018-05-26 14:58:21 +00:00
|
|
|
|
|
|
|
const auto submit = [=] {
|
|
|
|
const auto linkText = text->getLastText();
|
2018-07-31 19:51:06 +00:00
|
|
|
const auto linkUrl = qthelp::validate_url(url->getLastText());
|
2018-05-26 14:58:21 +00:00
|
|
|
if (linkText.isEmpty()) {
|
|
|
|
text->showError();
|
|
|
|
return;
|
|
|
|
} else if (linkUrl.isEmpty()) {
|
|
|
|
url->showError();
|
|
|
|
return;
|
|
|
|
}
|
2019-09-13 12:22:54 +00:00
|
|
|
const auto weak = Ui::MakeWeak(this);
|
2018-05-26 14:58:21 +00:00
|
|
|
_callback(linkText, linkUrl);
|
|
|
|
if (weak) {
|
|
|
|
closeBox();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
connect(text, &Ui::InputField::submitted, [=] {
|
|
|
|
url->setFocusFast();
|
|
|
|
});
|
2020-09-25 11:12:36 +00:00
|
|
|
connect(url, &Ui::MaskedInputField::submitted, [=] {
|
2018-05-26 14:58:21 +00:00
|
|
|
if (text->getLastText().isEmpty()) {
|
|
|
|
text->setFocusFast();
|
|
|
|
} else {
|
|
|
|
submit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-18 15:00:55 +00:00
|
|
|
setTitle(url->getLastText().isEmpty()
|
|
|
|
? tr::lng_formatting_link_create_title()
|
|
|
|
: tr::lng_formatting_link_edit_title());
|
2018-05-26 14:58:21 +00:00
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_formatting_link_create(), submit);
|
|
|
|
addButton(tr::lng_cancel(), [=] { closeBox(); });
|
2018-05-26 14:58:21 +00:00
|
|
|
|
|
|
|
content->resizeToWidth(st::boxWidth);
|
|
|
|
content->moveToLeft(0, 0);
|
|
|
|
setDimensions(st::boxWidth, content->height());
|
|
|
|
|
|
|
|
_setInnerFocus = [=] {
|
2020-09-25 11:12:36 +00:00
|
|
|
if (_startText.isEmpty()) {
|
|
|
|
text->setFocusFast();
|
|
|
|
} else {
|
|
|
|
url->setFocusFast();
|
|
|
|
}
|
2018-05-26 14:58:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:40:21 +00:00
|
|
|
TextWithEntities StripSupportHashtag(TextWithEntities &&text) {
|
|
|
|
static const auto expression = QRegularExpression(
|
|
|
|
qsl("\\n?#tsf[a-z0-9_-]*[\\s#a-z0-9_-]*$"),
|
|
|
|
QRegularExpression::CaseInsensitiveOption);
|
|
|
|
const auto match = expression.match(text.text);
|
|
|
|
if (!match.hasMatch()) {
|
2019-06-06 11:01:28 +00:00
|
|
|
return std::move(text);
|
2019-06-05 18:40:21 +00:00
|
|
|
}
|
|
|
|
text.text.chop(match.capturedLength());
|
|
|
|
const auto length = text.text.size();
|
|
|
|
if (!length) {
|
|
|
|
return TextWithEntities();
|
|
|
|
}
|
|
|
|
for (auto i = text.entities.begin(); i != text.entities.end();) {
|
|
|
|
auto &entity = *i;
|
|
|
|
if (entity.offset() >= length) {
|
|
|
|
i = text.entities.erase(i);
|
2019-06-08 09:39:15 +00:00
|
|
|
continue;
|
2019-06-05 18:40:21 +00:00
|
|
|
} else if (entity.offset() + entity.length() > length) {
|
|
|
|
entity.shrinkFromRight(length - entity.offset());
|
|
|
|
}
|
2019-06-08 09:39:15 +00:00
|
|
|
++i;
|
2019-06-05 18:40:21 +00:00
|
|
|
}
|
2019-06-06 11:01:28 +00:00
|
|
|
return std::move(text);
|
2019-06-05 18:40:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 13:27:53 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-05-26 14:58:21 +00:00
|
|
|
QString PrepareMentionTag(not_null<UserData*> user) {
|
2019-09-16 11:14:06 +00:00
|
|
|
return TextUtilities::kMentionTagStart
|
2021-04-01 21:04:10 +00:00
|
|
|
+ QString::number(user->id.value)
|
2018-05-26 14:58:21 +00:00
|
|
|
+ '.'
|
|
|
|
+ QString::number(user->accessHash());
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:40:21 +00:00
|
|
|
TextWithTags PrepareEditText(not_null<HistoryItem*> item) {
|
|
|
|
const auto original = item->history()->session().supportMode()
|
|
|
|
? StripSupportHashtag(item->originalText())
|
|
|
|
: item->originalText();
|
|
|
|
return TextWithTags{
|
|
|
|
original.text,
|
2019-09-16 11:14:06 +00:00
|
|
|
TextUtilities::ConvertEntitiesToTextTags(original.entities)
|
2019-06-05 18:40:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<bool(
|
2018-05-26 14:58:21 +00:00
|
|
|
Ui::InputField::EditLinkSelection selection,
|
|
|
|
QString text,
|
|
|
|
QString link,
|
|
|
|
EditLinkAction action)> DefaultEditLinkCallback(
|
2020-06-10 18:08:17 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2018-05-26 14:58:21 +00:00
|
|
|
not_null<Ui::InputField*> field) {
|
2019-09-13 12:22:54 +00:00
|
|
|
const auto weak = Ui::MakeWeak(field);
|
2018-05-26 14:58:21 +00:00
|
|
|
return [=](
|
|
|
|
EditLinkSelection selection,
|
|
|
|
QString text,
|
|
|
|
QString link,
|
|
|
|
EditLinkAction action) {
|
|
|
|
if (action == EditLinkAction::Check) {
|
|
|
|
return Ui::InputField::IsValidMarkdownLink(link)
|
2019-09-16 11:14:06 +00:00
|
|
|
&& !TextUtilities::IsMentionLink(link);
|
2018-05-26 14:58:21 +00:00
|
|
|
}
|
2021-06-13 07:37:52 +00:00
|
|
|
controller->show(Box<EditLinkBox>(controller, text, link, [=](
|
2018-05-26 14:58:21 +00:00
|
|
|
const QString &text,
|
|
|
|
const QString &link) {
|
|
|
|
if (const auto strong = weak.data()) {
|
|
|
|
strong->commitMarkdownLinkEdit(selection, text, link);
|
|
|
|
}
|
2019-09-18 11:19:05 +00:00
|
|
|
}), Ui::LayerOption::KeepOther);
|
2018-05-26 14:58:21 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitMessageField(
|
2019-06-06 10:21:40 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2018-05-26 14:58:21 +00:00
|
|
|
not_null<Ui::InputField*> field) {
|
2018-05-21 21:31:46 +00:00
|
|
|
field->setMinHeight(st::historySendSize.height() - 2 * st::historySendPadding);
|
|
|
|
field->setMaxHeight(st::historyComposeFieldMaxHeight);
|
2017-04-08 13:27:53 +00:00
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
field->setTagMimeProcessor(
|
|
|
|
std::make_unique<FieldTagMimeProcessor>(controller));
|
2018-05-13 15:14:02 +00:00
|
|
|
|
2018-05-21 21:31:46 +00:00
|
|
|
field->document()->setDocumentMargin(4.);
|
2019-09-13 10:24:06 +00:00
|
|
|
field->setAdditionalMargin(style::ConvertScale(4) - 4);
|
2018-05-21 21:31:46 +00:00
|
|
|
|
2018-05-22 19:09:13 +00:00
|
|
|
field->customTab(true);
|
2018-05-21 21:31:46 +00:00
|
|
|
field->setInstantReplaces(Ui::InstantReplaces::Default());
|
2019-08-02 10:40:35 +00:00
|
|
|
field->setInstantReplacesEnabled(
|
2020-06-18 18:04:16 +00:00
|
|
|
Core::App().settings().replaceEmojiValue());
|
2018-05-25 17:47:29 +00:00
|
|
|
field->setMarkdownReplacesEnabled(rpl::single(true));
|
2020-06-10 18:08:17 +00:00
|
|
|
field->setEditLinkCallback(DefaultEditLinkCallback(controller, field));
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:27:21 +00:00
|
|
|
void InitSpellchecker(
|
2020-06-10 18:08:17 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2019-11-05 11:27:21 +00:00
|
|
|
not_null<Ui::InputField*> field) {
|
|
|
|
#ifndef TDESKTOP_DISABLE_SPELLCHECK
|
2019-11-05 14:03:48 +00:00
|
|
|
const auto s = Ui::CreateChild<Spellchecker::SpellingHighlighter>(
|
2019-11-06 08:05:40 +00:00
|
|
|
field.get(),
|
2020-06-18 18:04:16 +00:00
|
|
|
Core::App().settings().spellcheckerEnabledValue(),
|
2020-02-21 09:48:24 +00:00
|
|
|
Spellchecker::SpellingHighlighter::CustomContextMenuItem{
|
|
|
|
tr::lng_settings_manage_dictionaries(tr::now),
|
2021-06-13 07:37:52 +00:00
|
|
|
[=] {
|
|
|
|
controller->show(Box<Ui::ManageDictionariesBox>(controller));
|
|
|
|
}
|
2020-02-21 09:48:24 +00:00
|
|
|
});
|
2019-10-18 13:54:26 +00:00
|
|
|
field->setExtendedContextMenu(s->contextMenuCreated());
|
2019-11-05 11:27:21 +00:00
|
|
|
#endif // TDESKTOP_DISABLE_SPELLCHECK
|
2019-10-18 13:54:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:31:46 +00:00
|
|
|
bool HasSendText(not_null<const Ui::InputField*> field) {
|
|
|
|
const auto &text = field->getTextWithTags().text;
|
2021-09-08 10:53:54 +00:00
|
|
|
for (const auto &ch : text) {
|
2018-05-21 21:31:46 +00:00
|
|
|
const auto code = ch.unicode();
|
|
|
|
if (code != ' '
|
|
|
|
&& code != '\n'
|
|
|
|
&& code != '\r'
|
2020-06-04 06:53:59 +00:00
|
|
|
&& !IsReplacedBySpace(code)) {
|
2017-04-08 13:27:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
InlineBotQuery ParseInlineBotQuery(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
not_null<const Ui::InputField*> field) {
|
2018-05-21 21:31:46 +00:00
|
|
|
auto result = InlineBotQuery();
|
|
|
|
|
2018-12-03 13:31:16 +00:00
|
|
|
const auto &full = field->getTextWithTags();
|
|
|
|
const auto &text = full.text;
|
2018-05-21 21:31:46 +00:00
|
|
|
const auto textLength = text.size();
|
|
|
|
|
|
|
|
auto inlineUsernameStart = 1;
|
|
|
|
auto inlineUsernameLength = 0;
|
|
|
|
if (textLength > 2 && text[0] == '@' && text[1].isLetter()) {
|
|
|
|
inlineUsernameLength = 1;
|
|
|
|
for (auto i = inlineUsernameStart + 1; i != textLength; ++i) {
|
|
|
|
const auto ch = text[i];
|
|
|
|
if (ch.isLetterOrNumber() || ch.unicode() == '_') {
|
|
|
|
++inlineUsernameLength;
|
|
|
|
continue;
|
|
|
|
} else if (!ch.isSpace()) {
|
|
|
|
inlineUsernameLength = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto inlineUsernameEnd = inlineUsernameStart + inlineUsernameLength;
|
|
|
|
auto inlineUsernameEqualsText = (inlineUsernameEnd == textLength);
|
|
|
|
auto validInlineUsername = false;
|
|
|
|
if (inlineUsernameEqualsText) {
|
|
|
|
validInlineUsername = text.endsWith(qstr("bot"));
|
|
|
|
} else if (inlineUsernameEnd < textLength && inlineUsernameLength) {
|
|
|
|
validInlineUsername = text[inlineUsernameEnd].isSpace();
|
|
|
|
}
|
|
|
|
if (validInlineUsername) {
|
2018-12-03 13:31:16 +00:00
|
|
|
if (!full.tags.isEmpty()
|
|
|
|
&& (full.tags.front().offset
|
|
|
|
< inlineUsernameStart + inlineUsernameLength)) {
|
|
|
|
return InlineBotQuery();
|
|
|
|
}
|
2021-10-19 13:00:21 +00:00
|
|
|
auto username = base::StringViewMid(text, inlineUsernameStart, inlineUsernameLength);
|
2018-05-21 21:31:46 +00:00
|
|
|
if (username != result.username) {
|
|
|
|
result.username = username.toString();
|
2020-06-08 09:06:50 +00:00
|
|
|
if (const auto peer = session->data().peerByUsername(result.username)) {
|
2018-05-21 21:31:46 +00:00
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
result.bot = peer->asUser();
|
|
|
|
} else {
|
|
|
|
result.bot = nullptr;
|
|
|
|
}
|
|
|
|
result.lookingUpBot = false;
|
|
|
|
} else {
|
|
|
|
result.bot = nullptr;
|
|
|
|
result.lookingUpBot = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result.lookingUpBot) {
|
|
|
|
result.query = QString();
|
|
|
|
return result;
|
2019-07-18 08:51:11 +00:00
|
|
|
} else if (result.bot
|
|
|
|
&& (!result.bot->isBot()
|
|
|
|
|| result.bot->botInfo->inlinePlaceholder.isEmpty())) {
|
2018-05-21 21:31:46 +00:00
|
|
|
result.bot = nullptr;
|
|
|
|
} else {
|
|
|
|
result.query = inlineUsernameEqualsText
|
|
|
|
? QString()
|
|
|
|
: text.mid(inlineUsernameEnd + 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inlineUsernameLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inlineUsernameLength < 3) {
|
|
|
|
result.bot = nullptr;
|
|
|
|
result.username = QString();
|
|
|
|
}
|
|
|
|
result.query = QString();
|
|
|
|
return result;
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:31:46 +00:00
|
|
|
AutocompleteQuery ParseMentionHashtagBotCommandQuery(
|
|
|
|
not_null<const Ui::InputField*> field) {
|
|
|
|
auto result = AutocompleteQuery();
|
|
|
|
|
|
|
|
const auto cursor = field->textCursor();
|
2018-05-26 14:58:21 +00:00
|
|
|
if (cursor.hasSelection()) {
|
2018-05-21 21:31:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-26 14:58:21 +00:00
|
|
|
const auto position = cursor.position();
|
2018-05-21 21:31:46 +00:00
|
|
|
const auto document = field->document();
|
|
|
|
const auto block = document->findBlock(position);
|
|
|
|
for (auto item = block.begin(); !item.atEnd(); ++item) {
|
|
|
|
const auto fragment = item.fragment();
|
|
|
|
if (!fragment.isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto fragmentPosition = fragment.position();
|
|
|
|
const auto fragmentEnd = fragmentPosition + fragment.length();
|
|
|
|
if (fragmentPosition >= position || fragmentEnd < position) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto format = fragment.charFormat();
|
|
|
|
if (format.isImageFormat()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mentionInCommand = false;
|
|
|
|
const auto text = fragment.text();
|
|
|
|
for (auto i = position - fragmentPosition; i != 0; --i) {
|
|
|
|
if (text[i - 1] == '@') {
|
|
|
|
if ((position - fragmentPosition - i < 1 || text[i].isLetter()) && (i < 2 || !(text[i - 2].isLetterOrNumber() || text[i - 2] == '_'))) {
|
|
|
|
result.fromStart = (i == 1) && (fragmentPosition == 0);
|
|
|
|
result.query = text.mid(i - 1, position - fragmentPosition - i + 1);
|
|
|
|
} else if ((position - fragmentPosition - i < 1 || text[i].isLetter()) && i > 2 && (text[i - 2].isLetterOrNumber() || text[i - 2] == '_') && !mentionInCommand) {
|
|
|
|
mentionInCommand = true;
|
|
|
|
--i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else if (text[i - 1] == '#') {
|
|
|
|
if (i < 2 || !(text[i - 2].isLetterOrNumber() || text[i - 2] == '_')) {
|
|
|
|
result.fromStart = (i == 1) && (fragmentPosition == 0);
|
|
|
|
result.query = text.mid(i - 1, position - fragmentPosition - i + 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else if (text[i - 1] == '/') {
|
|
|
|
if (i < 2) {
|
|
|
|
result.fromStart = (i == 1) && (fragmentPosition == 0);
|
|
|
|
result.query = text.mid(i - 1, position - fragmentPosition - i + 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (position - fragmentPosition - i > 127 || (!mentionInCommand && (position - fragmentPosition - i > 63))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!text[i - 1].isLetterOrNumber() && text[i - 1] != '_') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
2018-05-21 21:31:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageLinksParser::MessageLinksParser(not_null<Ui::InputField*> field)
|
|
|
|
: _field(field)
|
|
|
|
, _timer([=] { parse(); }) {
|
|
|
|
_connection = QObject::connect(_field, &Ui::InputField::changed, [=] {
|
|
|
|
const auto length = _field->getTextWithTags().text.size();
|
|
|
|
const auto timeout = (std::abs(length - _lastLength) > 2)
|
|
|
|
? 0
|
|
|
|
: kParseLinksTimeout;
|
|
|
|
if (!_timer.isActive() || timeout < _timer.remainingTime()) {
|
|
|
|
_timer.callOnce(timeout);
|
|
|
|
}
|
|
|
|
_lastLength = length;
|
|
|
|
});
|
|
|
|
_field->installEventFilter(this);
|
|
|
|
}
|
|
|
|
|
2021-01-29 11:27:17 +00:00
|
|
|
void MessageLinksParser::parseNow() {
|
|
|
|
_timer.cancel();
|
|
|
|
parse();
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:31:46 +00:00
|
|
|
bool MessageLinksParser::eventFilter(QObject *object, QEvent *event) {
|
|
|
|
if (object == _field) {
|
|
|
|
if (event->type() == QEvent::KeyPress) {
|
|
|
|
const auto text = static_cast<QKeyEvent*>(event)->text();
|
|
|
|
if (!text.isEmpty() && text.size() < 3) {
|
|
|
|
const auto ch = text[0];
|
|
|
|
if (false
|
|
|
|
|| ch == '\n'
|
|
|
|
|| ch == '\r'
|
|
|
|
|| ch.isSpace()
|
|
|
|
|| ch == QChar::LineSeparator) {
|
|
|
|
_timer.callOnce(0);
|
|
|
|
}
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
2018-05-21 21:31:46 +00:00
|
|
|
} else if (event->type() == QEvent::Drop) {
|
|
|
|
_timer.callOnce(0);
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-21 21:31:46 +00:00
|
|
|
return QObject::eventFilter(object, event);
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:31:46 +00:00
|
|
|
const rpl::variable<QStringList> &MessageLinksParser::list() const {
|
|
|
|
return _list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageLinksParser::parse() {
|
2018-05-26 14:58:21 +00:00
|
|
|
const auto &textWithTags = _field->getTextWithTags();
|
|
|
|
const auto &text = textWithTags.text;
|
|
|
|
const auto &tags = textWithTags.tags;
|
2018-05-31 18:28:37 +00:00
|
|
|
const auto &markdownTags = _field->getMarkdownTags();
|
2018-05-21 21:31:46 +00:00
|
|
|
if (text.isEmpty()) {
|
|
|
|
_list = QStringList();
|
2017-04-08 13:27:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-04 09:51:41 +00:00
|
|
|
const auto tagCanIntersectWithLink = [](const QString &tag) {
|
|
|
|
return (tag == Ui::InputField::kTagBold)
|
|
|
|
|| (tag == Ui::InputField::kTagItalic)
|
|
|
|
|| (tag == Ui::InputField::kTagUnderline)
|
|
|
|
|| (tag == Ui::InputField::kTagStrikeOut);
|
|
|
|
};
|
2018-05-21 21:31:46 +00:00
|
|
|
|
|
|
|
auto ranges = QVector<LinkRange>();
|
2018-05-26 14:58:21 +00:00
|
|
|
|
2018-05-31 18:28:37 +00:00
|
|
|
auto tag = tags.begin();
|
2018-05-26 14:58:21 +00:00
|
|
|
const auto tagsEnd = tags.end();
|
|
|
|
const auto processTag = [&] {
|
|
|
|
Expects(tag != tagsEnd);
|
|
|
|
|
|
|
|
if (Ui::InputField::IsValidMarkdownLink(tag->id)
|
2019-09-16 11:14:06 +00:00
|
|
|
&& !TextUtilities::IsMentionLink(tag->id)) {
|
2018-05-26 14:58:21 +00:00
|
|
|
ranges.push_back({ tag->offset, tag->length, tag->id });
|
|
|
|
}
|
|
|
|
++tag;
|
|
|
|
};
|
|
|
|
const auto processTagsBefore = [&](int offset) {
|
2019-07-04 09:51:41 +00:00
|
|
|
while (tag != tagsEnd
|
|
|
|
&& (tag->offset + tag->length <= offset
|
|
|
|
|| tagCanIntersectWithLink(tag->id))) {
|
2018-05-26 14:58:21 +00:00
|
|
|
processTag();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const auto hasTagsIntersection = [&](int till) {
|
|
|
|
if (tag == tagsEnd || tag->offset >= till) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (tag != tagsEnd && tag->offset < till) {
|
|
|
|
processTag();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2018-05-31 18:28:37 +00:00
|
|
|
auto markdownTag = markdownTags.begin();
|
|
|
|
const auto markdownTagsEnd = markdownTags.end();
|
|
|
|
const auto markdownTagsAllow = [&](int from, int length) {
|
|
|
|
while (markdownTag != markdownTagsEnd
|
2018-06-07 19:00:46 +00:00
|
|
|
&& (markdownTag->adjustedStart
|
|
|
|
+ markdownTag->adjustedLength <= from
|
2019-07-04 09:51:41 +00:00
|
|
|
|| !markdownTag->closed
|
|
|
|
|| tagCanIntersectWithLink(markdownTag->tag))) {
|
2018-05-31 18:28:37 +00:00
|
|
|
++markdownTag;
|
|
|
|
}
|
|
|
|
if (markdownTag == markdownTagsEnd
|
2018-06-07 19:00:46 +00:00
|
|
|
|| markdownTag->adjustedStart >= from + length) {
|
2018-05-31 18:28:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Ignore http-links that are completely inside some tags.
|
|
|
|
// This will allow sending http://test.com/__test__/test correctly.
|
2018-06-07 19:00:46 +00:00
|
|
|
return (markdownTag->adjustedStart > from)
|
|
|
|
|| (markdownTag->adjustedStart
|
|
|
|
+ markdownTag->adjustedLength < from + length);
|
2018-05-31 18:28:37 +00:00
|
|
|
};
|
|
|
|
|
2018-05-21 21:31:46 +00:00
|
|
|
const auto len = text.size();
|
|
|
|
const QChar *start = text.unicode(), *end = start + text.size();
|
|
|
|
for (auto offset = 0, matchOffset = offset; offset < len;) {
|
2018-08-08 20:11:28 +00:00
|
|
|
auto m = qthelp::RegExpDomain().match(text, matchOffset);
|
2018-05-21 21:31:46 +00:00
|
|
|
if (!m.hasMatch()) break;
|
|
|
|
|
|
|
|
auto domainOffset = m.capturedStart();
|
|
|
|
|
|
|
|
auto protocol = m.captured(1).toLower();
|
|
|
|
auto topDomain = m.captured(3).toLower();
|
|
|
|
auto isProtocolValid = protocol.isEmpty() || TextUtilities::IsValidProtocol(protocol);
|
|
|
|
auto isTopDomainValid = !protocol.isEmpty() || TextUtilities::IsValidTopDomain(topDomain);
|
|
|
|
|
|
|
|
if (protocol.isEmpty() && domainOffset > offset + 1 && *(start + domainOffset - 1) == QChar('@')) {
|
|
|
|
auto forMailName = text.mid(offset, domainOffset - offset - 1);
|
|
|
|
auto mMailName = TextUtilities::RegExpMailNameAtEnd().match(forMailName);
|
|
|
|
if (mMailName.hasMatch()) {
|
|
|
|
offset = matchOffset = m.capturedEnd();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isProtocolValid || !isTopDomainValid) {
|
|
|
|
offset = matchOffset = m.capturedEnd();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStack<const QChar*> parenth;
|
|
|
|
const QChar *domainEnd = start + m.capturedEnd(), *p = domainEnd;
|
|
|
|
for (; p < end; ++p) {
|
|
|
|
QChar ch(*p);
|
2020-06-04 06:53:59 +00:00
|
|
|
if (IsLinkEnd(ch)) {
|
|
|
|
break; // link finished
|
|
|
|
} else if (IsAlmostLinkEnd(ch)) {
|
2018-05-21 21:31:46 +00:00
|
|
|
const QChar *endTest = p + 1;
|
2020-06-04 06:53:59 +00:00
|
|
|
while (endTest < end && IsAlmostLinkEnd(*endTest)) {
|
2018-05-21 21:31:46 +00:00
|
|
|
++endTest;
|
|
|
|
}
|
2020-06-04 06:53:59 +00:00
|
|
|
if (endTest >= end || IsLinkEnd(*endTest)) {
|
2018-05-21 21:31:46 +00:00
|
|
|
break; // link finished at p
|
|
|
|
}
|
|
|
|
p = endTest;
|
|
|
|
ch = *p;
|
|
|
|
}
|
|
|
|
if (ch == '(' || ch == '[' || ch == '{' || ch == '<') {
|
|
|
|
parenth.push(p);
|
|
|
|
} else if (ch == ')' || ch == ']' || ch == '}' || ch == '>') {
|
|
|
|
if (parenth.isEmpty()) break;
|
|
|
|
const QChar *q = parenth.pop(), open(*q);
|
|
|
|
if ((ch == ')' && open != '(') || (ch == ']' && open != '[') || (ch == '}' && open != '{') || (ch == '>' && open != '<')) {
|
|
|
|
p = q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p > domainEnd) { // check, that domain ended
|
|
|
|
if (domainEnd->unicode() != '/' && domainEnd->unicode() != '?') {
|
|
|
|
matchOffset = domainEnd - start;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-05-26 14:58:21 +00:00
|
|
|
const auto range = LinkRange {
|
2021-10-19 13:00:21 +00:00
|
|
|
int(domainOffset),
|
2018-05-26 14:58:21 +00:00
|
|
|
static_cast<int>(p - start - domainOffset),
|
|
|
|
QString()
|
|
|
|
};
|
|
|
|
processTagsBefore(domainOffset);
|
|
|
|
if (!hasTagsIntersection(range.start + range.length)) {
|
2018-05-31 18:28:37 +00:00
|
|
|
if (markdownTagsAllow(range.start, range.length)) {
|
|
|
|
ranges.push_back(range);
|
|
|
|
}
|
2018-05-26 14:58:21 +00:00
|
|
|
}
|
2018-05-21 21:31:46 +00:00
|
|
|
offset = matchOffset = p - start;
|
|
|
|
}
|
2018-05-26 16:02:36 +00:00
|
|
|
processTagsBefore(QFIXED_MAX);
|
2018-05-21 21:31:46 +00:00
|
|
|
|
|
|
|
apply(text, ranges);
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:31:46 +00:00
|
|
|
void MessageLinksParser::apply(
|
|
|
|
const QString &text,
|
|
|
|
const QVector<LinkRange> &ranges) {
|
|
|
|
const auto count = int(ranges.size());
|
|
|
|
const auto current = _list.current();
|
2018-05-26 14:58:21 +00:00
|
|
|
const auto computeLink = [&](const LinkRange &range) {
|
|
|
|
return range.custom.isEmpty()
|
2021-10-19 13:00:21 +00:00
|
|
|
? base::StringViewMid(text, range.start, range.length)
|
|
|
|
: QStringView(range.custom);
|
2018-05-26 14:58:21 +00:00
|
|
|
};
|
2018-05-21 21:31:46 +00:00
|
|
|
const auto changed = [&] {
|
|
|
|
if (current.size() != count) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
2018-05-26 14:58:21 +00:00
|
|
|
if (computeLink(ranges[i]) != current[i]) {
|
2018-05-21 21:31:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}();
|
|
|
|
if (!changed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto parsed = QStringList();
|
|
|
|
parsed.reserve(count);
|
|
|
|
for (const auto &range : ranges) {
|
2018-05-26 14:58:21 +00:00
|
|
|
parsed.push_back(computeLink(range).toString());
|
2018-05-21 21:31:46 +00:00
|
|
|
}
|
|
|
|
_list = std::move(parsed);
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|