gift_premium_box.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "boxes/gift_premium_box.h"
  8. #include "api/api_premium.h"
  9. #include "api/api_premium_option.h"
  10. #include "apiwrap.h"
  11. #include "base/timer_rpl.h"
  12. #include "base/unixtime.h"
  13. #include "base/weak_ptr.h"
  14. #include "boxes/peer_list_controllers.h" // ContactsBoxController.
  15. #include "boxes/peers/prepare_short_info_box.h"
  16. #include "boxes/peers/replace_boost_box.h" // BoostsForGift.
  17. #include "boxes/premium_preview_box.h" // ShowPremiumPreviewBox.
  18. #include "boxes/star_gift_box.h" // ShowStarGiftBox.
  19. #include "boxes/transfer_gift_box.h" // ShowTransferGiftBox.
  20. #include "core/ui_integration.h"
  21. #include "data/data_boosts.h"
  22. #include "data/data_changes.h"
  23. #include "data/data_channel.h"
  24. #include "data/data_credits.h"
  25. #include "data/data_emoji_statuses.h"
  26. #include "data/data_media_types.h" // Data::GiveawayStart.
  27. #include "data/data_peer_values.h" // Data::PeerPremiumValue.
  28. #include "data/data_session.h"
  29. #include "data/data_premium_subscription_option.h"
  30. #include "data/data_user.h"
  31. #include "data/stickers/data_custom_emoji.h"
  32. #include "info/channel_statistics/boosts/giveaway/boost_badge.h" // InfiniteRadialAnimationWidget.
  33. #include "info/profile/info_profile_badge.h"
  34. #include "info/profile/info_profile_values.h"
  35. #include "lang/lang_keys.h"
  36. #include "main/main_app_config.h"
  37. #include "main/main_session.h"
  38. #include "mainwidget.h"
  39. #include "payments/payments_checkout_process.h"
  40. #include "payments/payments_form.h"
  41. #include "settings/settings_credits_graphics.h"
  42. #include "settings/settings_premium.h"
  43. #include "ui/basic_click_handlers.h" // UrlClickHandler::Open.
  44. #include "ui/boxes/boost_box.h" // StartFireworks.
  45. #include "ui/controls/userpic_button.h"
  46. #include "ui/effects/credits_graphics.h"
  47. #include "ui/effects/premium_graphics.h"
  48. #include "ui/effects/premium_stars_colored.h"
  49. #include "ui/effects/premium_top_bar.h"
  50. #include "ui/effects/spoiler_mess.h"
  51. #include "ui/layers/generic_box.h"
  52. #include "ui/painter.h"
  53. #include "ui/rect.h"
  54. #include "ui/ui_utility.h"
  55. #include "ui/vertical_list.h"
  56. #include "ui/text/text_utilities.h"
  57. #include "ui/toast/toast.h"
  58. #include "ui/widgets/checkbox.h"
  59. #include "ui/widgets/gradient_round_button.h"
  60. #include "ui/widgets/tooltip.h"
  61. #include "ui/wrap/padding_wrap.h"
  62. #include "ui/wrap/slide_wrap.h"
  63. #include "ui/wrap/table_layout.h"
  64. #include "window/window_peer_menu.h" // ShowChooseRecipientBox.
  65. #include "window/window_session_controller.h"
  66. #include "styles/style_boxes.h"
  67. #include "styles/style_credits.h"
  68. #include "styles/style_giveaway.h"
  69. #include "styles/style_info.h"
  70. #include "styles/style_layers.h"
  71. #include "styles/style_premium.h"
  72. #include <QtGui/QGuiApplication>
  73. namespace {
  74. constexpr auto kRarityTooltipDuration = 3 * crl::time(1000);
  75. [[nodiscard]] QString CreateMessageLink(
  76. not_null<Main::Session*> session,
  77. PeerId peerId,
  78. uint64 messageId) {
  79. if (const auto msgId = MsgId(peerId ? messageId : 0)) {
  80. const auto peer = session->data().peer(peerId);
  81. if (const auto channel = peer->asBroadcast()) {
  82. const auto username = channel->username();
  83. const auto base = username.isEmpty()
  84. ? u"c/%1"_q.arg(peerToChannel(channel->id).bare)
  85. : username;
  86. const auto query = base + '/' + QString::number(msgId.bare);
  87. return session->createInternalLink(query);
  88. }
  89. }
  90. return QString();
  91. };
  92. [[nodiscard]] Data::GiftCodeLink MakeGiftCodeLink(
  93. not_null<Main::Session*> session,
  94. const QString &slug) {
  95. const auto path = u"giftcode/"_q + slug;
  96. return {
  97. session->createInternalLink(path),
  98. session->createInternalLinkFull(path),
  99. };
  100. }
  101. [[nodiscard]] object_ptr<Ui::RpWidget> MakeLinkCopyIcon(
  102. not_null<QWidget*> parent) {
  103. auto result = object_ptr<Ui::RpWidget>(parent);
  104. const auto raw = result.data();
  105. raw->paintRequest() | rpl::start_with_next([=] {
  106. auto p = QPainter(raw);
  107. const auto &icon = st::giveawayGiftCodeLinkCopy;
  108. const auto left = (raw->width() - icon.width()) / 2;
  109. const auto top = (raw->height() - icon.height()) / 2;
  110. icon.paint(p, left, top, raw->width());
  111. }, raw->lifetime());
  112. raw->resize(
  113. st::giveawayGiftCodeLinkCopyWidth,
  114. st::giveawayGiftCodeLinkHeight);
  115. raw->setAttribute(Qt::WA_TransparentForMouseEvents);
  116. return result;
  117. }
  118. [[nodiscard]] tr::phrase<lngtag_count> GiftDurationPhrase(int months) {
  119. return (months < 12)
  120. ? tr::lng_premium_gift_duration_months
  121. : tr::lng_premium_gift_duration_years;
  122. }
  123. [[nodiscard]] object_ptr<Ui::FlatLabel> MakeMaybeMultilineTokenValue(
  124. not_null<Ui::TableLayout*> table,
  125. const QString &token,
  126. Settings::CreditsEntryBoxStyleOverrides st) {
  127. constexpr auto kOneLineCount = 24;
  128. const auto oneLine = token.length() <= kOneLineCount;
  129. return object_ptr<Ui::FlatLabel>(
  130. table,
  131. rpl::single(
  132. Ui::Text::Wrapped({ token }, EntityType::Code, {})),
  133. (oneLine
  134. ? table->st().defaultValue
  135. : st.tableValueMultiline
  136. ? *st.tableValueMultiline
  137. : st::giveawayGiftCodeValueMultiline));
  138. }
  139. [[nodiscard]] object_ptr<Ui::RpWidget> MakePeerTableValue(
  140. not_null<Ui::TableLayout*> table,
  141. std::shared_ptr<ChatHelpers::Show> show,
  142. PeerId id,
  143. rpl::producer<QString> button = nullptr,
  144. Fn<void()> handler = nullptr) {
  145. auto result = object_ptr<Ui::AbstractButton>(table);
  146. const auto raw = result.data();
  147. const auto &st = st::giveawayGiftCodeUserpic;
  148. raw->resize(raw->width(), st.photoSize);
  149. const auto peer = show->session().data().peer(id);
  150. const auto userpic = Ui::CreateChild<Ui::UserpicButton>(raw, peer, st);
  151. const auto label = Ui::CreateChild<Ui::FlatLabel>(
  152. raw,
  153. (button && handler) ? peer->shortName() : peer->name(),
  154. table->st().defaultValue);
  155. const auto send = (button && handler)
  156. ? Ui::CreateChild<Ui::RoundButton>(
  157. raw,
  158. std::move(button),
  159. table->st().smallButton)
  160. : nullptr;
  161. if (send) {
  162. send->setTextTransform(Ui::RoundButton::TextTransform::NoTransform);
  163. send->setClickedCallback(std::move(handler));
  164. }
  165. rpl::combine(
  166. raw->widthValue(),
  167. send ? send->widthValue() : rpl::single(0)
  168. ) | rpl::start_with_next([=](int width, int sendWidth) {
  169. const auto position = st::giveawayGiftCodeNamePosition;
  170. const auto sendSkip = sendWidth
  171. ? (st::normalFont->spacew + sendWidth)
  172. : 0;
  173. label->resizeToNaturalWidth(width - position.x() - sendSkip);
  174. label->moveToLeft(position.x(), position.y(), width);
  175. const auto top = (raw->height() - userpic->height()) / 2;
  176. userpic->moveToLeft(0, top, width);
  177. if (send) {
  178. send->moveToLeft(
  179. position.x() + label->width() + st::normalFont->spacew,
  180. (position.y()
  181. + table->st().defaultValue.style.font->ascent
  182. - table->st().smallButton.style.font->ascent),
  183. width);
  184. }
  185. }, label->lifetime());
  186. userpic->setAttribute(Qt::WA_TransparentForMouseEvents);
  187. label->setAttribute(Qt::WA_TransparentForMouseEvents);
  188. label->setTextColorOverride(table->st().defaultValue.palette.linkFg->c);
  189. raw->setClickedCallback([=] {
  190. show->showBox(PrepareShortInfoBox(peer, show));
  191. });
  192. return result;
  193. }
  194. [[nodiscard]] object_ptr<Ui::RpWidget> MakePeerWithStatusValue(
  195. not_null<Ui::TableLayout*> table,
  196. std::shared_ptr<ChatHelpers::Show> show,
  197. PeerId id,
  198. Fn<void(not_null<Ui::RpWidget*>, EmojiStatusId)> pushStatusId) {
  199. auto result = object_ptr<Ui::AbstractButton>(table);
  200. const auto raw = result.data();
  201. const auto &st = st::giveawayGiftCodeUserpic;
  202. raw->resize(raw->width(), st.photoSize);
  203. const auto peer = show->session().data().peer(id);
  204. const auto userpic = Ui::CreateChild<Ui::UserpicButton>(raw, peer, st);
  205. const auto label = Ui::CreateChild<Ui::FlatLabel>(
  206. raw,
  207. peer->name(),
  208. table->st().defaultValue);
  209. using namespace Info::Profile;
  210. struct State {
  211. rpl::variable<Badge::Content> content;
  212. };
  213. const auto state = label->lifetime().make_state<State>();
  214. state->content = EmojiStatusIdValue(
  215. peer
  216. ) | rpl::map([=](EmojiStatusId emojiStatusId) {
  217. if (!peer->session().premium()
  218. || (!peer->isSelf() && !emojiStatusId)) {
  219. return Badge::Content();
  220. }
  221. return Badge::Content{
  222. .badge = BadgeType::Premium,
  223. .emojiStatusId = emojiStatusId,
  224. };
  225. });
  226. const auto badge = label->lifetime().make_state<Badge>(
  227. raw,
  228. st::infoPeerBadge,
  229. &peer->session(),
  230. state->content.value(),
  231. nullptr,
  232. [=] { return show->paused(ChatHelpers::PauseReason::Layer); });
  233. state->content.value(
  234. ) | rpl::start_with_next([=](const Badge::Content &content) {
  235. if (const auto widget = badge->widget()) {
  236. pushStatusId(widget, content.emojiStatusId);
  237. }
  238. }, raw->lifetime());
  239. rpl::combine(
  240. raw->widthValue(),
  241. rpl::single(rpl::empty) | rpl::then(badge->updated())
  242. ) | rpl::start_with_next([=](int width, const auto &) {
  243. const auto position = st::giveawayGiftCodeNamePosition;
  244. const auto badgeWidget = badge->widget();
  245. const auto badgeSkip = badgeWidget
  246. ? (st::normalFont->spacew + badgeWidget->width())
  247. : 0;
  248. label->resizeToNaturalWidth(width - position.x() - badgeSkip);
  249. label->moveToLeft(position.x(), position.y(), width);
  250. const auto top = (raw->height() - userpic->height()) / 2;
  251. userpic->moveToLeft(0, top, width);
  252. if (badgeWidget) {
  253. badgeWidget->moveToLeft(
  254. position.x() + label->width() + st::normalFont->spacew,
  255. (position.y()
  256. + table->st().defaultValue.style.font->ascent
  257. - table->st().smallButton.style.font->ascent),
  258. width);
  259. }
  260. }, label->lifetime());
  261. userpic->setAttribute(Qt::WA_TransparentForMouseEvents);
  262. label->setAttribute(Qt::WA_TransparentForMouseEvents);
  263. label->setTextColorOverride(table->st().defaultValue.palette.linkFg->c);
  264. raw->setClickedCallback([=] {
  265. show->showBox(PrepareShortInfoBox(peer, show));
  266. });
  267. return result;
  268. }
  269. [[nodiscard]] object_ptr<Ui::RpWidget> MakeHiddenPeerTableValue(
  270. not_null<Ui::TableLayout*> table) {
  271. auto result = object_ptr<Ui::RpWidget>(table);
  272. const auto raw = result.data();
  273. const auto &st = st::giveawayGiftCodeUserpic;
  274. raw->resize(raw->width(), st.photoSize);
  275. const auto userpic = Ui::CreateChild<Ui::RpWidget>(raw);
  276. const auto usize = st.photoSize;
  277. userpic->resize(usize, usize);
  278. userpic->paintRequest() | rpl::start_with_next([=] {
  279. auto p = QPainter(userpic);
  280. Ui::EmptyUserpic::PaintHiddenAuthor(p, 0, 0, usize, usize);
  281. }, userpic->lifetime());
  282. const auto label = Ui::CreateChild<Ui::FlatLabel>(
  283. raw,
  284. tr::lng_gift_from_hidden(),
  285. table->st().defaultValue);
  286. raw->widthValue(
  287. ) | rpl::start_with_next([=](int width) {
  288. const auto position = st::giveawayGiftCodeNamePosition;
  289. label->resizeToNaturalWidth(width - position.x());
  290. label->moveToLeft(position.x(), position.y(), width);
  291. const auto top = (raw->height() - userpic->height()) / 2;
  292. userpic->moveToLeft(0, top, width);
  293. }, label->lifetime());
  294. userpic->setAttribute(Qt::WA_TransparentForMouseEvents);
  295. label->setAttribute(Qt::WA_TransparentForMouseEvents);
  296. label->setTextColorOverride(st::windowFg->c);
  297. return result;
  298. }
  299. void AddTableRow(
  300. not_null<Ui::TableLayout*> table,
  301. rpl::producer<QString> label,
  302. object_ptr<Ui::RpWidget> value,
  303. style::margins valueMargins) {
  304. table->addRow(
  305. (label
  306. ? object_ptr<Ui::FlatLabel>(
  307. table,
  308. std::move(label),
  309. table->st().defaultLabel)
  310. : object_ptr<Ui::FlatLabel>(nullptr)),
  311. std::move(value),
  312. st::giveawayGiftCodeLabelMargin,
  313. valueMargins);
  314. }
  315. [[nodiscard]] object_ptr<Ui::RpWidget> MakeAttributeValue(
  316. not_null<Ui::TableLayout*> table,
  317. const Data::UniqueGiftAttribute &attribute,
  318. Fn<void(not_null<Ui::RpWidget*>, int)> showTooltip) {
  319. auto result = object_ptr<Ui::RpWidget>(table);
  320. const auto raw = result.data();
  321. const auto label = Ui::CreateChild<Ui::FlatLabel>(
  322. raw,
  323. attribute.name,
  324. table->st().defaultValue);
  325. const auto permille = attribute.rarityPermille;
  326. const auto text = QString::number(permille / 10.) + '%';
  327. const auto rarity = Ui::CreateChild<Ui::RoundButton>(
  328. raw,
  329. rpl::single(text),
  330. table->st().smallButton);
  331. rarity->setTextTransform(Ui::RoundButton::TextTransform::NoTransform);
  332. rpl::combine(
  333. raw->widthValue(),
  334. rarity->widthValue()
  335. ) | rpl::start_with_next([=](int width, int convertWidth) {
  336. const auto convertSkip = convertWidth
  337. ? (st::normalFont->spacew + convertWidth)
  338. : 0;
  339. label->resizeToNaturalWidth(width - convertSkip);
  340. label->moveToLeft(0, 0, width);
  341. rarity->moveToLeft(
  342. label->width() + st::normalFont->spacew,
  343. (table->st().defaultValue.style.font->ascent
  344. - table->st().smallButton.style.font->ascent),
  345. width);
  346. }, label->lifetime());
  347. label->heightValue() | rpl::start_with_next([=](int height) {
  348. raw->resize(
  349. raw->width(),
  350. height + st::giveawayGiftCodeValueMargin.bottom());
  351. }, raw->lifetime());
  352. label->setAttribute(Qt::WA_TransparentForMouseEvents);
  353. rarity->setClickedCallback([=] {
  354. showTooltip(rarity, permille);
  355. });
  356. return result;
  357. }
  358. [[nodiscard]] object_ptr<Ui::RpWidget> MakeStarGiftStarsValue(
  359. not_null<Ui::TableLayout*> table,
  360. std::shared_ptr<ChatHelpers::Show> show,
  361. const Data::CreditsHistoryEntry &entry,
  362. Fn<void()> convertToStars) {
  363. auto result = object_ptr<Ui::RpWidget>(table);
  364. const auto raw = result.data();
  365. const auto star = Ui::CreateSingleStarWidget(
  366. raw,
  367. table->st().defaultValue.style.font->height);
  368. const auto label = Ui::CreateChild<Ui::FlatLabel>(
  369. raw,
  370. Lang::FormatStarsAmountDecimal(entry.credits),
  371. table->st().defaultValue,
  372. st::defaultPopupMenu);
  373. const auto convert = convertToStars
  374. ? Ui::CreateChild<Ui::RoundButton>(
  375. raw,
  376. tr::lng_gift_sell_small(
  377. lt_count_decimal,
  378. rpl::single(entry.starsConverted * 1.)),
  379. table->st().smallButton)
  380. : nullptr;
  381. if (convert) {
  382. using namespace Ui;
  383. convert->setTextTransform(RoundButton::TextTransform::NoTransform);
  384. convert->setClickedCallback(std::move(convertToStars));
  385. }
  386. rpl::combine(
  387. raw->widthValue(),
  388. convert ? convert->widthValue() : rpl::single(0)
  389. ) | rpl::start_with_next([=](int width, int convertWidth) {
  390. const auto convertSkip = convertWidth
  391. ? (st::normalFont->spacew + convertWidth)
  392. : 0;
  393. const auto labelLeft = rect::right(star) + st::normalFont->spacew;
  394. label->resizeToNaturalWidth(width - convertSkip - labelLeft);
  395. star->moveToLeft(0, 0, width);
  396. label->moveToLeft(labelLeft, 0, width);
  397. if (convert) {
  398. convert->moveToLeft(
  399. rect::right(label) + st::normalFont->spacew,
  400. (table->st().defaultValue.style.font->ascent
  401. - table->st().smallButton.style.font->ascent),
  402. width);
  403. }
  404. }, label->lifetime());
  405. label->heightValue() | rpl::start_with_next([=](int height) {
  406. raw->resize(
  407. raw->width(),
  408. height + st::giveawayGiftCodeValueMargin.bottom());
  409. }, raw->lifetime());
  410. label->setAttribute(Qt::WA_TransparentForMouseEvents);
  411. return result;
  412. }
  413. [[nodiscard]] object_ptr<Ui::RpWidget> MakeNonUniqueStatusTableValue(
  414. not_null<Ui::TableLayout*> table,
  415. Fn<void()> startUpgrade) {
  416. auto result = object_ptr<Ui::RpWidget>(table);
  417. const auto raw = result.data();
  418. const auto label = Ui::CreateChild<Ui::FlatLabel>(
  419. raw,
  420. tr::lng_gift_unique_status_non(),
  421. table->st().defaultValue,
  422. st::defaultPopupMenu);
  423. const auto upgrade = startUpgrade
  424. ? Ui::CreateChild<Ui::RoundButton>(
  425. raw,
  426. tr::lng_gift_unique_status_upgrade(),
  427. table->st().smallButton)
  428. : (Ui::RoundButton*)(nullptr);
  429. if (upgrade) {
  430. using namespace Ui;
  431. upgrade->setTextTransform(RoundButton::TextTransform::NoTransform);
  432. upgrade->setClickedCallback(startUpgrade);
  433. }
  434. rpl::combine(
  435. raw->widthValue(),
  436. upgrade ? upgrade->widthValue() : rpl::single(0)
  437. ) | rpl::start_with_next([=](int width, int toggleWidth) {
  438. const auto toggleSkip = toggleWidth
  439. ? (st::normalFont->spacew + toggleWidth)
  440. : 0;
  441. label->resizeToNaturalWidth(width - toggleSkip);
  442. label->moveToLeft(0, 0, width);
  443. if (upgrade) {
  444. upgrade->moveToLeft(
  445. label->width() + st::normalFont->spacew,
  446. (table->st().defaultValue.style.font->ascent
  447. - table->st().smallButton.style.font->ascent),
  448. width);
  449. }
  450. }, label->lifetime());
  451. label->heightValue() | rpl::start_with_next([=](int height) {
  452. raw->resize(
  453. raw->width(),
  454. height + st::giveawayGiftCodeValueMargin.bottom());
  455. }, raw->lifetime());
  456. label->setAttribute(Qt::WA_TransparentForMouseEvents);
  457. return result;
  458. }
  459. not_null<Ui::FlatLabel*> AddTableRow(
  460. not_null<Ui::TableLayout*> table,
  461. rpl::producer<QString> label,
  462. rpl::producer<TextWithEntities> value,
  463. const Ui::Text::MarkedContext &context = {}) {
  464. auto widget = object_ptr<Ui::FlatLabel>(
  465. table,
  466. std::move(value),
  467. table->st().defaultValue,
  468. st::defaultPopupMenu,
  469. context);
  470. const auto result = widget.data();
  471. AddTableRow(
  472. table,
  473. std::move(label),
  474. std::move(widget),
  475. st::giveawayGiftCodeValueMargin);
  476. return result;
  477. }
  478. void AddTableRow(
  479. not_null<Ui::TableLayout*> table,
  480. rpl::producer<QString> label,
  481. std::shared_ptr<ChatHelpers::Show> show,
  482. PeerId id) {
  483. if (!id) {
  484. return;
  485. }
  486. AddTableRow(
  487. table,
  488. std::move(label),
  489. MakePeerTableValue(table, show, id),
  490. st::giveawayGiftCodePeerMargin);
  491. }
  492. void AddTable(
  493. not_null<Ui::VerticalLayout*> container,
  494. std::shared_ptr<ChatHelpers::Show> show,
  495. Settings::CreditsEntryBoxStyleOverrides st,
  496. const Api::GiftCode &current,
  497. bool skipReason) {
  498. auto table = container->add(
  499. object_ptr<Ui::TableLayout>(
  500. container,
  501. st.table ? *st.table : st::giveawayGiftCodeTable),
  502. st::giveawayGiftCodeTableMargin);
  503. if (current.from) {
  504. AddTableRow(
  505. table,
  506. tr::lng_gift_link_label_from(),
  507. show,
  508. current.from);
  509. }
  510. if (current.from && current.to) {
  511. AddTableRow(
  512. table,
  513. tr::lng_gift_link_label_to(),
  514. show,
  515. current.to);
  516. } else if (current.from) {
  517. AddTableRow(
  518. table,
  519. tr::lng_gift_link_label_to(),
  520. tr::lng_gift_link_label_to_unclaimed(Ui::Text::WithEntities));
  521. }
  522. AddTableRow(
  523. table,
  524. tr::lng_gift_link_label_gift(),
  525. tr::lng_gift_link_gift_premium(
  526. lt_duration,
  527. GiftDurationValue(current.months) | Ui::Text::ToWithEntities(),
  528. Ui::Text::WithEntities));
  529. if (!skipReason && current.from) {
  530. const auto reason = AddTableRow(
  531. table,
  532. tr::lng_gift_link_label_reason(),
  533. (current.giveawayId
  534. ? ((current.to
  535. ? tr::lng_gift_link_reason_giveaway
  536. : tr::lng_gift_link_reason_unclaimed)(
  537. ) | Ui::Text::ToLink())
  538. : current.giveaway
  539. ? ((current.to
  540. ? tr::lng_gift_link_reason_giveaway
  541. : tr::lng_gift_link_reason_unclaimed)(
  542. Ui::Text::WithEntities
  543. ) | rpl::type_erased())
  544. : tr::lng_gift_link_reason_chosen(Ui::Text::WithEntities)));
  545. reason->setClickHandlerFilter([=](const auto &...) {
  546. if (const auto window = show->resolveWindow()) {
  547. window->showPeerHistory(
  548. current.from,
  549. Window::SectionShow::Way::Forward,
  550. current.giveawayId);
  551. }
  552. return false;
  553. });
  554. }
  555. if (current.date) {
  556. AddTableRow(
  557. table,
  558. tr::lng_gift_link_label_date(),
  559. rpl::single(Ui::Text::WithEntities(
  560. langDateTime(base::unixtime::parse(current.date)))));
  561. }
  562. }
  563. void ShareWithFriend(
  564. not_null<Window::SessionNavigation*> navigation,
  565. const QString &slug) {
  566. const auto chosen = [=](not_null<Data::Thread*> thread) {
  567. const auto content = navigation->parentController()->content();
  568. return content->shareUrl(
  569. thread,
  570. MakeGiftCodeLink(&navigation->session(), slug).link,
  571. QString());
  572. };
  573. Window::ShowChooseRecipientBox(navigation, chosen);
  574. }
  575. void ShowAlreadyPremiumToast(
  576. not_null<Window::SessionNavigation*> navigation,
  577. const QString &slug,
  578. TimeId date) {
  579. const auto instance = std::make_shared<
  580. base::weak_ptr<Ui::Toast::Instance>
  581. >();
  582. const auto shareLink = [=](
  583. const ClickHandlerPtr &,
  584. Qt::MouseButton button) {
  585. if (button == Qt::LeftButton) {
  586. if (const auto strong = instance->get()) {
  587. strong->hideAnimated();
  588. }
  589. ShareWithFriend(navigation, slug);
  590. }
  591. return false;
  592. };
  593. *instance = navigation->showToast({
  594. .title = tr::lng_gift_link_already_title(tr::now),
  595. .text = tr::lng_gift_link_already_about(
  596. tr::now,
  597. lt_date,
  598. Ui::Text::Bold(langDateTime(base::unixtime::parse(date))),
  599. lt_link,
  600. Ui::Text::Link(
  601. Ui::Text::Bold(tr::lng_gift_link_already_link(tr::now))),
  602. Ui::Text::WithEntities),
  603. .filter = crl::guard(navigation, shareLink),
  604. .duration = 6 * crl::time(1000),
  605. });
  606. }
  607. } // namespace
  608. rpl::producer<QString> GiftDurationValue(int months) {
  609. return GiftDurationPhrase(months)(
  610. lt_count,
  611. rpl::single(float64((months < 12) ? months : (months / 12))));
  612. }
  613. QString GiftDuration(int months) {
  614. return GiftDurationPhrase(months)(
  615. tr::now,
  616. lt_count,
  617. (months < 12) ? months : (months / 12));
  618. }
  619. void GiftCodeBox(
  620. not_null<Ui::GenericBox*> box,
  621. not_null<Window::SessionNavigation*> controller,
  622. const QString &slug) {
  623. struct State {
  624. rpl::variable<Api::GiftCode> data;
  625. rpl::variable<bool> used;
  626. bool sent = false;
  627. };
  628. const auto session = &controller->session();
  629. const auto state = box->lifetime().make_state<State>(State{});
  630. state->data = session->api().premium().giftCodeValue(slug);
  631. state->used = state->data.value(
  632. ) | rpl::map([=](const Api::GiftCode &data) {
  633. return data.used != 0;
  634. });
  635. box->setWidth(st::boxWideWidth);
  636. box->setStyle(st::giveawayGiftCodeBox);
  637. box->setNoContentMargin(true);
  638. const auto bar = box->setPinnedToTopContent(
  639. object_ptr<Ui::Premium::TopBar>(
  640. box,
  641. st::giveawayGiftCodeCover,
  642. Ui::Premium::TopBarDescriptor{
  643. .clickContextOther = nullptr,
  644. .title = rpl::conditional(
  645. state->used.value(),
  646. tr::lng_gift_link_used_title(),
  647. tr::lng_gift_link_title()),
  648. .about = rpl::conditional(
  649. state->used.value(),
  650. tr::lng_gift_link_used_about(Ui::Text::RichLangValue),
  651. tr::lng_gift_link_about(Ui::Text::RichLangValue)),
  652. .light = true,
  653. }));
  654. const auto max = st::giveawayGiftCodeTopHeight;
  655. bar->setMaximumHeight(max);
  656. bar->setMinimumHeight(st::infoLayerTopBarHeight);
  657. bar->resize(bar->width(), bar->maximumHeight());
  658. const auto link = MakeGiftCodeLink(&controller->session(), slug);
  659. box->addRow(
  660. Ui::MakeLinkLabel(
  661. box,
  662. rpl::single(link.text),
  663. rpl::single(link.link),
  664. box->uiShow(),
  665. MakeLinkCopyIcon(box)),
  666. st::giveawayGiftCodeLinkMargin);
  667. const auto show = controller->uiShow();
  668. AddTable(box->verticalLayout(), show, {}, state->data.current(), false);
  669. auto shareLink = tr::lng_gift_link_also_send_link(
  670. ) | rpl::map([](const QString &text) {
  671. return Ui::Text::Link(text);
  672. });
  673. auto richDate = [](const Api::GiftCode &data) {
  674. return TextWithEntities{
  675. langDateTime(base::unixtime::parse(data.used)),
  676. };
  677. };
  678. const auto footer = box->addRow(
  679. object_ptr<Ui::FlatLabel>(
  680. box,
  681. rpl::conditional(
  682. state->used.value(),
  683. tr::lng_gift_link_used_footer(
  684. lt_date,
  685. state->data.value() | rpl::map(richDate),
  686. Ui::Text::WithEntities),
  687. tr::lng_gift_link_also_send(
  688. lt_link,
  689. std::move(shareLink),
  690. Ui::Text::WithEntities)),
  691. st::giveawayGiftCodeFooter),
  692. st::giveawayGiftCodeFooterMargin);
  693. footer->setClickHandlerFilter([=](const auto &...) {
  694. ShareWithFriend(controller, slug);
  695. return false;
  696. });
  697. const auto close = Ui::CreateChild<Ui::IconButton>(
  698. box.get(),
  699. st::boxTitleClose);
  700. close->setClickedCallback([=] {
  701. box->closeBox();
  702. });
  703. box->widthValue(
  704. ) | rpl::start_with_next([=](int width) {
  705. close->moveToRight(0, 0);
  706. }, box->lifetime());
  707. const auto button = box->addButton(rpl::conditional(
  708. state->used.value(),
  709. tr::lng_box_ok(),
  710. tr::lng_gift_link_use()
  711. ), [=] {
  712. if (state->used.current()) {
  713. box->closeBox();
  714. } else if (!state->sent) {
  715. state->sent = true;
  716. const auto done = crl::guard(box, [=](const QString &error) {
  717. const auto activePrefix = u"PREMIUM_SUB_ACTIVE_UNTIL_"_q;
  718. if (error.isEmpty()) {
  719. auto copy = state->data.current();
  720. copy.used = base::unixtime::now();
  721. state->data = std::move(copy);
  722. Ui::StartFireworks(box->parentWidget());
  723. } else if (error.startsWith(activePrefix)) {
  724. const auto date = error.mid(activePrefix.size()).toInt();
  725. ShowAlreadyPremiumToast(controller, slug, date);
  726. state->sent = false;
  727. } else {
  728. box->uiShow()->showToast(error);
  729. state->sent = false;
  730. }
  731. });
  732. controller->session().api().premium().applyGiftCode(slug, done);
  733. }
  734. });
  735. const auto buttonPadding = st::giveawayGiftCodeBox.buttonPadding;
  736. const auto buttonWidth = st::boxWideWidth
  737. - buttonPadding.left()
  738. - buttonPadding.right();
  739. button->widthValue() | rpl::filter([=] {
  740. return (button->widthNoMargins() != buttonWidth);
  741. }) | rpl::start_with_next([=] {
  742. button->resizeToWidth(buttonWidth);
  743. }, button->lifetime());
  744. }
  745. void GiftCodePendingBox(
  746. not_null<Ui::GenericBox*> box,
  747. not_null<Window::SessionNavigation*> controller,
  748. const Api::GiftCode &data) {
  749. box->setWidth(st::boxWideWidth);
  750. box->setStyle(st::giveawayGiftCodeBox);
  751. box->setNoContentMargin(true);
  752. {
  753. const auto peerTo = controller->session().data().peer(data.to);
  754. const auto clickContext = [=, weak = base::make_weak(controller)] {
  755. if (const auto strong = weak.get()) {
  756. strong->uiShow()->showBox(
  757. PrepareShortInfoBox(peerTo, strong));
  758. }
  759. return QVariant();
  760. };
  761. const auto &st = st::giveawayGiftCodeCover;
  762. const auto resultToName = st.about.style.font->elided(
  763. peerTo->shortName(),
  764. st.about.minWidth / 2,
  765. Qt::ElideMiddle);
  766. const auto bar = box->setPinnedToTopContent(
  767. object_ptr<Ui::Premium::TopBar>(
  768. box,
  769. st,
  770. Ui::Premium::TopBarDescriptor{
  771. .clickContextOther = clickContext,
  772. .title = tr::lng_gift_link_title(),
  773. .about = tr::lng_gift_link_pending_about(
  774. lt_user,
  775. rpl::single(Ui::Text::Link(resultToName)),
  776. Ui::Text::RichLangValue),
  777. .light = true,
  778. }));
  779. const auto max = st::giveawayGiftCodeTopHeight;
  780. bar->setMaximumHeight(max);
  781. bar->setMinimumHeight(st::infoLayerTopBarHeight);
  782. bar->resize(bar->width(), bar->maximumHeight());
  783. }
  784. {
  785. const auto linkLabel = box->addRow(
  786. Ui::MakeLinkLabel(box, nullptr, nullptr, nullptr, nullptr),
  787. st::giveawayGiftCodeLinkMargin);
  788. const auto spoiler = Ui::CreateChild<Ui::AbstractButton>(linkLabel);
  789. spoiler->lifetime().make_state<Ui::Animations::Basic>([=] {
  790. spoiler->update();
  791. })->start();
  792. linkLabel->sizeValue(
  793. ) | rpl::start_with_next([=](const QSize &s) {
  794. spoiler->setGeometry(Rect(s));
  795. }, spoiler->lifetime());
  796. const auto spoilerCached = Ui::SpoilerMessCached(
  797. Ui::DefaultTextSpoilerMask(),
  798. st::giveawayGiftCodeLink.textFg->c);
  799. const auto textHeight = st::giveawayGiftCodeLink.style.font->height;
  800. spoiler->paintRequest(
  801. ) | rpl::start_with_next([=] {
  802. auto p = QPainter(spoiler);
  803. const auto rect = spoiler->rect();
  804. const auto r = rect
  805. - QMargins(
  806. st::boxRowPadding.left(),
  807. (rect.height() - textHeight) / 2,
  808. st::boxRowPadding.right(),
  809. (rect.height() - textHeight) / 2);
  810. Ui::FillSpoilerRect(p, r, spoilerCached.frame());
  811. }, spoiler->lifetime());
  812. spoiler->setClickedCallback([show = box->uiShow()] {
  813. show->showToast(tr::lng_gift_link_pending_toast(tr::now));
  814. });
  815. spoiler->show();
  816. }
  817. const auto show = controller->uiShow();
  818. AddTable(box->verticalLayout(), show, {}, data, true);
  819. box->addRow(
  820. object_ptr<Ui::FlatLabel>(
  821. box,
  822. tr::lng_gift_link_pending_footer(),
  823. st::giveawayGiftCodeFooter),
  824. st::giveawayGiftCodeFooterMargin);
  825. const auto close = Ui::CreateChild<Ui::IconButton>(
  826. box.get(),
  827. st::boxTitleClose);
  828. const auto closeCallback = [=] { box->closeBox(); };
  829. close->setClickedCallback(closeCallback);
  830. box->widthValue(
  831. ) | rpl::start_with_next([=](int width) {
  832. close->moveToRight(0, 0);
  833. }, box->lifetime());
  834. const auto button = box->addButton(tr::lng_close(), closeCallback);
  835. const auto buttonPadding = st::giveawayGiftCodeBox.buttonPadding;
  836. const auto buttonWidth = st::boxWideWidth
  837. - buttonPadding.left()
  838. - buttonPadding.right();
  839. button->widthValue() | rpl::filter([=] {
  840. return (button->widthNoMargins() != buttonWidth);
  841. }) | rpl::start_with_next([=] {
  842. button->resizeToWidth(buttonWidth);
  843. }, button->lifetime());
  844. }
  845. void ResolveGiftCode(
  846. not_null<Window::SessionNavigation*> controller,
  847. const QString &slug,
  848. PeerId fromId,
  849. PeerId toId) {
  850. const auto done = [=](Api::GiftCode code) {
  851. const auto session = &controller->session();
  852. const auto selfId = session->userPeerId();
  853. if (!code) {
  854. controller->showToast(tr::lng_gift_link_expired(tr::now));
  855. } else if (!code.from && fromId == selfId) {
  856. code.from = fromId;
  857. code.to = toId;
  858. const auto self = (fromId == selfId);
  859. const auto peer = session->data().peer(self ? toId : fromId);
  860. const auto months = code.months;
  861. const auto parent = controller->parentController();
  862. Settings::ShowGiftPremium(parent, peer, months, self);
  863. } else {
  864. controller->uiShow()->showBox(Box(GiftCodeBox, controller, slug));
  865. }
  866. };
  867. controller->session().api().premium().checkGiftCode(
  868. slug,
  869. crl::guard(controller, done));
  870. }
  871. void GiveawayInfoBox(
  872. not_null<Ui::GenericBox*> box,
  873. not_null<Window::SessionNavigation*> controller,
  874. std::optional<Data::GiveawayStart> start,
  875. std::optional<Data::GiveawayResults> results,
  876. Api::GiveawayInfo info) {
  877. Expects(start || results);
  878. using State = Api::GiveawayState;
  879. const auto finished = (info.state == State::Finished)
  880. || (info.state == State::Refunded);
  881. box->setTitle((finished
  882. ? tr::lng_prizes_end_title
  883. : tr::lng_prizes_how_title)());
  884. const auto first = results
  885. ? results->channel->name()
  886. : !start->channels.empty()
  887. ? start->channels.front()->name()
  888. : u"channel"_q;
  889. auto resultText = (!info.giftCode.isEmpty())
  890. ? tr::lng_prizes_you_won(
  891. lt_cup,
  892. rpl::single(
  893. TextWithEntities{ QString::fromUtf8("\xf0\x9f\x8f\x86") }),
  894. Ui::Text::WithEntities)
  895. : (info.credits)
  896. ? tr::lng_prizes_you_won_credits(
  897. lt_amount,
  898. tr::lng_prizes_you_won_credits_amount(
  899. lt_count,
  900. rpl::single(float64(info.credits)),
  901. Ui::Text::Bold),
  902. lt_cup,
  903. rpl::single(
  904. TextWithEntities{ QString::fromUtf8("\xf0\x9f\x8f\x86") }),
  905. Ui::Text::WithEntities)
  906. : (info.state == State::Finished)
  907. ? tr::lng_prizes_you_didnt(Ui::Text::WithEntities)
  908. : (rpl::producer<TextWithEntities>)(nullptr);
  909. if (resultText) {
  910. const auto &st = st::changePhoneDescription;
  911. const auto skip = st.style.font->height * 0.5;
  912. auto label = object_ptr<Ui::FlatLabel>(
  913. box.get(),
  914. std::move(resultText),
  915. st);
  916. if ((!info.giftCode.isEmpty()) || info.credits) {
  917. label->setTextColorOverride(st::windowActiveTextFg->c);
  918. }
  919. const auto result = box->addRow(
  920. object_ptr<Ui::PaddingWrap<Ui::CenterWrap<Ui::FlatLabel>>>(
  921. box.get(),
  922. object_ptr<Ui::CenterWrap<Ui::FlatLabel>>(
  923. box.get(),
  924. std::move(label)),
  925. QMargins(0, skip, 0, skip)));
  926. result->paintRequest() | rpl::start_with_next([=] {
  927. auto p = QPainter(result);
  928. p.setPen(Qt::NoPen);
  929. p.setBrush(st::boxDividerBg);
  930. p.drawRoundedRect(result->rect(), st::boxRadius, st::boxRadius);
  931. }, result->lifetime());
  932. Ui::AddSkip(box->verticalLayout());
  933. }
  934. auto text = TextWithEntities();
  935. const auto quantity = start
  936. ? start->quantity
  937. : (results->winnersCount + results->unclaimedCount);
  938. const auto months = start ? start->months : results->months;
  939. const auto group = results
  940. ? results->channel->isMegagroup()
  941. : (!start->channels.empty()
  942. && start->channels.front()->isMegagroup());
  943. const auto credits = start
  944. ? start->credits
  945. : (results ? results->credits : 0);
  946. text.append((finished
  947. ? tr::lng_prizes_end_text
  948. : tr::lng_prizes_how_text)(
  949. tr::now,
  950. lt_admins,
  951. credits
  952. ? (group
  953. ? tr::lng_prizes_credits_admins_group
  954. : tr::lng_prizes_credits_admins)(
  955. tr::now,
  956. lt_channel,
  957. Ui::Text::Bold(first),
  958. lt_amount,
  959. tr::lng_prizes_credits_admins_amount(
  960. tr::now,
  961. lt_count_decimal,
  962. float64(credits),
  963. Ui::Text::Bold),
  964. Ui::Text::RichLangValue)
  965. : (group
  966. ? tr::lng_prizes_admins_group
  967. : tr::lng_prizes_admins)(
  968. tr::now,
  969. lt_count,
  970. quantity,
  971. lt_channel,
  972. Ui::Text::Bold(first),
  973. lt_duration,
  974. TextWithEntities{ GiftDuration(months) },
  975. Ui::Text::RichLangValue),
  976. Ui::Text::RichLangValue));
  977. const auto many = start
  978. ? (start->channels.size() > 1)
  979. : (results->additionalPeersCount > 0);
  980. const auto count = info.winnersCount
  981. ? info.winnersCount
  982. : quantity;
  983. const auto all = start ? start->all : results->all;
  984. auto winners = all
  985. ? (many
  986. ? (group
  987. ? tr::lng_prizes_winners_all_of_many_group
  988. : tr::lng_prizes_winners_all_of_many)
  989. : (group
  990. ? tr::lng_prizes_winners_all_of_one_group
  991. : tr::lng_prizes_winners_all_of_one))(
  992. tr::now,
  993. lt_count,
  994. count,
  995. lt_channel,
  996. Ui::Text::Bold(first),
  997. Ui::Text::RichLangValue)
  998. : (many
  999. ? tr::lng_prizes_winners_new_of_many
  1000. : tr::lng_prizes_winners_new_of_one)(
  1001. tr::now,
  1002. lt_count,
  1003. count,
  1004. lt_channel,
  1005. Ui::Text::Bold(first),
  1006. lt_start_date,
  1007. Ui::Text::Bold(
  1008. langDateTime(base::unixtime::parse(info.startDate))),
  1009. Ui::Text::RichLangValue);
  1010. const auto additionalPrize = results
  1011. ? results->additionalPrize
  1012. : start->additionalPrize;
  1013. if (!additionalPrize.isEmpty()) {
  1014. text.append("\n\n").append((group
  1015. ? tr::lng_prizes_additional_added_group
  1016. : tr::lng_prizes_additional_added)(
  1017. tr::now,
  1018. lt_count,
  1019. count,
  1020. lt_channel,
  1021. Ui::Text::Bold(first),
  1022. lt_prize,
  1023. TextWithEntities{ additionalPrize },
  1024. Ui::Text::RichLangValue));
  1025. }
  1026. const auto untilDate = start
  1027. ? start->untilDate
  1028. : results->untilDate;
  1029. text.append("\n\n").append((finished
  1030. ? tr::lng_prizes_end_when_finish
  1031. : tr::lng_prizes_how_when_finish)(
  1032. tr::now,
  1033. lt_date,
  1034. Ui::Text::Bold(langDayOfMonthFull(
  1035. base::unixtime::parse(untilDate).date())),
  1036. lt_winners,
  1037. winners,
  1038. Ui::Text::RichLangValue));
  1039. if (info.activatedCount > 0) {
  1040. text.append(' ').append(tr::lng_prizes_end_activated(
  1041. tr::now,
  1042. lt_count,
  1043. info.activatedCount,
  1044. Ui::Text::RichLangValue));
  1045. }
  1046. if (!info.giftCode.isEmpty()
  1047. || info.state == State::Finished
  1048. || info.state == State::Preparing) {
  1049. } else if (info.state != State::Refunded) {
  1050. if (info.adminChannelId) {
  1051. const auto channel = controller->session().data().channel(
  1052. info.adminChannelId);
  1053. text.append("\n\n").append((channel->isMegagroup()
  1054. ? tr::lng_prizes_how_no_admin_group
  1055. : tr::lng_prizes_how_no_admin)(
  1056. tr::now,
  1057. lt_channel,
  1058. Ui::Text::Bold(channel->name()),
  1059. Ui::Text::RichLangValue));
  1060. } else if (info.tooEarlyDate) {
  1061. const auto channel = controller->session().data().channel(
  1062. info.adminChannelId);
  1063. text.append("\n\n").append((channel->isMegagroup()
  1064. ? tr::lng_prizes_how_no_joined_group
  1065. : tr::lng_prizes_how_no_joined)(
  1066. tr::now,
  1067. lt_date,
  1068. Ui::Text::Bold(
  1069. langDateTime(
  1070. base::unixtime::parse(info.tooEarlyDate))),
  1071. Ui::Text::RichLangValue));
  1072. } else if (!info.disallowedCountry.isEmpty()) {
  1073. text.append("\n\n").append(tr::lng_prizes_how_no_country(
  1074. tr::now,
  1075. Ui::Text::RichLangValue));
  1076. } else if (info.participating) {
  1077. text.append("\n\n").append((many
  1078. ? tr::lng_prizes_how_yes_joined_many
  1079. : tr::lng_prizes_how_yes_joined_one)(
  1080. tr::now,
  1081. lt_channel,
  1082. Ui::Text::Bold(first),
  1083. Ui::Text::RichLangValue));
  1084. } else {
  1085. text.append("\n\n").append((many
  1086. ? tr::lng_prizes_how_participate_many
  1087. : tr::lng_prizes_how_participate_one)(
  1088. tr::now,
  1089. lt_channel,
  1090. Ui::Text::Bold(first),
  1091. lt_date,
  1092. Ui::Text::Bold(langDayOfMonthFull(
  1093. base::unixtime::parse(untilDate).date())),
  1094. Ui::Text::RichLangValue));
  1095. }
  1096. }
  1097. const auto padding = st::boxPadding;
  1098. box->addRow(
  1099. object_ptr<Ui::FlatLabel>(
  1100. box.get(),
  1101. rpl::single(std::move(text)),
  1102. st::boxLabel),
  1103. { padding.left(), 0, padding.right(), padding.bottom() });
  1104. if (info.state == State::Refunded) {
  1105. const auto wrap = box->addRow(
  1106. object_ptr<Ui::PaddingWrap<Ui::FlatLabel>>(
  1107. box.get(),
  1108. object_ptr<Ui::FlatLabel>(
  1109. box.get(),
  1110. (group
  1111. ? tr::lng_prizes_cancelled_group()
  1112. : tr::lng_prizes_cancelled()),
  1113. st::giveawayRefundedLabel),
  1114. st::giveawayRefundedPadding),
  1115. { padding.left(), 0, padding.right(), padding.bottom() });
  1116. const auto bg = wrap->lifetime().make_state<Ui::RoundRect>(
  1117. st::boxRadius,
  1118. st::attentionBoxButton.textBgOver);
  1119. wrap->paintRequest() | rpl::start_with_next([=] {
  1120. auto p = QPainter(wrap);
  1121. bg->paint(p, wrap->rect());
  1122. }, wrap->lifetime());
  1123. }
  1124. if (const auto slug = info.giftCode; !slug.isEmpty()) {
  1125. box->addButton(tr::lng_prizes_view_prize(), [=] {
  1126. ResolveGiftCode(controller, slug);
  1127. });
  1128. box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
  1129. } else {
  1130. box->addButton(tr::lng_close(), [=] { box->closeBox(); });
  1131. }
  1132. }
  1133. void ResolveGiveawayInfo(
  1134. not_null<Window::SessionNavigation*> controller,
  1135. not_null<PeerData*> peer,
  1136. MsgId messageId,
  1137. std::optional<Data::GiveawayStart> start,
  1138. std::optional<Data::GiveawayResults> results) {
  1139. const auto show = [=](Api::GiveawayInfo info) {
  1140. if (!info) {
  1141. controller->showToast(
  1142. tr::lng_confirm_phone_link_invalid(tr::now));
  1143. } else {
  1144. controller->uiShow()->showBox(
  1145. Box(GiveawayInfoBox, controller, start, results, info));
  1146. }
  1147. };
  1148. controller->session().api().premium().resolveGiveawayInfo(
  1149. peer,
  1150. messageId,
  1151. crl::guard(controller, show));
  1152. }
  1153. QString TonAddressUrl(
  1154. not_null<Main::Session*> session,
  1155. const QString &address) {
  1156. const auto prefix = session->appConfig().get<QString>(
  1157. u"ton_blockchain_explorer_url"_q,
  1158. u"https://tonviewer.com/"_q);
  1159. return prefix + address;
  1160. }
  1161. void AddStarGiftTable(
  1162. std::shared_ptr<ChatHelpers::Show> show,
  1163. not_null<Ui::VerticalLayout*> container,
  1164. Settings::CreditsEntryBoxStyleOverrides st,
  1165. const Data::CreditsHistoryEntry &entry,
  1166. Fn<void()> convertToStars,
  1167. Fn<void()> startUpgrade) {
  1168. auto table = container->add(
  1169. object_ptr<Ui::TableLayout>(
  1170. container,
  1171. st.table ? *st.table : st::giveawayGiftCodeTable),
  1172. st::giveawayGiftCodeTableMargin);
  1173. const auto peerId = PeerId(entry.barePeerId);
  1174. const auto session = &show->session();
  1175. const auto unique = entry.uniqueGift.get();
  1176. const auto selfBareId = session->userPeerId().value;
  1177. const auto giftToSelf = (peerId == session->userPeerId())
  1178. && (entry.in || entry.bareGiftOwnerId == selfBareId);
  1179. const auto giftToChannel = entry.giftChannelSavedId
  1180. && peerIsChannel(PeerId(entry.bareEntryOwnerId));
  1181. const auto raw = std::make_shared<Ui::ImportantTooltip*>(nullptr);
  1182. const auto showTooltip = [=](
  1183. not_null<Ui::RpWidget*> widget,
  1184. rpl::producer<TextWithEntities> text) {
  1185. if (*raw) {
  1186. (*raw)->toggleAnimated(false);
  1187. }
  1188. const auto tooltip = Ui::CreateChild<Ui::ImportantTooltip>(
  1189. container,
  1190. Ui::MakeNiceTooltipLabel(
  1191. container,
  1192. std::move(text),
  1193. st::boxWideWidth,
  1194. st::defaultImportantTooltipLabel),
  1195. st::defaultImportantTooltip);
  1196. tooltip->toggleFast(false);
  1197. const auto update = [=] {
  1198. const auto geometry = Ui::MapFrom(
  1199. container,
  1200. widget,
  1201. widget->rect());
  1202. const auto countPosition = [=](QSize size) {
  1203. const auto left = geometry.x()
  1204. + (geometry.width() - size.width()) / 2;
  1205. const auto right = container->width()
  1206. - st::normalFont->spacew;
  1207. return QPoint(
  1208. std::max(std::min(left, right - size.width()), 0),
  1209. geometry.y() - size.height() - st::normalFont->descent);
  1210. };
  1211. tooltip->pointAt(geometry, RectPart::Top, countPosition);
  1212. };
  1213. container->widthValue(
  1214. ) | rpl::start_with_next(update, tooltip->lifetime());
  1215. update();
  1216. tooltip->toggleAnimated(true);
  1217. *raw = tooltip;
  1218. tooltip->shownValue() | rpl::filter(
  1219. !rpl::mappers::_1
  1220. ) | rpl::start_with_next([=] {
  1221. crl::on_main(tooltip, [=] {
  1222. if (tooltip->isHidden()) {
  1223. if (*raw == tooltip) {
  1224. *raw = nullptr;
  1225. }
  1226. delete tooltip;
  1227. }
  1228. });
  1229. }, tooltip->lifetime());
  1230. base::timer_once(
  1231. kRarityTooltipDuration
  1232. ) | rpl::start_with_next([=] {
  1233. tooltip->toggleAnimated(false);
  1234. }, tooltip->lifetime());
  1235. };
  1236. if (unique && entry.bareGiftOwnerId) {
  1237. const auto ownerId = PeerId(entry.bareGiftOwnerId);
  1238. const auto was = std::make_shared<std::optional<CollectibleId>>();
  1239. const auto handleChange = [=](
  1240. not_null<Ui::RpWidget*> badge,
  1241. EmojiStatusId emojiStatusId) {
  1242. const auto id = emojiStatusId.collectible
  1243. ? emojiStatusId.collectible->id
  1244. : 0;
  1245. const auto show = [&](const auto &phrase) {
  1246. showTooltip(badge, phrase(
  1247. lt_name,
  1248. rpl::single(Ui::Text::Bold(UniqueGiftName(*unique))),
  1249. Ui::Text::WithEntities));
  1250. };
  1251. if (!*was || *was == id) {
  1252. *was = id;
  1253. return;
  1254. } else if (*was == unique->id) {
  1255. show(tr::lng_gift_wear_end_toast);
  1256. } else if (id == unique->id) {
  1257. show(tr::lng_gift_wear_start_toast);
  1258. }
  1259. *was = id;
  1260. };
  1261. AddTableRow(
  1262. table,
  1263. tr::lng_gift_unique_owner(),
  1264. MakePeerWithStatusValue(table, show, ownerId, handleChange),
  1265. st::giveawayGiftCodePeerMargin);
  1266. } else if (unique) {
  1267. if (!unique->ownerName.isEmpty()) {
  1268. AddTableRow(
  1269. table,
  1270. tr::lng_gift_unique_owner(),
  1271. rpl::single(TextWithEntities{ unique->ownerName }));
  1272. } else if (auto address = unique->ownerAddress; !address.isEmpty()) {
  1273. auto label = MakeMaybeMultilineTokenValue(table, address, st);
  1274. label->setClickHandlerFilter([=](const auto &...) {
  1275. TextUtilities::SetClipboardText(
  1276. TextForMimeData::Simple(address));
  1277. show->showToast(
  1278. tr::lng_gift_unique_address_copied(tr::now));
  1279. return false;
  1280. });
  1281. AddTableRow(
  1282. table,
  1283. tr::lng_gift_unique_owner(),
  1284. std::move(label),
  1285. st::giveawayGiftCodeValueMargin);
  1286. }
  1287. } else if (giftToChannel) {
  1288. AddTableRow(
  1289. table,
  1290. tr::lng_credits_box_history_entry_peer_in(),
  1291. (entry.bareActorId
  1292. ? MakePeerTableValue(table, show, PeerId(entry.bareActorId))
  1293. : MakeHiddenPeerTableValue(table)),
  1294. st::giveawayGiftCodePeerMargin);
  1295. if (entry.bareEntryOwnerId) {
  1296. AddTableRow(
  1297. table,
  1298. tr::lng_credits_box_history_entry_peer(),
  1299. MakePeerTableValue(
  1300. table,
  1301. show,
  1302. PeerId(entry.bareEntryOwnerId)),
  1303. st::giveawayGiftCodePeerMargin);
  1304. }
  1305. } else if (peerId && !giftToSelf) {
  1306. const auto user = session->data().peer(peerId)->asUser();
  1307. const auto withSendButton = entry.in && user && !user->isBot();
  1308. auto send = withSendButton ? tr::lng_gift_send_small() : nullptr;
  1309. auto handler = send ? Fn<void()>([=] {
  1310. if (const auto window = show->resolveWindow()) {
  1311. Ui::ShowStarGiftBox(window, user);
  1312. }
  1313. }) : nullptr;
  1314. AddTableRow(
  1315. table,
  1316. tr::lng_credits_box_history_entry_peer_in(),
  1317. MakePeerTableValue(table, show, peerId, send, handler),
  1318. st::giveawayGiftCodePeerMargin);
  1319. } else if (!entry.soldOutInfo) {
  1320. AddTableRow(
  1321. table,
  1322. tr::lng_credits_box_history_entry_peer_in(),
  1323. MakeHiddenPeerTableValue(table),
  1324. st::giveawayGiftCodePeerMargin);
  1325. }
  1326. if (!unique && !entry.firstSaleDate.isNull()) {
  1327. AddTableRow(
  1328. table,
  1329. tr::lng_gift_link_label_first_sale(),
  1330. rpl::single(Ui::Text::WithEntities(
  1331. langDateTime(entry.firstSaleDate))));
  1332. }
  1333. if (!unique && !entry.lastSaleDate.isNull()) {
  1334. AddTableRow(
  1335. table,
  1336. tr::lng_gift_link_label_last_sale(),
  1337. rpl::single(Ui::Text::WithEntities(
  1338. langDateTime(entry.lastSaleDate))));
  1339. }
  1340. if (!unique && !entry.date.isNull()) {
  1341. AddTableRow(
  1342. table,
  1343. tr::lng_gift_link_label_date(),
  1344. rpl::single(Ui::Text::WithEntities(langDateTime(entry.date))));
  1345. }
  1346. const auto marginWithButton = st::giveawayGiftCodeValueMargin
  1347. - QMargins(0, 0, 0, st::giveawayGiftCodeValueMargin.bottom());
  1348. if (unique) {
  1349. const auto showRarity = [=](
  1350. not_null<Ui::RpWidget*> widget,
  1351. int rarity) {
  1352. const auto percent = QString::number(rarity / 10.) + '%';
  1353. showTooltip(widget, tr::lng_gift_unique_rarity(
  1354. lt_percent,
  1355. rpl::single(TextWithEntities{ percent }),
  1356. Ui::Text::WithEntities));
  1357. };
  1358. AddTableRow(
  1359. table,
  1360. tr::lng_gift_unique_model(),
  1361. MakeAttributeValue(table, unique->model, showRarity),
  1362. marginWithButton);
  1363. AddTableRow(
  1364. table,
  1365. tr::lng_gift_unique_backdrop(),
  1366. MakeAttributeValue(table, unique->backdrop, showRarity),
  1367. marginWithButton);
  1368. AddTableRow(
  1369. table,
  1370. tr::lng_gift_unique_symbol(),
  1371. MakeAttributeValue(table, unique->pattern, showRarity),
  1372. marginWithButton);
  1373. } else {
  1374. AddTableRow(
  1375. table,
  1376. tr::lng_gift_link_label_value(),
  1377. MakeStarGiftStarsValue(
  1378. table,
  1379. show,
  1380. entry,
  1381. std::move(convertToStars)),
  1382. marginWithButton);
  1383. }
  1384. if (entry.limitedCount > 0 && !entry.giftRefunded) {
  1385. auto amount = rpl::single(TextWithEntities{
  1386. Lang::FormatCountDecimal(entry.limitedCount)
  1387. });
  1388. const auto count = unique
  1389. ? (entry.limitedCount - entry.limitedLeft)
  1390. : entry.limitedLeft;
  1391. AddTableRow(
  1392. table,
  1393. (unique
  1394. ? tr::lng_gift_unique_availability_label()
  1395. : tr::lng_gift_availability()),
  1396. ((!unique && !count)
  1397. ? tr::lng_gift_availability_none(
  1398. lt_amount,
  1399. std::move(amount),
  1400. Ui::Text::WithEntities)
  1401. : (unique
  1402. ? tr::lng_gift_unique_availability
  1403. : tr::lng_gift_availability_left)(
  1404. lt_count_decimal,
  1405. rpl::single(count * 1.),
  1406. lt_amount,
  1407. std::move(amount),
  1408. Ui::Text::WithEntities)));
  1409. }
  1410. if (!unique && !entry.soldOutInfo && startUpgrade) {
  1411. AddTableRow(
  1412. table,
  1413. tr::lng_gift_unique_status(),
  1414. MakeNonUniqueStatusTableValue(table, std::move(startUpgrade)),
  1415. marginWithButton);
  1416. }
  1417. if (unique) {
  1418. const auto &original = unique->originalDetails;
  1419. if (original.recipientId) {
  1420. const auto owner = &show->session().data();
  1421. const auto to = owner->peer(original.recipientId);
  1422. const auto from = original.senderId
  1423. ? owner->peer(original.senderId).get()
  1424. : nullptr;
  1425. const auto date = base::unixtime::parse(original.date).date();
  1426. const auto dateText = TextWithEntities{ langDayOfMonth(date) };
  1427. auto label = object_ptr<Ui::FlatLabel>(
  1428. table,
  1429. (from
  1430. ? (original.message.empty()
  1431. ? tr::lng_gift_unique_info_sender(
  1432. lt_from,
  1433. rpl::single(Ui::Text::Link(from->name(), 2)),
  1434. lt_recipient,
  1435. rpl::single(Ui::Text::Link(to->name(), 1)),
  1436. lt_date,
  1437. rpl::single(dateText),
  1438. Ui::Text::WithEntities)
  1439. : tr::lng_gift_unique_info_sender_comment(
  1440. lt_from,
  1441. rpl::single(Ui::Text::Link(from->name(), 2)),
  1442. lt_recipient,
  1443. rpl::single(Ui::Text::Link(to->name(), 1)),
  1444. lt_date,
  1445. rpl::single(dateText),
  1446. lt_text,
  1447. rpl::single(original.message),
  1448. Ui::Text::WithEntities))
  1449. : (original.message.empty()
  1450. ? tr::lng_gift_unique_info_reciever(
  1451. lt_recipient,
  1452. rpl::single(Ui::Text::Link(to->name(), 1)),
  1453. lt_date,
  1454. rpl::single(dateText),
  1455. Ui::Text::WithEntities)
  1456. : tr::lng_gift_unique_info_reciever_comment(
  1457. lt_recipient,
  1458. rpl::single(Ui::Text::Link(to->name(), 1)),
  1459. lt_date,
  1460. rpl::single(dateText),
  1461. lt_text,
  1462. rpl::single(original.message),
  1463. Ui::Text::WithEntities))),
  1464. (st.tableValueMessage
  1465. ? *st.tableValueMessage
  1466. : st::giveawayGiftMessage),
  1467. st::defaultPopupMenu,
  1468. Core::TextContext({ .session = session }));
  1469. const auto showBoxLink = [=](not_null<PeerData*> peer) {
  1470. return std::make_shared<LambdaClickHandler>([=] {
  1471. show->showBox(PrepareShortInfoBox(peer, show));
  1472. });
  1473. };
  1474. label->setLink(1, showBoxLink(to));
  1475. if (from) {
  1476. label->setLink(2, showBoxLink(from));
  1477. }
  1478. label->setSelectable(true);
  1479. table->addRow(
  1480. std::move(label),
  1481. nullptr,
  1482. st::giveawayGiftCodeLabelMargin,
  1483. st::giveawayGiftCodeValueMargin);
  1484. }
  1485. } else if (!entry.description.empty()) {
  1486. auto label = object_ptr<Ui::FlatLabel>(
  1487. table,
  1488. rpl::single(entry.description),
  1489. (st.tableValueMessage
  1490. ? *st.tableValueMessage
  1491. : st::giveawayGiftMessage),
  1492. st::defaultPopupMenu,
  1493. Core::TextContext({ .session = session }));
  1494. label->setSelectable(true);
  1495. table->addRow(
  1496. nullptr,
  1497. std::move(label),
  1498. st::giveawayGiftCodeLabelMargin,
  1499. st::giveawayGiftCodeValueMargin);
  1500. }
  1501. }
  1502. void AddCreditsHistoryEntryTable(
  1503. std::shared_ptr<ChatHelpers::Show> show,
  1504. not_null<Ui::VerticalLayout*> container,
  1505. Settings::CreditsEntryBoxStyleOverrides st,
  1506. const Data::CreditsHistoryEntry &entry) {
  1507. if (!entry) {
  1508. return;
  1509. }
  1510. auto table = container->add(
  1511. object_ptr<Ui::TableLayout>(
  1512. container,
  1513. st.table ? *st.table : st::giveawayGiftCodeTable),
  1514. st::giveawayGiftCodeTableMargin);
  1515. const auto peerId = PeerId(entry.barePeerId);
  1516. const auto actorId = PeerId(entry.bareActorId);
  1517. const auto starrefRecipientId = PeerId(entry.starrefRecipientId);
  1518. const auto session = &show->session();
  1519. if (entry.starrefCommission) {
  1520. if (entry.starrefAmount) {
  1521. AddTableRow(
  1522. table,
  1523. tr::lng_star_ref_commission_title(),
  1524. rpl::single(TextWithEntities{
  1525. QString::number(entry.starrefCommission / 10.) + '%' }));
  1526. } else {
  1527. AddTableRow(
  1528. table,
  1529. tr::lng_gift_link_label_reason(),
  1530. tr::lng_credits_box_history_entry_reason_star_ref(
  1531. Ui::Text::WithEntities));
  1532. }
  1533. }
  1534. if (starrefRecipientId && entry.starrefAmount) {
  1535. AddTableRow(
  1536. table,
  1537. tr::lng_credits_box_history_entry_affiliate(),
  1538. show,
  1539. starrefRecipientId);
  1540. }
  1541. if (peerId && entry.starrefCommission) {
  1542. AddTableRow(
  1543. table,
  1544. (entry.starrefAmount
  1545. ? tr::lng_credits_box_history_entry_referred
  1546. : tr::lng_credits_box_history_entry_miniapp)(),
  1547. show,
  1548. peerId);
  1549. }
  1550. if (actorId || (!entry.starrefCommission && peerId)) {
  1551. auto text = entry.starrefCommission
  1552. ? tr::lng_credits_box_history_entry_referred()
  1553. : entry.in
  1554. ? tr::lng_credits_box_history_entry_peer_in()
  1555. : entry.giftUpgraded
  1556. ? tr::lng_credits_box_history_entry_gift_from()
  1557. : tr::lng_credits_box_history_entry_peer();
  1558. AddTableRow(
  1559. table,
  1560. std::move(text),
  1561. show,
  1562. actorId ? actorId : peerId);
  1563. }
  1564. if (const auto msgId = MsgId(peerId ? entry.bareMsgId : 0)) {
  1565. const auto peer = session->data().peer(peerId);
  1566. if (const auto channel = peer->asBroadcast()) {
  1567. const auto link = CreateMessageLink(
  1568. session,
  1569. peerId,
  1570. entry.bareMsgId);
  1571. auto label = object_ptr<Ui::FlatLabel>(
  1572. table,
  1573. rpl::single(Ui::Text::Link(link)),
  1574. table->st().defaultValue);
  1575. label->setClickHandlerFilter([=](const auto &...) {
  1576. if (const auto window = show->resolveWindow()) {
  1577. window->showPeerHistory(channel, {}, msgId);
  1578. }
  1579. return false;
  1580. });
  1581. AddTableRow(
  1582. table,
  1583. (entry.reaction
  1584. ? tr::lng_credits_box_history_entry_message
  1585. : tr::lng_credits_box_history_entry_media)(),
  1586. std::move(label),
  1587. st::giveawayGiftCodeValueMargin);
  1588. }
  1589. }
  1590. using Type = Data::CreditsHistoryEntry::PeerType;
  1591. if (entry.peerType == Type::AppStore) {
  1592. AddTableRow(
  1593. table,
  1594. tr::lng_credits_box_history_entry_via(),
  1595. tr::lng_credits_box_history_entry_app_store(
  1596. Ui::Text::RichLangValue));
  1597. } else if (entry.peerType == Type::PlayMarket) {
  1598. AddTableRow(
  1599. table,
  1600. tr::lng_credits_box_history_entry_via(),
  1601. tr::lng_credits_box_history_entry_play_market(
  1602. Ui::Text::RichLangValue));
  1603. } else if (entry.peerType == Type::Fragment) {
  1604. AddTableRow(
  1605. table,
  1606. (entry.gift
  1607. ? tr::lng_credits_box_history_entry_peer_in
  1608. : tr::lng_credits_box_history_entry_via)(),
  1609. (entry.gift
  1610. ? tr::lng_credits_box_history_entry_anonymous
  1611. : tr::lng_credits_box_history_entry_fragment)(
  1612. Ui::Text::RichLangValue));
  1613. } else if (entry.peerType == Type::Ads) {
  1614. AddTableRow(
  1615. table,
  1616. tr::lng_credits_box_history_entry_via(),
  1617. tr::lng_credits_box_history_entry_ads(Ui::Text::RichLangValue));
  1618. } else if (entry.peerType == Type::PremiumBot) {
  1619. AddTableRow(
  1620. table,
  1621. tr::lng_credits_box_history_entry_via(),
  1622. tr::lng_credits_box_history_entry_via_premium_bot(
  1623. Ui::Text::RichLangValue));
  1624. }
  1625. if (entry.bareGiveawayMsgId) {
  1626. AddTableRow(
  1627. table,
  1628. tr::lng_gift_link_label_to(),
  1629. show,
  1630. show->session().userId());
  1631. }
  1632. if (entry.bareGiveawayMsgId && entry.credits) {
  1633. AddTableRow(
  1634. table,
  1635. tr::lng_gift_link_label_gift(),
  1636. tr::lng_gift_stars_title(
  1637. lt_count,
  1638. rpl::single(entry.credits.value()),
  1639. Ui::Text::RichLangValue));
  1640. }
  1641. {
  1642. const auto link = CreateMessageLink(
  1643. session,
  1644. peerId,
  1645. entry.bareGiveawayMsgId);
  1646. if (!link.isEmpty()) {
  1647. AddTableRow(
  1648. table,
  1649. tr::lng_gift_link_label_reason(),
  1650. tr::lng_gift_link_reason_giveaway(
  1651. ) | rpl::map([link](const QString &text) {
  1652. return Ui::Text::Link(text, link);
  1653. }));
  1654. }
  1655. }
  1656. if (!entry.subscriptionUntil.isNull() && !entry.title.isEmpty()) {
  1657. AddTableRow(
  1658. table,
  1659. tr::lng_gift_link_label_reason(),
  1660. tr::lng_credits_box_history_entry_subscription(
  1661. Ui::Text::WithEntities));
  1662. }
  1663. if (entry.paidMessagesAmount) {
  1664. auto value = Ui::Text::IconEmoji(&st::starIconEmojiColored);
  1665. const auto full = (entry.in ? 1 : -1)
  1666. * (entry.credits + entry.paidMessagesAmount);
  1667. const auto starsText = Lang::FormatStarsAmountDecimal(full);
  1668. AddTableRow(
  1669. table,
  1670. tr::lng_credits_paid_messages_full(),
  1671. rpl::single(value.append(' ' + starsText)));
  1672. }
  1673. if (const auto months = entry.premiumMonthsForStars) {
  1674. AddTableRow(
  1675. table,
  1676. tr::lng_credits_premium_gift_duration(),
  1677. tr::lng_months(
  1678. lt_count,
  1679. rpl::single(1. * months),
  1680. Ui::Text::WithEntities));
  1681. }
  1682. if (!entry.id.isEmpty()) {
  1683. auto label = MakeMaybeMultilineTokenValue(table, entry.id, st);
  1684. label->setClickHandlerFilter([=](const auto &...) {
  1685. TextUtilities::SetClipboardText(
  1686. TextForMimeData::Simple(entry.id));
  1687. show->showToast(
  1688. tr::lng_credits_box_history_entry_id_copied(tr::now));
  1689. return false;
  1690. });
  1691. AddTableRow(
  1692. table,
  1693. tr::lng_credits_box_history_entry_id(),
  1694. std::move(label),
  1695. st::giveawayGiftCodeValueMargin);
  1696. }
  1697. if (entry.floodSkip) {
  1698. AddTableRow(
  1699. table,
  1700. tr::lng_credits_box_history_entry_floodskip_row(),
  1701. rpl::single(
  1702. Ui::Text::WithEntities(
  1703. Lang::FormatCountDecimal(entry.floodSkip))));
  1704. }
  1705. if (!entry.date.isNull()) {
  1706. AddTableRow(
  1707. table,
  1708. tr::lng_gift_link_label_date(),
  1709. rpl::single(Ui::Text::WithEntities(langDateTime(entry.date))));
  1710. }
  1711. if (!entry.successDate.isNull()) {
  1712. AddTableRow(
  1713. table,
  1714. tr::lng_credits_box_history_entry_success_date(),
  1715. rpl::single(Ui::Text::WithEntities(langDateTime(entry.date))));
  1716. }
  1717. if (!entry.successLink.isEmpty()) {
  1718. AddTableRow(
  1719. table,
  1720. tr::lng_credits_box_history_entry_success_url(),
  1721. rpl::single(
  1722. Ui::Text::Link(entry.successLink, entry.successLink)));
  1723. }
  1724. }
  1725. void AddSubscriptionEntryTable(
  1726. std::shared_ptr<ChatHelpers::Show> show,
  1727. not_null<Ui::VerticalLayout*> container,
  1728. Settings::CreditsEntryBoxStyleOverrides st,
  1729. const Data::SubscriptionEntry &s) {
  1730. if (!s) {
  1731. return;
  1732. }
  1733. auto table = container->add(
  1734. object_ptr<Ui::TableLayout>(
  1735. container,
  1736. st.table ? *st.table : st::giveawayGiftCodeTable),
  1737. st::giveawayGiftCodeTableMargin);
  1738. const auto peerId = PeerId(s.barePeerId);
  1739. const auto user = peerIsUser(peerId)
  1740. ? show->session().data().peer(peerId)->asUser()
  1741. : nullptr;
  1742. AddTableRow(
  1743. table,
  1744. (!s.title.isEmpty() && user && user->botInfo)
  1745. ? tr::lng_credits_subscription_row_to_bot()
  1746. : (!s.title.isEmpty() && user && !user->botInfo)
  1747. ? tr::lng_credits_subscription_row_to_business()
  1748. : tr::lng_credits_subscription_row_to(),
  1749. show,
  1750. peerId);
  1751. if (!s.title.isEmpty()) {
  1752. AddTableRow(
  1753. table,
  1754. tr::lng_credits_subscription_row_to(),
  1755. rpl::single(Ui::Text::WithEntities(s.title)));
  1756. }
  1757. if (!s.until.isNull()) {
  1758. if (s.subscription.period > 0) {
  1759. const auto subscribed = s.until.addSecs(-s.subscription.period);
  1760. if (subscribed.isValid()) {
  1761. AddTableRow(
  1762. table,
  1763. tr::lng_group_invite_joined_row_date(),
  1764. rpl::single(
  1765. Ui::Text::WithEntities(langDateTime(subscribed))));
  1766. }
  1767. }
  1768. AddTableRow(
  1769. table,
  1770. s.expired
  1771. ? tr::lng_credits_subscription_row_next_none()
  1772. : s.cancelled
  1773. ? tr::lng_credits_subscription_row_next_off()
  1774. : tr::lng_credits_subscription_row_next_on(),
  1775. rpl::single(Ui::Text::WithEntities(langDateTime(s.until))));
  1776. }
  1777. }
  1778. void AddSubscriberEntryTable(
  1779. std::shared_ptr<ChatHelpers::Show> show,
  1780. not_null<Ui::VerticalLayout*> container,
  1781. Settings::CreditsEntryBoxStyleOverrides st,
  1782. not_null<PeerData*> peer,
  1783. TimeId date) {
  1784. auto table = container->add(
  1785. object_ptr<Ui::TableLayout>(
  1786. container,
  1787. st.table ? *st.table : st::giveawayGiftCodeTable),
  1788. st::giveawayGiftCodeTableMargin);
  1789. AddTableRow(
  1790. table,
  1791. tr::lng_group_invite_joined_row_subscriber(),
  1792. show,
  1793. peer->id);
  1794. if (const auto d = base::unixtime::parse(date); !d.isNull()) {
  1795. AddTableRow(
  1796. table,
  1797. tr::lng_group_invite_joined_row_date(),
  1798. rpl::single(Ui::Text::WithEntities(langDateTime(d))));
  1799. }
  1800. }
  1801. void AddCreditsBoostTable(
  1802. std::shared_ptr<ChatHelpers::Show> show,
  1803. not_null<Ui::VerticalLayout*> container,
  1804. Settings::CreditsEntryBoxStyleOverrides st,
  1805. const Data::Boost &b) {
  1806. auto table = container->add(
  1807. object_ptr<Ui::TableLayout>(
  1808. container,
  1809. st.table ? *st.table : st::giveawayGiftCodeTable),
  1810. st::giveawayGiftCodeTableMargin);
  1811. const auto peerId = b.giveawayMessage.peer;
  1812. if (!peerId) {
  1813. return;
  1814. }
  1815. const auto from = show->session().data().peer(peerId);
  1816. AddTableRow(
  1817. table,
  1818. tr::lng_credits_box_history_entry_peer_in(),
  1819. show,
  1820. from->id);
  1821. if (b.credits) {
  1822. AddTableRow(
  1823. table,
  1824. tr::lng_gift_link_label_gift(),
  1825. tr::lng_gift_stars_title(
  1826. lt_count,
  1827. rpl::single(float64(b.credits)),
  1828. Ui::Text::RichLangValue));
  1829. }
  1830. {
  1831. const auto link = CreateMessageLink(
  1832. &show->session(),
  1833. peerId,
  1834. b.giveawayMessage.msg.bare);
  1835. if (!link.isEmpty()) {
  1836. AddTableRow(
  1837. table,
  1838. tr::lng_gift_link_label_reason(),
  1839. tr::lng_gift_link_reason_giveaway(
  1840. ) | rpl::map([link](const QString &text) {
  1841. return Ui::Text::Link(text, link);
  1842. }));
  1843. }
  1844. }
  1845. if (!b.date.isNull()) {
  1846. AddTableRow(
  1847. table,
  1848. tr::lng_gift_link_label_date(),
  1849. rpl::single(Ui::Text::WithEntities(langDateTime(b.date))));
  1850. }
  1851. if (!b.expiresAt.isNull()) {
  1852. AddTableRow(
  1853. table,
  1854. tr::lng_gift_until(),
  1855. rpl::single(Ui::Text::WithEntities(langDateTime(b.expiresAt))));
  1856. }
  1857. }