mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Simplify paid stars check.
This commit is contained in:
parent
fe9bac096b
commit
c38982d286
7 changed files with 107 additions and 82 deletions
|
@ -634,6 +634,7 @@ void SendConfirmedFile(
|
||||||
.replyTo = file->to.replyTo,
|
.replyTo = file->to.replyTo,
|
||||||
.date = NewMessageDate(file->to.options),
|
.date = NewMessageDate(file->to.options),
|
||||||
.shortcutId = file->to.options.shortcutId,
|
.shortcutId = file->to.options.shortcutId,
|
||||||
|
.starsPaid = file->to.options.starsApproved,
|
||||||
.postAuthor = NewMessagePostAuthor(action),
|
.postAuthor = NewMessagePostAuthor(action),
|
||||||
.groupedId = groupId,
|
.groupedId = groupId,
|
||||||
.effectId = file->to.options.effectId,
|
.effectId = file->to.options.effectId,
|
||||||
|
|
|
@ -3532,6 +3532,7 @@ void ApiWrap::sendSharedContact(
|
||||||
.replyTo = action.replyTo,
|
.replyTo = action.replyTo,
|
||||||
.date = NewMessageDate(action.options),
|
.date = NewMessageDate(action.options),
|
||||||
.shortcutId = action.options.shortcutId,
|
.shortcutId = action.options.shortcutId,
|
||||||
|
.starsPaid = action.options.starsApproved,
|
||||||
.postAuthor = NewMessagePostAuthor(action),
|
.postAuthor = NewMessagePostAuthor(action),
|
||||||
.effectId = action.options.effectId,
|
.effectId = action.options.effectId,
|
||||||
}, TextWithEntities(), MTP_messageMediaContact(
|
}, TextWithEntities(), MTP_messageMediaContact(
|
||||||
|
|
|
@ -189,26 +189,17 @@ struct SendError {
|
||||||
|
|
||||||
struct Args {
|
struct Args {
|
||||||
QString text;
|
QString text;
|
||||||
int paidStars = 0;
|
|
||||||
int paidMessages = 0;
|
|
||||||
int boostsToLift = 0;
|
int boostsToLift = 0;
|
||||||
bool resolving = false;
|
|
||||||
bool premiumToLift = false;
|
bool premiumToLift = false;
|
||||||
};
|
};
|
||||||
SendError(Args &&args)
|
SendError(Args &&args)
|
||||||
: text(std::move(args.text))
|
: text(std::move(args.text))
|
||||||
, paidStars(args.paidStars)
|
|
||||||
, paidMessages(args.paidMessages)
|
|
||||||
, boostsToLift(args.boostsToLift)
|
, boostsToLift(args.boostsToLift)
|
||||||
, resolving(args.resolving)
|
|
||||||
, premiumToLift(args.premiumToLift) {
|
, premiumToLift(args.premiumToLift) {
|
||||||
}
|
}
|
||||||
|
|
||||||
QString text;
|
QString text;
|
||||||
int paidStars = 0;
|
|
||||||
int paidMessages = 0;
|
|
||||||
int boostsToLift = 0;
|
int boostsToLift = 0;
|
||||||
bool resolving = false;
|
|
||||||
bool premiumToLift = false;
|
bool premiumToLift = false;
|
||||||
|
|
||||||
[[nodiscard]] SendError value_or(SendError other) const {
|
[[nodiscard]] SendError value_or(SendError other) const {
|
||||||
|
|
|
@ -60,6 +60,33 @@ bool PeerCallKnown(not_null<PeerData*> peer) {
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
int ComputeSendingMessagesCount(
|
||||||
|
not_null<History*> history,
|
||||||
|
const SendingErrorRequest &request) {
|
||||||
|
auto result = 0;
|
||||||
|
if (request.text && !request.text->empty()) {
|
||||||
|
auto sending = TextWithEntities();
|
||||||
|
auto left = TextWithEntities{
|
||||||
|
request.text->text,
|
||||||
|
TextUtilities::ConvertTextTagsToEntities(request.text->tags)
|
||||||
|
};
|
||||||
|
auto prepareFlags = Ui::ItemTextOptions(
|
||||||
|
history,
|
||||||
|
history->session().user()).flags;
|
||||||
|
TextUtilities::PrepareForSending(left, prepareFlags);
|
||||||
|
|
||||||
|
while (TextUtilities::CutPart(sending, left, MaxMessageSize)) {
|
||||||
|
++result;
|
||||||
|
}
|
||||||
|
if (!result) {
|
||||||
|
++result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
+ (request.story ? 1 : 0)
|
||||||
|
+ (request.forward ? int(request.forward->size()) : 0);
|
||||||
|
}
|
||||||
|
|
||||||
Data::SendError GetErrorForSending(
|
Data::SendError GetErrorForSending(
|
||||||
not_null<PeerData*> peer,
|
not_null<PeerData*> peer,
|
||||||
SendingErrorRequest request) {
|
SendingErrorRequest request) {
|
||||||
|
@ -93,33 +120,10 @@ Data::SendError GetErrorForSending(
|
||||||
return tr::lng_forward_cant(tr::now);
|
return tr::lng_forward_cant(tr::now);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const auto countMessages = [&] {
|
|
||||||
auto result = 0;
|
|
||||||
if (hasText) {
|
|
||||||
auto sending = TextWithEntities();
|
|
||||||
auto left = TextWithEntities{
|
|
||||||
request.text->text,
|
|
||||||
TextUtilities::ConvertTextTagsToEntities(request.text->tags)
|
|
||||||
};
|
|
||||||
auto prepareFlags = Ui::ItemTextOptions(
|
|
||||||
thread->owningHistory(),
|
|
||||||
peer->session().user()).flags;
|
|
||||||
TextUtilities::PrepareForSending(left, prepareFlags);
|
|
||||||
|
|
||||||
while (TextUtilities::CutPart(sending, left, MaxMessageSize)) {
|
|
||||||
++result;
|
|
||||||
}
|
|
||||||
if (!result) {
|
|
||||||
++result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
+ (request.story ? 1 : 0)
|
|
||||||
+ (request.mediaMessage ? 1 : 0)
|
|
||||||
+ (request.forward ? int(request.forward->size()) : 0);
|
|
||||||
};
|
|
||||||
if (peer->slowmodeApplied()) {
|
if (peer->slowmodeApplied()) {
|
||||||
const auto count = countMessages();
|
const auto count = request.messagesCount
|
||||||
|
? request.messagesCount
|
||||||
|
: ComputeSendingMessagesCount(thread->owningHistory(), request);
|
||||||
if (const auto history = peer->owner().historyLoaded(peer)) {
|
if (const auto history = peer->owner().historyLoaded(peer)) {
|
||||||
if (!request.ignoreSlowmodeCountdown
|
if (!request.ignoreSlowmodeCountdown
|
||||||
&& (history->latestSendingMessage() != nullptr)
|
&& (history->latestSendingMessage() != nullptr)
|
||||||
|
@ -157,29 +161,6 @@ Data::SendError GetErrorForSending(
|
||||||
Ui::FormatDurationWordsSlowmode(left));
|
Ui::FormatDurationWordsSlowmode(left));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (const auto user = peer->asUser()) {
|
|
||||||
if (user->hasStarsPerMessage()
|
|
||||||
&& !user->messageMoneyRestrictionsKnown()) {
|
|
||||||
user->updateFull();
|
|
||||||
return Data::SendError({ .resolving = true });
|
|
||||||
}
|
|
||||||
} else if (const auto channel = peer->asChannel()) {
|
|
||||||
if (!channel->isFullLoaded()) {
|
|
||||||
channel->updateFull();
|
|
||||||
return Data::SendError({ .resolving = true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!peer->session().credits().loaded()) {
|
|
||||||
peer->session().credits().load();
|
|
||||||
return Data::SendError({ .resolving = true });
|
|
||||||
} else if (const auto perMessage = peer->starsPerMessageChecked()) {
|
|
||||||
const auto count = countMessages();
|
|
||||||
return Data::SendError({
|
|
||||||
.paidStars = count * perMessage,
|
|
||||||
.paidMessages = count,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,6 +182,34 @@ Data::SendErrorWithThread GetErrorForSending(
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<SendPaymentDetails> ComputePaymentDetails(
|
||||||
|
not_null<PeerData*> peer,
|
||||||
|
int messagesCount) {
|
||||||
|
if (const auto user = peer->asUser()) {
|
||||||
|
if (user->hasStarsPerMessage()
|
||||||
|
&& !user->messageMoneyRestrictionsKnown()) {
|
||||||
|
user->updateFull();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
} else if (const auto channel = peer->asChannel()) {
|
||||||
|
if (!channel->isFullLoaded()) {
|
||||||
|
channel->updateFull();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!peer->session().credits().loaded()) {
|
||||||
|
peer->session().credits().load();
|
||||||
|
return {};
|
||||||
|
} else if (const auto perMessage = peer->starsPerMessageChecked()) {
|
||||||
|
return SendPaymentDetails{
|
||||||
|
.messages = messagesCount,
|
||||||
|
.stars = messagesCount * perMessage,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return SendPaymentDetails();
|
||||||
|
}
|
||||||
|
|
||||||
object_ptr<Ui::BoxContent> MakeSendErrorBox(
|
object_ptr<Ui::BoxContent> MakeSendErrorBox(
|
||||||
const Data::SendErrorWithThread &error,
|
const Data::SendErrorWithThread &error,
|
||||||
bool withTitle) {
|
bool withTitle) {
|
||||||
|
|
|
@ -114,9 +114,12 @@ struct SendingErrorRequest {
|
||||||
const HistoryItemsList *forward = nullptr;
|
const HistoryItemsList *forward = nullptr;
|
||||||
const Data::Story *story = nullptr;
|
const Data::Story *story = nullptr;
|
||||||
const TextWithTags *text = nullptr;
|
const TextWithTags *text = nullptr;
|
||||||
|
int messagesCount = 0;
|
||||||
bool ignoreSlowmodeCountdown = false;
|
bool ignoreSlowmodeCountdown = false;
|
||||||
bool mediaMessage = false;
|
|
||||||
};
|
};
|
||||||
|
[[nodiscard]] int ComputeSendingMessagesCount(
|
||||||
|
not_null<History*> history,
|
||||||
|
const SendingErrorRequest &request);
|
||||||
[[nodiscard]] Data::SendError GetErrorForSending(
|
[[nodiscard]] Data::SendError GetErrorForSending(
|
||||||
not_null<PeerData*> peer,
|
not_null<PeerData*> peer,
|
||||||
SendingErrorRequest request);
|
SendingErrorRequest request);
|
||||||
|
@ -124,6 +127,14 @@ struct SendingErrorRequest {
|
||||||
not_null<Data::Thread*> thread,
|
not_null<Data::Thread*> thread,
|
||||||
SendingErrorRequest request);
|
SendingErrorRequest request);
|
||||||
|
|
||||||
|
struct SendPaymentDetails {
|
||||||
|
int messages = 0;
|
||||||
|
int stars = 0;
|
||||||
|
};
|
||||||
|
[[nodiscard]] std::optional<SendPaymentDetails> ComputePaymentDetails(
|
||||||
|
not_null<PeerData*> peer,
|
||||||
|
int messagesCount);
|
||||||
|
|
||||||
[[nodiscard]] Data::SendErrorWithThread GetErrorForSending(
|
[[nodiscard]] Data::SendErrorWithThread GetErrorForSending(
|
||||||
const std::vector<not_null<Data::Thread*>> &threads,
|
const std::vector<not_null<Data::Thread*>> &threads,
|
||||||
SendingErrorRequest request);
|
SendingErrorRequest request);
|
||||||
|
|
|
@ -4351,13 +4351,11 @@ void HistoryWidget::sendVoice(const VoiceToSend &data) {
|
||||||
copy.options.starsApproved = approved;
|
copy.options.starsApproved = approved;
|
||||||
sendVoice(copy);
|
sendVoice(copy);
|
||||||
};
|
};
|
||||||
const auto ignoreSlowmodeCountdown = data.options.scheduled != 0;
|
const auto checked = checkSendPayment(
|
||||||
if (showSendMessageError(
|
1 + int(_forwardPanel->items().size()),
|
||||||
{},
|
data.options.starsApproved,
|
||||||
ignoreSlowmodeCountdown,
|
withPaymentApproved);
|
||||||
crl::guard(this, withPaymentApproved),
|
if (!checked) {
|
||||||
data.options.starsApproved,
|
|
||||||
true)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5904,28 +5902,38 @@ bool HistoryWidget::showSendMessageError(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const auto topicRootId = resolveReplyToTopicRootId();
|
const auto topicRootId = resolveReplyToTopicRootId();
|
||||||
const auto error = GetErrorForSending(
|
auto request = SendingErrorRequest{
|
||||||
_peer,
|
.topicRootId = topicRootId,
|
||||||
{
|
.forward = &_forwardPanel->items(),
|
||||||
.topicRootId = topicRootId,
|
.text = &textWithTags,
|
||||||
.forward = &_forwardPanel->items(),
|
.ignoreSlowmodeCountdown = ignoreSlowmodeCountdown,
|
||||||
.text = &textWithTags,
|
};
|
||||||
.ignoreSlowmodeCountdown = ignoreSlowmodeCountdown,
|
request.messagesCount = ComputeSendingMessagesCount(_history, request)
|
||||||
.mediaMessage = mediaMessage,
|
+ (mediaMessage ? 1 : 0);
|
||||||
});
|
const auto error = GetErrorForSending(_peer, request);
|
||||||
if (resend && error.resolving) {
|
if (error) {
|
||||||
|
Data::ShowSendErrorToast(controller(), _peer, error);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return resend
|
||||||
|
&& !checkSendPayment(request.messagesCount, starsApproved, resend);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HistoryWidget::checkSendPayment(
|
||||||
|
int messagesCount,
|
||||||
|
int starsApproved,
|
||||||
|
Fn<void(int starsApproved)> resend) {
|
||||||
|
const auto details = ComputePaymentDetails(_peer, messagesCount);
|
||||||
|
if (!details) {
|
||||||
_resendOnFullUpdated = [=] { resend(starsApproved); };
|
_resendOnFullUpdated = [=] { resend(starsApproved); };
|
||||||
return true;
|
return true;
|
||||||
} else if (resend && error.paidStars > starsApproved) {
|
} else if (const auto stars = details->stars) {
|
||||||
Data::ShowSendPaidConfirm(controller(), _peer, error, [=] {
|
Data::ShowSendPaidConfirm(controller(), _peer, error, [=] {
|
||||||
resend(error.paidStars);
|
resend(stars);
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
} else if (!error) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
Data::ShowSendErrorToast(controller(), _peer, error);
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HistoryWidget::confirmSendingFiles(const QStringList &files) {
|
bool HistoryWidget::confirmSendingFiles(const QStringList &files) {
|
||||||
|
|
|
@ -474,6 +474,10 @@ private:
|
||||||
Fn<void(int starsApproved)> resend = nullptr,
|
Fn<void(int starsApproved)> resend = nullptr,
|
||||||
int starsApproved = 0,
|
int starsApproved = 0,
|
||||||
bool mediaMessage = false);
|
bool mediaMessage = false);
|
||||||
|
bool checkSendPayment(
|
||||||
|
int messagesCount,
|
||||||
|
int starsApproved,
|
||||||
|
Fn<void(int starsApproved)> resend);
|
||||||
|
|
||||||
void sendingFilesConfirmed(
|
void sendingFilesConfirmed(
|
||||||
Ui::PreparedList &&list,
|
Ui::PreparedList &&list,
|
||||||
|
|
Loading…
Add table
Reference in a new issue