Rework hiding 'via @bot' option

Instead of using "hide_via" flag, it sends preview of inline query.
This commit is contained in:
Eric Kotato 2021-08-23 03:04:03 +03:00
parent 2975dec8b9
commit f620f7480b
14 changed files with 150 additions and 21 deletions

View file

@ -3120,6 +3120,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"ktg_mute_for_selected_time" = "For selected time";
"ktg_send_hide_via_message" = "Send without 'via @bot'";
"ktg_send_preview" = "Send preview";
"ktg_send_silent_preview" = "Send preview silent";
"ktg_reminder_preview" = "Remind with preview";
"ktg_schedule_preview" = "Send preview scheduled";
// Keys finished

View file

@ -223,6 +223,9 @@
"ktg_filters_hide_edit_toast": "Edit button is hidden.\nYou can enable it back in Kotatogram Settings.",
"ktg_settings_telegram_sites_autologin": "Auto-login on Telegram sites",
"ktg_mute_for_selected_time": "For selected time",
"ktg_send_hide_via_message": "Send without 'via @bot'",
"ktg_send_preview": "Send preview",
"ktg_send_silent_preview": "Send preview silent",
"ktg_reminder_preview": "Remind with preview",
"ktg_schedule_preview": "Send preview scheduled",
"dummy_last_string": ""
}

View file

@ -223,6 +223,9 @@
"ktg_filters_hide_edit_toast": "Кнопка изменения скрыта.\nВы можете включить её обратно в настройках Kotatogram.",
"ktg_settings_telegram_sites_autologin": "Авто-вход на сайты Telegram",
"ktg_mute_for_selected_time": "На указанное время",
"ktg_send_hide_via_message": "Отправить без \"через @bot\"",
"ktg_send_preview": "Отправить превью",
"ktg_send_silent_preview": "Отправить превью тихо",
"ktg_reminder_preview": "Напомнить с помощью превью",
"ktg_schedule_preview": "Отправить превью позже",
"dummy_last_string": ""
}

View file

@ -184,6 +184,26 @@ void SendExistingMedia(
} // namespace
void SendWebDocument(
Api::MessageToSend &&message,
not_null<DocumentData*> document,
Fn<void()> doneCallback,
bool forwarding) {
const auto inputMedia = [=] {
return MTP_inputMediaDocumentExternal(
MTP_flags(0),
MTP_string(document->url()),
MTPint()); // ttl_seconds
};
SendExistingMedia(
std::move(message),
document,
inputMedia,
document->stickerOrGifOrigin(),
(doneCallback ? std::move(doneCallback) : nullptr),
forwarding);
}
void SendExistingDocument(
Api::MessageToSend &&message,
not_null<DocumentData*> document,

View file

@ -25,6 +25,12 @@ namespace Api {
struct MessageToSend;
struct SendAction;
void SendWebDocument(
Api::MessageToSend &&message,
not_null<DocumentData*> document,
Fn<void()> doneCallback = nullptr,
bool forwarding = false);
void SendExistingDocument(
Api::MessageToSend &&message,
not_null<DocumentData*> document,

View file

@ -65,6 +65,36 @@ FillMenuResult FillSendMenu(
return FillMenuResult::Success;
}
FillMenuResult FillSendPreviewMenu(
not_null<Ui::PopupMenu*> menu,
Type type,
Fn<void()> defaultSend,
Fn<void()> silent,
Fn<void()> schedule) {
if (!defaultSend && !silent && !schedule) {
return FillMenuResult::None;
}
const auto now = type;
if (now == Type::Disabled) {
return FillMenuResult::None;
}
if (defaultSend && now != Type::Scheduled && now != Type::ScheduledToUser) {
menu->addAction(tr::ktg_send_preview(tr::now), defaultSend);
}
if (silent && now != Type::Reminder) {
menu->addAction(tr::ktg_send_silent_preview(tr::now), silent);
}
if (schedule && now != Type::SilentOnly) {
menu->addAction(
(now == Type::Reminder
? tr::ktg_reminder_preview(tr::now)
: tr::ktg_schedule_preview(tr::now)),
schedule);
}
return FillMenuResult::Success;
}
void SetupMenuAndShortcuts(
not_null<Ui::RpWidget*> button,
Fn<Type()> type,

View file

@ -44,6 +44,13 @@ FillMenuResult FillSendMenu(
Fn<void()> silent,
Fn<void()> schedule);
FillMenuResult FillSendPreviewMenu(
not_null<Ui::PopupMenu*> menu,
Type type,
Fn<void()> defaultSend,
Fn<void()> silent,
Fn<void()> schedule);
void SetupMenuAndShortcuts(
not_null<Ui::RpWidget*> button,
Fn<Type()> type,

View file

@ -1225,6 +1225,10 @@ void DocumentData::refreshFileReference(const QByteArray &value) {
_videoThumbnail.location.refreshFileReference(value);
}
QString DocumentData::url() const {
return _url;
}
QString DocumentData::filename() const {
return _filename;
}

View file

@ -220,6 +220,7 @@ public:
// to (this) received from the server "same" document.
void collectLocalData(not_null<DocumentData*> local);
[[nodiscard]] QString url() const;
[[nodiscard]] QString filename() const;
[[nodiscard]] QString mimeString() const;
[[nodiscard]] bool hasMimeType(QLatin1String mime) const;

View file

@ -1037,7 +1037,20 @@ void HistoryWidget::initTabbedSelector() {
selector->inlineResultChosen(
) | filter | rpl::start_with_next([=](Selector::InlineChosen data) {
sendInlineResult(data);
if (data.sendPreview) {
const auto request = data.result->openRequest();
if (const auto photo = request.photo()) {
sendExistingPhoto(photo, data.options);
} else if (const auto document = request.document()) {
sendExistingDocument(document, data.options);
}
addRecentBot(data.bot);
clearFieldText();
saveCloudDraft();
} else {
sendInlineResult(data);
}
}, lifetime());
selector->contextMenuRequested(
@ -1416,6 +1429,17 @@ void HistoryWidget::applyInlineBotQuery(UserData *bot, const QString &query) {
} else if (const auto document = request.document()) {
controller()->openDocument(document, FullMsgId());
}
} else if (result.sendPreview) {
const auto request = result.result->openRequest();
if (const auto photo = request.photo()) {
sendExistingPhoto(photo, result.options);
} else if (const auto document = request.document()) {
sendExistingDocument(document, result.options);
}
addRecentBot(result.bot);
clearFieldText();
saveCloudDraft();
} else {
sendInlineResult(result);
}
@ -5785,17 +5809,7 @@ void HistoryWidget::sendInlineResult(InlineBots::ResultSelected result) {
_saveDraftStart = crl::now();
saveDraft();
auto &bots = cRefRecentInlineBots();
const auto index = bots.indexOf(result.bot);
if (index) {
if (index > 0) {
bots.removeAt(index);
} else if (bots.size() >= RecentInlineBotsLimit) {
bots.resize(RecentInlineBotsLimit - 1);
}
bots.push_front(result.bot);
session().local().writeRecentHashtagsAndBots();
}
addRecentBot(result.bot);
hideSelectorControlsAnimated();
@ -6171,7 +6185,12 @@ bool HistoryWidget::sendExistingDocument(
auto message = Api::MessageToSend(_history);
message.action.options = std::move(options);
message.action.replyTo = replyToId();
Api::SendExistingDocument(std::move(message), document);
if (document->hasRemoteLocation()) {
Api::SendExistingDocument(std::move(message), document);
} else {
Api::SendWebDocument(std::move(message), document);
}
if (_fieldAutocomplete->stickersShown()) {
clearFieldText();
@ -6933,6 +6952,20 @@ void HistoryWidget::messageDataReceived(ChannelData *channel, MsgId msgId) {
}
}
void HistoryWidget::addRecentBot(not_null<UserData*> bot) {
auto &bots = cRefRecentInlineBots();
const auto index = bots.indexOf(bot);
if (index) {
if (index > 0) {
bots.removeAt(index);
} else if (bots.size() >= RecentInlineBotsLimit) {
bots.resize(RecentInlineBotsLimit - 1);
}
bots.push_front(bot);
session().local().writeRecentHashtagsAndBots();
}
}
void HistoryWidget::updateReplyEditText(not_null<HistoryItem*> item) {
_replyEditMsgText.setText(
st::messageTextStyle,

View file

@ -378,6 +378,8 @@ private:
void requestMessageData(MsgId msgId);
void messageDataReceived(ChannelData *channel, MsgId msgId);
void addRecentBot(not_null<UserData*> bot);
void send(Api::SendOptions options);
void sendWithModifiers(Qt::KeyboardModifiers modifiers);
void sendSilent();

View file

@ -134,6 +134,7 @@ struct ResultSelected {
Api::SendOptions options;
// Open in OverlayWidget;
bool open = false;
bool sendPreview = false;
};
} // namespace InlineBots

View file

@ -258,7 +258,8 @@ void Inner::selectInlineResult(
int row,
int column,
Api::SendOptions options,
bool open) {
bool open,
bool sendPreview) {
if (row >= _rows.size() || column >= _rows.at(row).items.size()) {
return;
}
@ -271,6 +272,7 @@ void Inner::selectInlineResult(
.bot = _inlineBot,
.options = std::move(options),
.open = open,
.sendPreview = sendPreview,
});
}
}
@ -316,16 +318,29 @@ void Inner::contextMenuEvent(QContextMenuEvent *e) {
SendMenu::DefaultSilentCallback(send),
SendMenu::DefaultScheduleCallback(this, type, send));
_menu->addAction(tr::ktg_send_hide_via_message(tr::now), [=] {
send({ .hideVia = true });
});
const auto hideViaActions = [&] {
const auto sendPreview = [=](Api::SendOptions options) {
selectInlineResult(row, column, options, false, true);
};
SendMenu::FillSendPreviewMenu(
_menu,
type,
[=] { sendPreview({}); },
SendMenu::DefaultSilentCallback(sendPreview),
SendMenu::DefaultScheduleCallback(this, type, sendPreview));
};
auto item = _rows[row].items[column];
if (const auto previewDocument = item->getPreviewDocument()) {
hideViaActions();
auto callback = [&](const QString &text, Fn<void()> &&done) {
_menu->addAction(text, std::move(done));
};
ChatHelpers::AddGifAction(std::move(callback), previewDocument);
} else if (const auto previewPhoto = item->getPreviewPhoto()) {
hideViaActions();
}
if (!_menu->empty()) {

View file

@ -152,7 +152,8 @@ private:
int row,
int column,
Api::SendOptions options,
bool open);
bool open,
bool sendPreview = false);
not_null<Window::SessionController*> _controller;
const std::unique_ptr<Ui::PathShiftGradient> _pathGradient;