Added ability to schedule and send silently media from inline bots.

This commit is contained in:
23rd 2020-08-07 19:43:00 +03:00 committed by John Preston
parent d1d153e886
commit 1fee7d1a41
3 changed files with 65 additions and 7 deletions

View File

@ -1199,8 +1199,9 @@ void HistoryWidget::applyInlineBotQuery(UserData *bot, const QString &query) {
_inlineResults.create(this, controller());
_inlineResults->setResultSelectedCallback([=](
InlineBots::Result *result,
UserData *bot) {
sendInlineResult(result, bot, Api::SendOptions());
UserData *bot,
Api::SendOptions options) {
sendInlineResult(result, bot, options);
});
_inlineResults->requesting(
) | rpl::start_with_next([=](bool requesting) {

View File

@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "inline_bots/inline_results_widget.h"
#include "api/api_common.h"
#include "chat_helpers/message_field.h" // FillSendMenu
#include "data/data_photo.h"
#include "data/data_document.h"
#include "data/data_channel.h"
@ -15,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_file_origin.h"
#include "data/data_changes.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/popup_menu.h"
#include "ui/widgets/shadow.h"
#include "ui/effects/ripple_animation.h"
#include "ui/image/image_prepare.h"
@ -31,6 +34,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/widgets/scroll_area.h"
#include "ui/widgets/labels.h"
#include "history/view/history_view_cursor_state.h"
#include "history/view/history_view_schedule_box.h"
#include "facades.h"
#include "app.h"
#include "styles/style_chat_helpers.h"
@ -254,14 +258,24 @@ void Inner::mouseReleaseEvent(QMouseEvent *e) {
}
void Inner::selectInlineResult(int row, int column) {
selectInlineResult(row, column, Api::SendOptions());
}
void Inner::selectInlineResult(
int row,
int column,
Api::SendOptions options) {
if (row >= _rows.size() || column >= _rows.at(row).items.size()) {
return;
}
auto item = _rows[row].items[column];
if (auto inlineResult = item->getResult()) {
if (const auto inlineResult = item->getResult()) {
if (inlineResult->onChoose(item)) {
_resultSelectedCallback(inlineResult, _inlineBot);
_resultSelectedCallback(
inlineResult,
_inlineBot,
std::move(options));
}
}
}
@ -285,6 +299,39 @@ void Inner::enterFromChildEvent(QEvent *e, QWidget *child) {
updateSelected();
}
void Inner::contextMenuEvent(QContextMenuEvent *e) {
if (_selected < 0 || _pressed >= 0) {
return;
}
const auto row = _selected / MatrixRowShift;
const auto column = _selected % MatrixRowShift;
const auto type = SendMenuType::Scheduled;
_menu = base::make_unique_q<Ui::PopupMenu>(this);
const auto send = [=](Api::SendOptions options) {
selectInlineResult(row, column, options);
};
const auto silent = [=] { send({ .silent = true }); };
const auto schedule = [=] {
Ui::show(
HistoryView::PrepareScheduleBox(
this,
type,
[=](Api::SendOptions options) { send(options); }),
Ui::LayerOption::KeepOther);
};
FillSendMenu(
_menu,
[&] { return type; },
silent,
schedule);
if (!_menu->actions().empty()) {
_menu->popup(QCursor::pos());
}
}
void Inner::clearSelection() {
if (_selected >= 0) {
int srow = _selected / MatrixRowShift, scol = _selected % MatrixRowShift;

View File

@ -18,6 +18,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <QtCore/QTimer>
namespace Api {
struct SendOptions;
} // namespace Api
namespace Ui {
class ScrollArea;
class IconButton;
@ -25,6 +29,7 @@ class LinkButton;
class RoundButton;
class FlatLabel;
class RippleAnimation;
class PopupMenu;
} // namespace Ui
namespace Window {
@ -44,6 +49,7 @@ namespace internal {
constexpr int kInlineItemsMaxPerRow = 5;
using Results = std::vector<std::unique_ptr<Result>>;
using ResultSelected = Fn<void(Result *, UserData *, Api::SendOptions)>;
struct CacheEntry {
QString nextOffset;
@ -79,7 +85,7 @@ public:
int countHeight();
void setResultSelectedCallback(Fn<void(Result *result, UserData *bot)> callback) {
void setResultSelectedCallback(ResultSelected callback) {
_resultSelectedCallback = std::move(callback);
}
@ -102,6 +108,7 @@ protected:
void leaveEventHook(QEvent *e) override;
void leaveToChildEvent(QEvent *e, QWidget *child) override;
void enterFromChildEvent(QEvent *e, QWidget *child) override;
void contextMenuEvent(QContextMenuEvent *e) override;
private slots:
void onSwitchPm();
@ -140,6 +147,7 @@ private:
int validateExistingInlineRows(const Results &results);
void selectInlineResult(int row, int column);
void selectInlineResult(int row, int column, Api::SendOptions options);
not_null<Window::SessionController*> _controller;
@ -157,6 +165,8 @@ private:
object_ptr<Ui::FlatLabel> _restrictedLabel = { nullptr };
base::unique_qptr<Ui::PopupMenu> _menu;
QVector<Row> _rows;
std::map<Result*, std::unique_ptr<ItemBase>> _inlineLayouts;
@ -168,7 +178,7 @@ private:
base::Timer _previewTimer;
bool _previewShown = false;
Fn<void(Result *result, UserData *bot)> _resultSelectedCallback;
ResultSelected _resultSelectedCallback;
};
@ -195,7 +205,7 @@ public:
void showAnimated();
void hideAnimated();
void setResultSelectedCallback(Fn<void(Result *result, UserData *bot)> callback) {
void setResultSelectedCallback(internal::ResultSelected callback) {
_inner->setResultSelectedCallback(std::move(callback));
}