Moved out sending method of many messages to many peers to single place.

This commit is contained in:
23rd 2022-12-02 14:07:12 +03:00
parent 037f2bcce2
commit bd8e7fdddd
2 changed files with 86 additions and 79 deletions

View File

@ -210,8 +210,6 @@ ShareBox::ShareBox(QWidget*, Descriptor &&descriptor)
}
void ShareBox::prepareCommentField() {
using namespace rpl::mappers;
_comment->hide(anim::type::instant);
rpl::combine(
@ -476,7 +474,7 @@ void ShareBox::showMenu(not_null<Ui::RpWidget*> parent) {
auto item = base::make_unique_q<Menu::ItemWithCheck>(
_menu->menu(),
st::popupMenuWithIcons.menu,
new QAction(QString(), _menu->menu()),
Ui::CreateChild<QAction>(_menu->menu().get()),
nullptr,
nullptr);
std::move(
@ -1343,75 +1341,23 @@ QString AppendShareGameScoreUrl(
return url + shareComponent;
}
void FastShareMessage(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item) {
struct ShareData {
ShareData(not_null<PeerData*> peer, MessageIdsList &&ids)
: peer(peer)
, msgIds(std::move(ids)) {
}
not_null<PeerData*> peer;
MessageIdsList msgIds;
ShareBox::SubmitCallback ShareBox::DefaultForwardCallback(
std::shared_ptr<Ui::Show> show,
not_null<History*> history,
MessageIdsList msgIds) {
struct State final {
base::flat_set<mtpRequestId> requests;
};
const auto show = std::make_shared<Window::Show>(controller);
const auto history = item->history();
const auto owner = &history->owner();
const auto session = &history->session();
const auto data = std::make_shared<ShareData>(
history->peer,
owner->itemOrItsGroup(item));
const auto isGame = item->getMessageBot()
&& item->media()
&& (item->media()->game() != nullptr);
const auto canCopyLink = item->hasDirectLink() || isGame;
const auto items = owner->idsToItems(data->msgIds);
const auto hasCaptions = ranges::any_of(items, [](auto item) {
return item->media()
&& !item->originalText().text.isEmpty()
&& item->media()->allowsEditCaption();
});
const auto hasOnlyForcedForwardedInfo = hasCaptions
? false
: ranges::all_of(items, [](auto item) {
return item->media() && item->media()->forceForwardedInfo();
});
auto copyCallback = [=, toastParent = show->toastParent()] {
const auto item = owner->message(data->msgIds[0]);
if (!item) {
return;
}
if (item->hasDirectLink()) {
using namespace HistoryView;
CopyPostLink(controller, item->fullId(), Context::History);
} else if (const auto bot = item->getMessageBot()) {
if (const auto media = item->media()) {
if (const auto game = media->game()) {
const auto link = session->createInternalLinkFull(
bot->username() + u"?game="_q + game->shortName);
QGuiApplication::clipboard()->setText(link);
Ui::Toast::Show(
toastParent,
tr::lng_share_game_link_copied(tr::now));
}
}
}
};
auto submitCallback = [=](
const auto state = std::make_shared<State>();
return [=](
std::vector<not_null<Data::Thread*>> &&result,
TextWithTags &&comment,
Api::SendOptions options,
Data::ForwardOptions forwardOptions) {
if (!data->requests.empty()) {
if (!state->requests.empty()) {
return; // Share clicked already.
}
auto items = history->owner().idsToItems(data->msgIds);
auto items = history->owner().idsToItems(msgIds);
if (items.empty() || result.empty()) {
return;
}
@ -1451,20 +1397,20 @@ void FastShareMessage(
| ((forwardOptions == Data::ForwardOptions::NoNamesAndCaptions)
? Flag::f_drop_media_captions
: Flag(0));
auto msgIds = QVector<MTPint>();
msgIds.reserve(data->msgIds.size());
for (const auto &fullId : data->msgIds) {
msgIds.push_back(MTP_int(fullId.msg));
auto mtpMsgIds = QVector<MTPint>();
mtpMsgIds.reserve(msgIds.size());
for (const auto &fullId : msgIds) {
mtpMsgIds.push_back(MTP_int(fullId.msg));
}
const auto generateRandom = [&] {
auto result = QVector<MTPlong>(data->msgIds.size());
auto result = QVector<MTPlong>(msgIds.size());
for (auto &value : result) {
value = base::RandomValue<MTPlong>();
}
return result;
};
auto &api = owner->session().api();
auto &histories = owner->histories();
auto &api = history->owner().session().api();
auto &histories = history->owner().histories();
const auto requestType = Data::Histories::RequestType::Send;
for (const auto thread : result) {
if (!comment.text.isEmpty()) {
@ -1486,8 +1432,8 @@ void FastShareMessage(
history->sendRequestId = api.request(
MTPmessages_ForwardMessages(
MTP_flags(sendFlags),
data->peer->input,
MTP_vector<MTPint>(msgIds),
history->peer->input,
MTP_vector<MTPint>(mtpMsgIds),
MTP_vector<MTPlong>(generateRandom()),
peer->input,
MTP_int(topicRootId),
@ -1495,8 +1441,8 @@ void FastShareMessage(
MTP_inputPeerEmpty() // send_as
)).done([=](const MTPUpdates &updates, mtpRequestId reqId) {
history->session().api().applyUpdates(updates);
data->requests.remove(reqId);
if (data->requests.empty()) {
state->requests.remove(reqId);
if (state->requests.empty()) {
if (show->valid()) {
Ui::Toast::Show(
show->toastParent(),
@ -1520,9 +1466,60 @@ void FastShareMessage(
}).afterRequest(history->sendRequestId).send();
return history->sendRequestId;
});
data->requests.insert(history->sendRequestId);
state->requests.insert(history->sendRequestId);
}
};
}
void FastShareMessage(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item) {
const auto show = std::make_shared<Window::Show>(controller);
const auto history = item->history();
const auto owner = &history->owner();
const auto session = &history->session();
const auto msgIds = owner->itemOrItsGroup(item);
const auto isGame = item->getMessageBot()
&& item->media()
&& (item->media()->game() != nullptr);
const auto canCopyLink = item->hasDirectLink() || isGame;
const auto items = owner->idsToItems(msgIds);
const auto hasCaptions = ranges::any_of(items, [](auto item) {
return item->media()
&& !item->originalText().text.isEmpty()
&& item->media()->allowsEditCaption();
});
const auto hasOnlyForcedForwardedInfo = hasCaptions
? false
: ranges::all_of(items, [](auto item) {
return item->media() && item->media()->forceForwardedInfo();
});
auto copyCallback = [=, toastParent = show->toastParent()] {
const auto item = owner->message(msgIds[0]);
if (!item) {
return;
}
if (item->hasDirectLink()) {
using namespace HistoryView;
CopyPostLink(controller, item->fullId(), Context::History);
} else if (const auto bot = item->getMessageBot()) {
if (const auto media = item->media()) {
if (const auto game = media->game()) {
const auto link = session->createInternalLinkFull(
bot->username() + u"?game="_q + game->shortName);
QGuiApplication::clipboard()->setText(link);
Ui::Toast::Show(
toastParent,
tr::lng_share_game_link_copied(tr::now));
}
}
}
};
auto filterCallback = [isGame](not_null<Data::Thread*> thread) {
return thread->canWrite()
&& (!isGame || !thread->peer()->isBroadcast());
@ -1534,10 +1531,13 @@ void FastShareMessage(
Box<ShareBox>(ShareBox::Descriptor{
.session = session,
.copyCallback = std::move(copyLinkCallback),
.submitCallback = std::move(submitCallback),
.submitCallback = ShareBox::DefaultForwardCallback(
show,
history,
msgIds),
.filterCallback = std::move(filterCallback),
.forwardOptions = {
.messagesCount = int(data->msgIds.size()),
.messagesCount = int(msgIds.size()),
.show = !hasOnlyForcedForwardedInfo,
.hasCaptions = hasCaptions,
},

View File

@ -15,6 +15,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/effects/round_checkbox.h"
#include "mtproto/sender.h"
class History;
namespace style {
struct MultiSelect;
struct InputField;
@ -74,9 +76,14 @@ public:
std::vector<not_null<Data::Thread*>>&&,
TextWithTags&&,
Api::SendOptions,
Data::ForwardOptions option)>;
Data::ForwardOptions)>;
using FilterCallback = Fn<bool(not_null<Data::Thread*>)>;
[[nodiscard]] static SubmitCallback DefaultForwardCallback(
std::shared_ptr<Ui::Show> show,
not_null<History*> history,
MessageIdsList msgIds);
struct Descriptor {
not_null<Main::Session*> session;
CopyCallback copyCallback;