Added support of premium requirement for summary feature.

This commit is contained in:
23rd
2025-12-31 11:48:26 +03:00
parent 607226d196
commit ef7efd7796
5 changed files with 96 additions and 51 deletions
+19 -3
View File
@@ -115,13 +115,19 @@ void Transcribes::toggle(not_null<HistoryItem*> item) {
}
}
void Transcribes::toggleSummary(not_null<HistoryItem*> item) {
void Transcribes::toggleSummary(
not_null<HistoryItem*> item,
Fn<void()> onPremiumRequired) {
const auto id = item->fullId();
auto i = _summaries.find(id);
if (i == _summaries.end()) {
auto &entry = _summaries.emplace(id).first->second;
entry.onPremiumRequired = std::move(onPremiumRequired);
summarize(item);
} else if (!i->second.loading) {
i->second.shown = !i->second.shown;
i->second.shown = i->second.premiumRequired
? false
: !i->second.shown;
_session->data().requestItemResize(item);
if (i->second.shown) {
_session->data().requestItemShowHighlight(item);
@@ -252,6 +258,8 @@ void Transcribes::summarize(not_null<HistoryItem*> item) {
auto &entry = _summaries[id];
entry.requestId = 0;
entry.loading = false;
entry.premiumRequired = false;
entry.onPremiumRequired = nullptr;
entry.result = TextWithEntities(
qs(data.vtext()),
Api::EntitiesFromMTP(_session, data.ventities().v));
@@ -259,10 +267,18 @@ void Transcribes::summarize(not_null<HistoryItem*> item) {
_session->data().requestItemResize(item);
_session->data().requestItemShowHighlight(item);
}
}).fail([=] {
}).fail([=](const MTP::Error &error) {
auto &entry = _summaries[id];
if (error.type() == u"SUMMARY_FLOOD_PREMIUM"_q) {
if (!entry.premiumRequired && entry.onPremiumRequired) {
entry.onPremiumRequired();
}
entry.premiumRequired = true;
}
entry.requestId = 0;
entry.shown = false;
entry.loading = false;
entry.onPremiumRequired = nullptr;
if (const auto item = _session->data().message(id)) {
_session->data().requestItemResize(item);
}
+5 -1
View File
@@ -18,9 +18,11 @@ class Session;
namespace Api {
struct SummaryEntry {
Fn<void()> onPremiumRequired = nullptr;
TextWithEntities result;
bool shown = false;
bool loading = false;
bool premiumRequired = false;
mtpRequestId requestId = 0;
};
@@ -41,7 +43,9 @@ public:
void toggle(not_null<HistoryItem*> item);
[[nodiscard]] const Entry &entry(not_null<HistoryItem*> item) const;
void toggleSummary(not_null<HistoryItem*> item);
void toggleSummary(
not_null<HistoryItem*> item,
Fn<void()> onPremiumRequired);
[[nodiscard]] const SummaryEntry &summary(
not_null<const HistoryItem*> item) const;
void setSummaryFromLang(FullMsgId, QString &&summaryFromLang);
@@ -466,7 +466,9 @@ void Reply::updateForSummary(not_null<Element*> view) {
return;
}
if (const auto i = controller->session().data().message(my.itemId)) {
controller->session().api().transcribes().toggleSummary(i);
controller->session().api().transcribes().toggleSummary(
i,
nullptr);
}
});
}
@@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_session.h"
#include "main/main_session.h"
#include "lang/lang_keys.h"
#include "settings/settings_premium.h"
#include "ui/chat/chat_style.h"
#include "ui/effects/radial_animation.h"
#include "ui/effects/ripple_animation.h"
@@ -129,7 +130,43 @@ void TranscribeButton::paint(
p.setBrush(context.st->msgServiceBg());
p.drawEllipse(r);
if (!_loading && hasLock()) {
if (_summarize) {
if (!_particles) {
_particles = std::make_unique<Ui::StarParticles>(
Ui::StarParticles::Type::RadialInside,
7,
st::lineWidth * 4);
_particles->setColor(st::msgServiceFg->c);
_particles->setSpeed(0.2);
}
p.setClipRegion(QRegion(r, QRegion::Ellipse));
const auto paused = context.paused
|| (!_summarizeHovered && !_loading)
|| On(PowerSaving::kChatEffects);
_particles->paint(p, r, context.now, paused);
p.setClipping(false);
if (hasLock()) {
context.st->historyFastTranscribeLock().paint(
p,
r.topLeft() + st::historyFastSummaryLockPos,
r.width());
}
if (!paused) {
const auto session = &_item->history()->session();
Ui::PostponeCall(session, [=, itemId = _item->fullId()] {
if (const auto i = session->data().message(itemId)) {
session->data().requestItemRepaint(i, r);
}
});
}
(_item->history()->session().api().transcribes().summary(
_item).shown
? st::mediaviewFullScreenButton.icon
: st::mediaviewFullScreenOutIcon).paintInCenter(
p,
r,
st::msgServiceFg->c);
} else if (!_loading && hasLock()) {
ClipPainterForLock(p, true, r);
context.st->historyFastTranscribeIcon().paintInCenter(p, r);
p.setClipping(false);
@@ -138,39 +175,7 @@ void TranscribeButton::paint(
r.topLeft() + st::historyFastTranscribeLockPos,
r.width());
} else {
if (_summarize) {
if (!_particles) {
_particles = std::make_unique<Ui::StarParticles>(
Ui::StarParticles::Type::RadialInside,
7,
st::lineWidth * 4);
_particles->setColor(st::msgServiceFg->c);
_particles->setSpeed(0.2);
}
p.setClipRegion(QRegion(r, QRegion::Ellipse));
const auto paused = context.paused
|| (!_summarizeHovered && !_loading)
|| On(PowerSaving::kChatEffects);
_particles->paint(p, r, context.now, paused);
p.setClipping(false);
if (!paused) {
const auto session = &_item->history()->session();
Ui::PostponeCall(session, [=, itemId = _item->fullId()] {
if (const auto i = session->data().message(itemId)) {
session->data().requestItemRepaint(i, r);
}
});
}
(_item->history()->session().api().transcribes().summary(
_item).shown
? st::mediaviewFullScreenButton.icon
: st::mediaviewFullScreenOutIcon).paintInCenter(
p,
r,
st::msgServiceFg->c);
} else {
context.st->historyFastTranscribeIcon().paintInCenter(p, r);
}
context.st->historyFastTranscribeIcon().paintInCenter(p, r);
}
const auto state = _animation
@@ -273,10 +278,14 @@ void TranscribeButton::paint(
bool TranscribeButton::hasLock() const {
const auto session = &_item->history()->session();
if (session->premium()) {
return false;
}
const auto transcribes = &session->api().transcribes();
if (session->premium()
|| transcribes->freeFor(_item)
|| transcribes->trialsCount()) {
if (_summarize) {
return transcribes->summary(_item).premiumRequired;
}
if (transcribes->freeFor(_item) || transcribes->trialsCount()) {
return false;
}
const auto until = transcribes->trialsRefreshAt();
@@ -319,15 +328,19 @@ ClickHandlerPtr TranscribeButton::link() {
if (session->premium()) {
auto &transcribes = session->api().transcribes();
return summarize
? transcribes.toggleSummary(item)
? transcribes.toggleSummary(item, nullptr)
: transcribes.toggle(item);
}
const auto my = context.other.value<ClickHandlerContext>();
if (hasLock()) {
if (const auto controller = my.sessionWindow.get()) {
ShowPremiumPreviewBox(
controller,
PremiumFeature::VoiceToText);
if (summarize) {
Settings::ShowPremium(controller, u"summary"_q);
} else {
ShowPremiumPreviewBox(
controller,
PremiumFeature::VoiceToText);
}
}
} else {
const auto max = session->api().transcribes().trialsMaxLengthMs();
@@ -343,10 +356,19 @@ ClickHandlerPtr TranscribeButton::link() {
}
}
}
auto &transcribes = session->api().transcribes();
summarize
? transcribes.toggleSummary(item)
: transcribes.toggle(item);
if (summarize) {
const auto weak = my.sessionWindow;
session->api().transcribes().toggleSummary(item, [=] {
if (const auto strong = weak.get()) {
Settings::ShowPremium(strong, u"summary"_q);
// ShowPremiumPreviewBox(
// strong,
// PremiumFeature::VoiceToText);
}
});
} else {
session->api().transcribes().toggle(item);
}
}
});
return _link;
+2 -1
View File
@@ -591,10 +591,11 @@ historyGoToOriginalIcon: icon {{ "filled_go_to_message", msgServiceFg, point(0px
historyFastCommentsIcon: icon {{ "fast_comments", msgServiceFg }};
historyFastCloseSize: 30px;
historyFastCloseIcon: icon {{ "box_button_close", msgServiceFg }};
historyFastMoreIcon: icon {{ "title_menu_dots", msgServiceFg, point(0px, -2px) }};
historyFastMoreIcon: icon {{ "title_menu_dots", msgServiceFg, point(0px, -2px) }};
historyFastTranscribeIcon: icon {{ "chat/voice_to_text", msgServiceFg }};
historyFastTranscribeLock: icon {{ "chat/mini_lock", msgServiceFg }};
historyFastTranscribeLockPos: point(18px, 13px);
historyFastSummaryLockPos: point(13px, 13px);
historyFastTranscribeLockOverlayPos: point(21px, 13px);
historyFastTranscribeLockOverlaySize: size(6px, 10px);