Count senders / captions count correctly in forwards.

This commit is contained in:
John Preston 2024-01-18 13:26:14 +04:00
parent 95d4e02ae1
commit 5f97d683df
8 changed files with 65 additions and 41 deletions

View file

@ -495,7 +495,6 @@ void ShareBox::showMenu(not_null<Ui::RpWidget*> parent) {
};
Ui::FillForwardOptions(
std::move(createView),
_descriptor.forwardOptions.messagesCount,
_forwardOptions,
[=](Ui::ForwardOptions value) { _forwardOptions = value; },
_menu->lifetime());
@ -522,7 +521,10 @@ void ShareBox::createButtons() {
const auto send = addButton(tr::lng_share_confirm(), [=] {
submit({});
});
_forwardOptions.hasCaptions = _descriptor.forwardOptions.hasCaptions;
_forwardOptions.sendersCount
= _descriptor.forwardOptions.sendersCount;
_forwardOptions.captionsCount
= _descriptor.forwardOptions.captionsCount;
send->setAcceptBoth();
send->clicks(
@ -575,7 +577,7 @@ void ShareBox::innerSelectedChanged(
void ShareBox::submit(Api::SendOptions options) {
if (const auto onstack = _descriptor.submitCallback) {
const auto forwardOptions = (_forwardOptions.hasCaptions
const auto forwardOptions = (_forwardOptions.captionsCount
&& _forwardOptions.dropCaptions)
? Data::ForwardOptions::NoNamesAndCaptions
: _forwardOptions.dropNames
@ -1659,9 +1661,9 @@ void FastShareMessage(
msgIds),
.filterCallback = std::move(filterCallback),
.forwardOptions = {
.messagesCount = int(msgIds.size()),
.sendersCount = ItemsForwardSendersCount(items),
.captionsCount = ItemsForwardCaptionsCount(items),
.show = !hasOnlyForcedForwardedInfo,
.hasCaptions = hasCaptions,
},
.premiumRequiredError = SharePremiumRequiredError(),
}),

View file

@ -100,9 +100,9 @@ public:
const style::PeerList *st = nullptr;
const style::InputField *stLabel = nullptr;
struct {
int messagesCount = 0;
int sendersCount = 0;
int captionsCount = 0;
bool show = false;
bool hasCaptions = false;
} forwardOptions;
HistoryView::ScheduleBoxStyleArgs scheduleBoxStyle;

View file

@ -808,3 +808,29 @@ void ClearMediaAsExpired(not_null<HistoryItem*> item) {
}
}
}
int ItemsForwardSendersCount(const HistoryItemsList &list) {
auto peers = base::flat_set<not_null<PeerData*>>();
auto names = base::flat_set<QString>();
for (const auto &item : list) {
if (const auto peer = item->originalSender()) {
peers.emplace(peer);
} else {
names.emplace(item->originalHiddenSenderInfo()->name);
}
}
return int(peers.size()) + int(names.size());
}
int ItemsForwardCaptionsCount(const HistoryItemsList &list) {
auto result = 0;
for (const auto &item : list) {
if (const auto media = item->media()) {
if (!item->originalText().text.isEmpty()
&& media->allowsEditCaption()) {
++result;
}
}
}
return result;
}

View file

@ -158,3 +158,6 @@ ClickHandlerPtr JumpToStoryClickHandler(
void ShowTrialTranscribesToast(int left, TimeId until);
void ClearMediaAsExpired(not_null<HistoryItem*> item);
[[nodiscard]] int ItemsForwardSendersCount(const HistoryItemsList &list);
[[nodiscard]] int ItemsForwardCaptionsCount(const HistoryItemsList &list);

View file

@ -44,18 +44,6 @@ constexpr auto kUnknownVersion = -1;
constexpr auto kNameWithCaptionsVersion = -2;
constexpr auto kNameNoCaptionsVersion = -3;
[[nodiscard]] bool HasCaptions(const HistoryItemsList &list) {
for (const auto &item : list) {
if (const auto media = item->media()) {
if (!item->originalText().text.isEmpty()
&& media->allowsEditCaption()) {
return true;
}
}
}
return false;
}
[[nodiscard]] bool HasOnlyForcedForwardedInfo(const HistoryItemsList &list) {
for (const auto &item : list) {
if (const auto media = item->media()) {
@ -257,10 +245,10 @@ void ForwardPanel::editOptions(std::shared_ptr<ChatHelpers::Show> show) {
const auto now = _data.options;
const auto count = _data.items.size();
const auto dropNames = (now != Options::PreserveInfo);
const auto hasCaptions = HasCaptions(_data.items);
const auto hasOnlyForcedForwardedInfo = hasCaptions
? false
: HasOnlyForcedForwardedInfo(_data.items);
const auto sendersCount = ItemsForwardSendersCount(_data.items);
const auto captionsCount = ItemsForwardCaptionsCount(_data.items);
const auto hasOnlyForcedForwardedInfo = !captionsCount
&& HasOnlyForcedForwardedInfo(_data.items);
const auto dropCaptions = (now == Options::NoNamesAndCaptions);
const auto weak = base::make_weak(this);
const auto changeRecipient = crl::guard(this, [=] {
@ -283,7 +271,7 @@ void ForwardPanel::editOptions(std::shared_ptr<ChatHelpers::Show> show) {
if (_data.items.empty()) {
return;
}
const auto newOptions = (options.hasCaptions
const auto newOptions = (options.captionsCount
&& options.dropCaptions)
? Options::NoNamesAndCaptions
: options.dropNames
@ -302,8 +290,9 @@ void ForwardPanel::editOptions(std::shared_ptr<ChatHelpers::Show> show) {
Ui::ForwardOptionsBox,
count,
Ui::ForwardOptions{
.sendersCount = sendersCount,
.captionsCount = captionsCount,
.dropNames = dropNames,
.hasCaptions = hasCaptions,
.dropCaptions = dropCaptions,
},
optionsChanged,
@ -312,10 +301,9 @@ void ForwardPanel::editOptions(std::shared_ptr<ChatHelpers::Show> show) {
void ForwardPanel::editToNextOption() {
using Options = Data::ForwardOptions;
const auto hasCaptions = HasCaptions(_data.items);
const auto hasOnlyForcedForwardedInfo = hasCaptions
? false
: HasOnlyForcedForwardedInfo(_data.items);
const auto captionsCount = ItemsForwardCaptionsCount(_data.items);
const auto hasOnlyForcedForwardedInfo = !captionsCount
&& HasOnlyForcedForwardedInfo(_data.items);
if (hasOnlyForcedForwardedInfo) {
return;
}
@ -323,7 +311,7 @@ void ForwardPanel::editToNextOption() {
const auto now = _data.options;
const auto next = (now == Options::PreserveInfo)
? Options::NoSenderNames
: ((now == Options::NoSenderNames) && hasCaptions)
: ((now == Options::NoSenderNames) && captionsCount)
? Options::NoNamesAndCaptions
: Options::PreserveInfo;

View file

@ -19,20 +19,19 @@ void FillForwardOptions(
Fn<not_null<AbstractCheckView*>(
rpl::producer<QString> &&,
bool)> createView,
int count,
ForwardOptions options,
Fn<void(ForwardOptions)> optionsChanged,
rpl::lifetime &lifetime) {
Expects(optionsChanged != nullptr);
const auto names = createView(
(count == 1
(options.sendersCount == 1
? tr::lng_forward_show_sender
: tr::lng_forward_show_senders)(),
!options.dropNames);
const auto captions = options.hasCaptions
const auto captions = options.captionsCount
? createView(
(count == 1
(options.captionsCount == 1
? tr::lng_forward_show_caption
: tr::lng_forward_show_captions)(),
!options.dropCaptions).get()
@ -40,8 +39,9 @@ void FillForwardOptions(
const auto notify = [=] {
optionsChanged({
.sendersCount = options.sendersCount,
.captionsCount = options.captionsCount,
.dropNames = !names->checked(),
.hasCaptions = options.hasCaptions,
.dropCaptions = (captions && !captions->checked()),
});
};
@ -118,7 +118,6 @@ void ForwardOptionsBox(
};
FillForwardOptions(
std::move(createView),
count,
options,
std::move(optionsChanged),
box->lifetime());

View file

@ -14,8 +14,9 @@ namespace Ui {
class AbstractCheckView;
struct ForwardOptions {
int sendersCount = 0;
int captionsCount = 0;
bool dropNames = false;
bool hasCaptions = false;
bool dropCaptions = false;
};
@ -23,7 +24,6 @@ void FillForwardOptions(
Fn<not_null<AbstractCheckView*>(
rpl::producer<QString> &&,
bool)> createView,
int count,
ForwardOptions options,
Fn<void(ForwardOptions)> optionsChanged,
rpl::lifetime &lifetime);

View file

@ -1761,7 +1761,10 @@ QPointer<Ui::BoxContent> ShowForwardMessagesBox(
Fn<void()> &&successCallback) {
const auto session = &show->session();
const auto owner = &session->data();
const auto msgIds = owner->itemsToIds(owner->idsToItems(draft.ids));
const auto itemsList = owner->idsToItems(draft.ids);
const auto msgIds = owner->itemsToIds(itemsList);
const auto sendersCount = ItemsForwardSendersCount(itemsList);
const auto captionsCount = ItemsForwardCaptionsCount(itemsList);
if (msgIds.empty()) {
return nullptr;
}
@ -1779,7 +1782,7 @@ QPointer<Ui::BoxContent> ShowForwardMessagesBox(
}
[[nodiscard]] Data::ForwardOptions forwardOptionsData() const {
return (_forwardOptions.hasCaptions
return (_forwardOptions.captionsCount
&& _forwardOptions.dropCaptions)
? Data::ForwardOptions::NoNamesAndCaptions
: _forwardOptions.dropNames
@ -1868,6 +1871,10 @@ QPointer<Ui::BoxContent> ShowForwardMessagesBox(
const auto controllerRaw = controller.get();
auto box = Box<ListBox>(std::move(controller), nullptr);
const auto boxRaw = box.data();
boxRaw->setForwardOptions({
.sendersCount = sendersCount,
.captionsCount = captionsCount,
});
show->showBox(std::move(box));
auto state = State{ boxRaw, controllerRaw };
return boxRaw->lifetime().make_state<State>(std::move(state));
@ -1988,7 +1995,6 @@ QPointer<Ui::BoxContent> ShowForwardMessagesBox(
};
Ui::FillForwardOptions(
std::move(createView),
msgIds.size(),
state->box->forwardOptions(),
[=](Ui::ForwardOptions o) {
state->box->setForwardOptions(o);