| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800 |
- /*
- This file is part of Telegram Desktop,
- the official desktop application for the Telegram messaging service.
- For license and copyright information please follow this link:
- https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
- */
- #include "apiwrap.h"
- #include "core/wallet_replacer.h"
- #include "api/api_authorizations.h"
- #include "api/api_attached_stickers.h"
- #include "api/api_blocked_peers.h"
- #include "api/api_chat_links.h"
- #include "api/api_chat_participants.h"
- #include "api/api_cloud_password.h"
- #include "api/api_hash.h"
- #include "api/api_invite_links.h"
- #include "api/api_media.h"
- #include "api/api_peer_colors.h"
- #include "api/api_peer_photo.h"
- #include "api/api_polls.h"
- #include "api/api_sending.h"
- #include "api/api_text_entities.h"
- #include "api/api_self_destruct.h"
- #include "api/api_sensitive_content.h"
- #include "api/api_global_privacy.h"
- #include "api/api_updates.h"
- #include "api/api_user_privacy.h"
- #include "api/api_views.h"
- #include "api/api_confirm_phone.h"
- #include "api/api_unread_things.h"
- #include "api/api_ringtones.h"
- #include "api/api_transcribes.h"
- #include "api/api_premium.h"
- #include "api/api_user_names.h"
- #include "api/api_websites.h"
- #include "data/business/data_shortcut_messages.h"
- #include "data/components/scheduled_messages.h"
- #include "data/notify/data_notify_settings.h"
- #include "data/data_changes.h"
- #include "data/data_web_page.h"
- #include "data/data_folder.h"
- #include "data/data_forum_topic.h"
- #include "data/data_forum.h"
- #include "data/data_saved_sublist.h"
- #include "data/data_search_controller.h"
- #include "data/data_session.h"
- #include "data/data_channel.h"
- #include "data/data_chat.h"
- #include "data/data_user.h"
- #include "data/data_chat_filters.h"
- #include "data/data_histories.h"
- #include "data/data_history_messages.h"
- #include "core/core_cloud_password.h"
- #include "core/application.h"
- #include "base/unixtime.h"
- #include "base/random.h"
- #include "base/call_delayed.h"
- #include "lang/lang_keys.h"
- #include "mainwidget.h"
- #include "boxes/add_contact_box.h"
- #include "mtproto/mtproto_config.h"
- #include "history/history.h"
- #include "history/history_item_components.h"
- #include "history/history_item_helpers.h"
- #include "main/main_session.h"
- #include "main/main_session_settings.h"
- #include "main/main_account.h"
- #include "ui/boxes/confirm_box.h"
- #include "boxes/sticker_set_box.h"
- #include "boxes/premium_limits_box.h"
- #include "window/notifications_manager.h"
- #include "window/window_controller.h"
- #include "window/window_lock_widgets.h"
- #include "window/window_session_controller.h"
- #include "inline_bots/inline_bot_result.h"
- #include "chat_helpers/message_field.h"
- #include "ui/item_text_options.h"
- #include "ui/text/text_utilities.h"
- #include "ui/chat/attach/attach_prepare.h"
- #include "ui/toast/toast.h"
- #include "support/support_helper.h"
- #include "settings/settings_premium.h"
- #include "storage/localimageloader.h"
- #include "storage/download_manager_mtproto.h"
- #include "storage/file_upload.h"
- #include "storage/storage_account.h"
- namespace {
- // Save draft to the cloud with 1 sec extra delay.
- constexpr auto kSaveCloudDraftTimeout = 1000;
- constexpr auto kTopPromotionInterval = TimeId(60 * 60);
- constexpr auto kTopPromotionMinDelay = TimeId(10);
- constexpr auto kSmallDelayMs = 5;
- constexpr auto kReadFeaturedSetsTimeout = crl::time(1000);
- constexpr auto kFileLoaderQueueStopTimeout = crl::time(5000);
- constexpr auto kStickersByEmojiInvalidateTimeout = crl::time(6 * 1000);
- constexpr auto kNotifySettingSaveTimeout = crl::time(1000);
- constexpr auto kDialogsFirstLoad = 20;
- constexpr auto kDialogsPerPage = 500;
- constexpr auto kStatsSessionKillTimeout = 10 * crl::time(1000);
- using PhotoFileLocationId = Data::PhotoFileLocationId;
- using DocumentFileLocationId = Data::DocumentFileLocationId;
- using UpdatedFileReferences = Data::UpdatedFileReferences;
- [[nodiscard]] TimeId UnixtimeFromMsgId(mtpMsgId msgId) {
- return TimeId(msgId >> 32);
- }
- [[nodiscard]] std::shared_ptr<ChatHelpers::Show> ShowForPeer(
- not_null<PeerData*> peer) {
- if (const auto window = Core::App().windowFor(peer)) {
- if (const auto controller = window->sessionController()) {
- if (&controller->session() == &peer->session()) {
- return controller->uiShow();
- }
- }
- }
- return nullptr;
- }
- void ShowChannelsLimitBox(not_null<PeerData*> peer) {
- if (const auto window = Core::App().windowFor(peer)) {
- window->invokeForSessionController(
- &peer->session().account(),
- peer,
- [&](not_null<Window::SessionController*> controller) {
- controller->show(Box(ChannelsLimitBox, &peer->session()));
- });
- }
- }
- [[nodiscard]] FileLoadTo FileLoadTaskOptions(const Api::SendAction &action) {
- const auto peer = action.history->peer;
- return FileLoadTo(
- peer->id,
- action.options,
- action.replyTo,
- action.replaceMediaOf);
- }
- [[nodiscard]] QString FormatVideoTimestamp(TimeId seconds) {
- const auto minutes = seconds / 60;
- const auto hours = minutes / 60;
- return hours
- ? u"%1h%2m%3s"_q.arg(hours).arg(minutes % 60).arg(seconds % 60)
- : minutes
- ? u"%1m%2s"_q.arg(minutes).arg(seconds % 60)
- : QString::number(seconds);
- }
- } // namespace
- ApiWrap::ApiWrap(not_null<Main::Session*> session)
- : MTP::Sender(&session->account().mtp())
- , _session(session)
- , _messageDataResolveDelayed([=] { resolveMessageDatas(); })
- , _webPagesTimer([=] { resolveWebPages(); })
- , _draftsSaveTimer([=] { saveDraftsToCloud(); })
- , _featuredSetsReadTimer([=] { readFeaturedSets(); })
- , _dialogsLoadState(std::make_unique<DialogsLoadState>())
- , _fileLoader(std::make_unique<TaskQueue>(kFileLoaderQueueStopTimeout))
- , _topPromotionTimer([=] { refreshTopPromotion(); })
- , _updateNotifyTimer([=] { sendNotifySettingsUpdates(); })
- , _statsSessionKillTimer([=] { checkStatsSessions(); })
- , _authorizations(std::make_unique<Api::Authorizations>(this))
- , _attachedStickers(std::make_unique<Api::AttachedStickers>(this))
- , _blockedPeers(std::make_unique<Api::BlockedPeers>(this))
- , _cloudPassword(std::make_unique<Api::CloudPassword>(this))
- , _selfDestruct(std::make_unique<Api::SelfDestruct>(this))
- , _sensitiveContent(std::make_unique<Api::SensitiveContent>(this))
- , _globalPrivacy(std::make_unique<Api::GlobalPrivacy>(this))
- , _userPrivacy(std::make_unique<Api::UserPrivacy>(this))
- , _inviteLinks(std::make_unique<Api::InviteLinks>(this))
- , _chatLinks(std::make_unique<Api::ChatLinks>(this))
- , _views(std::make_unique<Api::ViewsManager>(this))
- , _confirmPhone(std::make_unique<Api::ConfirmPhone>(this))
- , _peerPhoto(std::make_unique<Api::PeerPhoto>(this))
- , _polls(std::make_unique<Api::Polls>(this))
- , _chatParticipants(std::make_unique<Api::ChatParticipants>(this))
- , _unreadThings(std::make_unique<Api::UnreadThings>(this))
- , _ringtones(std::make_unique<Api::Ringtones>(this))
- , _transcribes(std::make_unique<Api::Transcribes>(this))
- , _premium(std::make_unique<Api::Premium>(this))
- , _usernames(std::make_unique<Api::Usernames>(this))
- , _websites(std::make_unique<Api::Websites>(this))
- , _peerColors(std::make_unique<Api::PeerColors>(this)) {
- crl::on_main(session, [=] {
- // You can't use _session->lifetime() in the constructor,
- // only queued, because it is not constructed yet.
- _session->data().chatsFilters().changed(
- ) | rpl::filter([=] {
- return _session->data().chatsFilters().archiveNeeded();
- }) | rpl::start_with_next([=] {
- requestMoreDialogsIfNeeded();
- }, _session->lifetime());
- setupSupportMode();
- Core::App().settings().proxy().connectionTypeValue(
- ) | rpl::start_with_next([=] {
- refreshTopPromotion();
- }, _session->lifetime());
- });
- }
- ApiWrap::~ApiWrap() = default;
- Main::Session &ApiWrap::session() const {
- return *_session;
- }
- Storage::Account &ApiWrap::local() const {
- return _session->local();
- }
- Api::Updates &ApiWrap::updates() const {
- return _session->updates();
- }
- void ApiWrap::setupSupportMode() {
- if (!_session->supportMode()) {
- return;
- }
- _session->settings().supportChatsTimeSliceValue(
- ) | rpl::start_with_next([=](int seconds) {
- _dialogsLoadTill = seconds ? std::max(base::unixtime::now() - seconds, 0) : 0;
- refreshDialogsLoadBlocked();
- }, _session->lifetime());
- }
- void ApiWrap::requestChangelog(
- const QString &sinceVersion,
- Fn<void(const MTPUpdates &result)> callback) {
- //request(MTPhelp_GetAppChangelog(
- // MTP_string(sinceVersion)
- //)).done(
- // callback
- //).send();
- }
- void ApiWrap::refreshTopPromotion() {
- const auto now = base::unixtime::now();
- const auto next = (_topPromotionNextRequestTime != 0)
- ? _topPromotionNextRequestTime
- : now;
- if (_topPromotionRequestId) {
- getTopPromotionDelayed(now, next);
- return;
- }
- const auto key = [&]() -> std::pair<QString, uint32> {
- if (!Core::App().settings().proxy().isEnabled()) {
- return {};
- }
- const auto &proxy = Core::App().settings().proxy().selected();
- if (proxy.type != MTP::ProxyData::Type::Mtproto) {
- return {};
- }
- return { proxy.host, proxy.port };
- }();
- if (_topPromotionKey == key && now < next) {
- getTopPromotionDelayed(now, next);
- return;
- }
- _topPromotionKey = key;
- _topPromotionRequestId = request(MTPhelp_GetPromoData(
- )).done([=](const MTPhelp_PromoData &result) {
- _topPromotionRequestId = 0;
- topPromotionDone(result);
- }).fail([=] {
- _topPromotionRequestId = 0;
- const auto now = base::unixtime::now();
- const auto next = _topPromotionNextRequestTime = now
- + kTopPromotionInterval;
- if (!_topPromotionTimer.isActive()) {
- getTopPromotionDelayed(now, next);
- }
- }).send();
- }
- void ApiWrap::getTopPromotionDelayed(TimeId now, TimeId next) {
- _topPromotionTimer.callOnce(std::min(
- std::max(next - now, kTopPromotionMinDelay),
- kTopPromotionInterval) * crl::time(1000));
- };
- void ApiWrap::topPromotionDone(const MTPhelp_PromoData &proxy) {
- _topPromotionNextRequestTime = proxy.match([&](const auto &data) {
- return data.vexpires().v;
- });
- getTopPromotionDelayed(
- base::unixtime::now(),
- _topPromotionNextRequestTime);
- proxy.match([&](const MTPDhelp_promoDataEmpty &data) {
- _session->data().setTopPromoted(nullptr, QString(), QString());
- }, [&](const MTPDhelp_promoData &data) {
- _session->data().processChats(data.vchats());
- _session->data().processUsers(data.vusers());
- const auto peerId = peerFromMTP(data.vpeer());
- const auto history = _session->data().history(peerId);
- _session->data().setTopPromoted(
- history,
- data.vpsa_type().value_or_empty(),
- data.vpsa_message().value_or_empty());
- });
- }
- void ApiWrap::requestDeepLinkInfo(
- const QString &path,
- Fn<void(TextWithEntities message, bool updateRequired)> callback) {
- request(_deepLinkInfoRequestId).cancel();
- _deepLinkInfoRequestId = request(MTPhelp_GetDeepLinkInfo(
- MTP_string(path)
- )).done([=](const MTPhelp_DeepLinkInfo &result) {
- _deepLinkInfoRequestId = 0;
- if (result.type() == mtpc_help_deepLinkInfo) {
- const auto &data = result.c_help_deepLinkInfo();
- callback(TextWithEntities{
- qs(data.vmessage()),
- Api::EntitiesFromMTP(
- _session,
- data.ventities().value_or_empty())
- }, data.is_update_app());
- }
- }).fail([=] {
- _deepLinkInfoRequestId = 0;
- }).send();
- }
- void ApiWrap::requestTermsUpdate() {
- if (_termsUpdateRequestId) {
- return;
- }
- const auto now = crl::now();
- if (_termsUpdateSendAt && now < _termsUpdateSendAt) {
- base::call_delayed(_termsUpdateSendAt - now, _session, [=] {
- requestTermsUpdate();
- });
- return;
- }
- constexpr auto kTermsUpdateTimeoutMin = 10 * crl::time(1000);
- constexpr auto kTermsUpdateTimeoutMax = 86400 * crl::time(1000);
- _termsUpdateRequestId = request(MTPhelp_GetTermsOfServiceUpdate(
- )).done([=](const MTPhelp_TermsOfServiceUpdate &result) {
- _termsUpdateRequestId = 0;
- const auto requestNext = [&](auto &&data) {
- const auto timeout = (data.vexpires().v - base::unixtime::now());
- _termsUpdateSendAt = crl::now() + std::clamp(
- timeout * crl::time(1000),
- kTermsUpdateTimeoutMin,
- kTermsUpdateTimeoutMax);
- requestTermsUpdate();
- };
- switch (result.type()) {
- case mtpc_help_termsOfServiceUpdateEmpty: {
- const auto &data = result.c_help_termsOfServiceUpdateEmpty();
- requestNext(data);
- } break;
- case mtpc_help_termsOfServiceUpdate: {
- const auto &data = result.c_help_termsOfServiceUpdate();
- const auto &terms = data.vterms_of_service();
- const auto &fields = terms.c_help_termsOfService();
- session().lockByTerms(
- Window::TermsLock::FromMTP(_session, fields));
- requestNext(data);
- } break;
- default: Unexpected("Type in requestTermsUpdate().");
- }
- }).fail([=] {
- _termsUpdateRequestId = 0;
- _termsUpdateSendAt = crl::now() + kTermsUpdateTimeoutMin;
- requestTermsUpdate();
- }).send();
- }
- void ApiWrap::acceptTerms(bytes::const_span id) {
- request(MTPhelp_AcceptTermsOfService(
- MTP_dataJSON(MTP_bytes(id))
- )).done([=] {
- requestTermsUpdate();
- }).send();
- }
- void ApiWrap::checkChatInvite(
- const QString &hash,
- FnMut<void(const MTPChatInvite &)> done,
- Fn<void(const MTP::Error &)> fail) {
- request(base::take(_checkInviteRequestId)).cancel();
- _checkInviteRequestId = request(MTPmessages_CheckChatInvite(
- MTP_string(hash)
- )).done(std::move(done)).fail(std::move(fail)).send();
- }
- void ApiWrap::checkFilterInvite(
- const QString &slug,
- FnMut<void(const MTPchatlists_ChatlistInvite &)> done,
- Fn<void(const MTP::Error &)> fail) {
- request(base::take(_checkFilterInviteRequestId)).cancel();
- _checkFilterInviteRequestId = request(
- MTPchatlists_CheckChatlistInvite(MTP_string(slug))
- ).done(std::move(done)).fail(std::move(fail)).send();
- }
- void ApiWrap::savePinnedOrder(Data::Folder *folder) {
- const auto &order = _session->data().pinnedChatsOrder(folder);
- const auto input = [](Dialogs::Key key) {
- if (const auto history = key.history()) {
- return MTP_inputDialogPeer(history->peer->input);
- } else if (const auto folder = key.folder()) {
- return MTP_inputDialogPeerFolder(MTP_int(folder->id()));
- }
- Unexpected("Key type in pinnedDialogsOrder().");
- };
- auto peers = QVector<MTPInputDialogPeer>();
- peers.reserve(order.size());
- ranges::transform(
- order,
- ranges::back_inserter(peers),
- input);
- request(MTPmessages_ReorderPinnedDialogs(
- MTP_flags(MTPmessages_ReorderPinnedDialogs::Flag::f_force),
- MTP_int(folder ? folder->id() : 0),
- MTP_vector(peers)
- )).send();
- }
- void ApiWrap::savePinnedOrder(not_null<Data::Forum*> forum) {
- const auto &order = _session->data().pinnedChatsOrder(forum);
- const auto input = [](Dialogs::Key key) {
- if (const auto topic = key.topic()) {
- return MTP_int(topic->rootId().bare);
- }
- Unexpected("Key type in pinnedDialogsOrder().");
- };
- auto topics = QVector<MTPint>();
- topics.reserve(order.size());
- ranges::transform(
- order,
- ranges::back_inserter(topics),
- input);
- request(MTPchannels_ReorderPinnedForumTopics(
- MTP_flags(MTPchannels_ReorderPinnedForumTopics::Flag::f_force),
- forum->channel()->inputChannel,
- MTP_vector(topics)
- )).done([=](const MTPUpdates &result) {
- applyUpdates(result);
- }).send();
- }
- void ApiWrap::savePinnedOrder(not_null<Data::SavedMessages*> saved) {
- const auto &order = _session->data().pinnedChatsOrder(saved);
- const auto input = [](Dialogs::Key key) {
- if (const auto sublist = key.sublist()) {
- return MTP_inputDialogPeer(sublist->peer()->input);
- }
- Unexpected("Key type in pinnedDialogsOrder().");
- };
- auto peers = QVector<MTPInputDialogPeer>();
- peers.reserve(order.size());
- ranges::transform(
- order,
- ranges::back_inserter(peers),
- input);
- request(MTPmessages_ReorderPinnedSavedDialogs(
- MTP_flags(MTPmessages_ReorderPinnedSavedDialogs::Flag::f_force),
- MTP_vector(peers)
- )).send();
- }
- void ApiWrap::toggleHistoryArchived(
- not_null<History*> history,
- bool archived,
- Fn<void()> callback) {
- if (const auto already = _historyArchivedRequests.take(history)) {
- request(already->first).cancel();
- }
- const auto isPinned = history->isPinnedDialog(0);
- const auto archiveId = Data::Folder::kId;
- const auto requestId = request(MTPfolders_EditPeerFolders(
- MTP_vector<MTPInputFolderPeer>(
- 1,
- MTP_inputFolderPeer(
- history->peer->input,
- MTP_int(archived ? archiveId : 0)))
- )).done([=](const MTPUpdates &result) {
- applyUpdates(result);
- if (archived) {
- history->setFolder(_session->data().folder(archiveId));
- } else {
- history->clearFolder();
- }
- if (const auto data = _historyArchivedRequests.take(history)) {
- data->second();
- }
- if (isPinned) {
- _session->data().notifyPinnedDialogsOrderUpdated();
- }
- }).fail([=] {
- _historyArchivedRequests.remove(history);
- }).send();
- _historyArchivedRequests.emplace(history, requestId, callback);
- }
- void ApiWrap::sendMessageFail(
- const MTP::Error &error,
- not_null<PeerData*> peer,
- uint64 randomId,
- FullMsgId itemId) {
- sendMessageFail(error.type(), peer, randomId, itemId);
- }
- void ApiWrap::sendMessageFail(
- const QString &error,
- not_null<PeerData*> peer,
- uint64 randomId,
- FullMsgId itemId) {
- const auto show = ShowForPeer(peer);
- const auto paidStarsPrefix = u"ALLOW_PAYMENT_REQUIRED_"_q;
- if (show && error == u"PEER_FLOOD"_q) {
- show->showBox(
- Ui::MakeInformBox(
- PeerFloodErrorText(&session(), PeerFloodType::Send)),
- Ui::LayerOption::CloseOther);
- } else if (show && error == u"USER_BANNED_IN_CHANNEL"_q) {
- const auto link = Ui::Text::Link(
- tr::lng_cant_more_info(tr::now),
- session().createInternalLinkFull(u"spambot"_q));
- show->showBox(
- Ui::MakeInformBox(
- tr::lng_error_public_groups_denied(
- tr::now,
- lt_more_info,
- link,
- Ui::Text::WithEntities)),
- Ui::LayerOption::CloseOther);
- } else if (error.startsWith(u"SLOWMODE_WAIT_"_q)) {
- const auto chop = u"SLOWMODE_WAIT_"_q.size();
- const auto left = base::StringViewMid(error, chop).toInt();
- if (const auto channel = peer->asChannel()) {
- const auto seconds = channel->slowmodeSeconds();
- if (seconds >= left) {
- channel->growSlowmodeLastMessage(
- base::unixtime::now() - (left - seconds));
- } else {
- requestFullPeer(peer);
- }
- }
- } else if (error == u"SCHEDULE_STATUS_PRIVATE"_q) {
- auto &scheduled = _session->scheduledMessages();
- Assert(peer->isUser());
- if (const auto item = scheduled.lookupItem(peer->id, itemId.msg)) {
- scheduled.removeSending(item);
- if (show) {
- show->showBox(
- Ui::MakeInformBox(tr::lng_cant_do_this()),
- Ui::LayerOption::CloseOther);
- }
- }
- } else if (show && error == u"CHAT_FORWARDS_RESTRICTED"_q) {
- show->showToast(peer->isBroadcast()
- ? tr::lng_error_noforwards_channel(tr::now)
- : tr::lng_error_noforwards_group(tr::now), kJoinErrorDuration);
- } else if (error == u"PREMIUM_ACCOUNT_REQUIRED"_q) {
- Settings::ShowPremium(&session(), "premium_stickers");
- } else if (error == u"SCHEDULE_TOO_MUCH"_q) {
- auto &scheduled = _session->scheduledMessages();
- if (const auto item = scheduled.lookupItem(peer->id, itemId.msg)) {
- scheduled.removeSending(item);
- }
- if (show) {
- show->showToast(tr::lng_error_schedule_limit(tr::now));
- }
- } else if (error.startsWith(paidStarsPrefix)) {
- if (show) {
- show->showToast(
- u"Payment requirements changed. Please, try again."_q);
- }
- if (const auto stars = error.mid(paidStarsPrefix.size()).toInt()) {
- if (const auto user = peer->asUser()) {
- user->setStarsPerMessage(stars);
- } else if (const auto channel = peer->asChannel()) {
- channel->setStarsPerMessage(stars);
- }
- }
- peer->updateFull();
- }
- if (const auto item = _session->data().message(itemId)) {
- Assert(randomId != 0);
- _session->data().unregisterMessageRandomId(randomId);
- item->sendFailed();
- if (error == u"TOPIC_CLOSED"_q) {
- if (const auto topic = item->topic()) {
- topic->setClosed(true);
- }
- }
- }
- }
- void ApiWrap::requestMessageData(
- PeerData *peer,
- MsgId msgId,
- Fn<void()> done) {
- auto &requests = (peer && peer->isChannel())
- ? _channelMessageDataRequests[peer->asChannel()][msgId]
- : _messageDataRequests[msgId];
- if (done) {
- requests.callbacks.push_back(std::move(done));
- }
- if (!requests.requestId) {
- _messageDataResolveDelayed.call();
- }
- }
- QVector<MTPInputMessage> ApiWrap::collectMessageIds(
- const MessageDataRequests &requests) {
- auto result = QVector<MTPInputMessage>();
- result.reserve(requests.size());
- for (const auto &[msgId, request] : requests) {
- if (request.requestId > 0) {
- continue;
- }
- result.push_back(MTP_inputMessageID(MTP_int(msgId)));
- }
- return result;
- }
- auto ApiWrap::messageDataRequests(ChannelData *channel, bool onlyExisting)
- -> MessageDataRequests* {
- if (!channel) {
- return &_messageDataRequests;
- }
- const auto i = _channelMessageDataRequests.find(channel);
- if (i != end(_channelMessageDataRequests)) {
- return &i->second;
- } else if (onlyExisting) {
- return nullptr;
- }
- return &_channelMessageDataRequests.emplace(
- channel,
- MessageDataRequests()
- ).first->second;
- }
- void ApiWrap::resolveMessageDatas() {
- if (_messageDataRequests.empty() && _channelMessageDataRequests.empty()) {
- return;
- }
- const auto ids = collectMessageIds(_messageDataRequests);
- if (!ids.isEmpty()) {
- const auto requestId = request(MTPmessages_GetMessages(
- MTP_vector<MTPInputMessage>(ids)
- )).done([=](
- const MTPmessages_Messages &result,
- mtpRequestId requestId) {
- _session->data().processExistingMessages(nullptr, result);
- finalizeMessageDataRequest(nullptr, requestId);
- }).fail([=](const MTP::Error &error, mtpRequestId requestId) {
- finalizeMessageDataRequest(nullptr, requestId);
- }).afterDelay(kSmallDelayMs).send();
- for (auto &[msgId, request] : _messageDataRequests) {
- if (request.requestId > 0) {
- continue;
- }
- request.requestId = requestId;
- }
- }
- for (auto j = _channelMessageDataRequests.begin(); j != _channelMessageDataRequests.cend();) {
- if (j->second.empty()) {
- j = _channelMessageDataRequests.erase(j);
- continue;
- }
- const auto ids = collectMessageIds(j->second);
- if (!ids.isEmpty()) {
- const auto channel = j->first;
- const auto requestId = request(MTPchannels_GetMessages(
- channel->inputChannel,
- MTP_vector<MTPInputMessage>(ids)
- )).done([=](
- const MTPmessages_Messages &result,
- mtpRequestId requestId) {
- _session->data().processExistingMessages(channel, result);
- finalizeMessageDataRequest(channel, requestId);
- }).fail([=](const MTP::Error &error, mtpRequestId requestId) {
- finalizeMessageDataRequest(channel, requestId);
- }).afterDelay(kSmallDelayMs).send();
- for (auto &[msgId, request] : j->second) {
- if (request.requestId > 0) {
- continue;
- }
- request.requestId = requestId;
- }
- }
- ++j;
- }
- }
- void ApiWrap::finalizeMessageDataRequest(
- ChannelData *channel,
- mtpRequestId requestId) {
- auto requests = messageDataRequests(channel, true);
- if (!requests) {
- return;
- }
- auto callbacks = std::vector<Fn<void()>>();
- for (auto i = requests->begin(); i != requests->cend();) {
- if (i->second.requestId == requestId) {
- auto &list = i->second.callbacks;
- if (callbacks.empty()) {
- callbacks = std::move(list);
- } else {
- callbacks.insert(
- end(callbacks),
- std::make_move_iterator(begin(list)),
- std::make_move_iterator(end(list)));
- }
- i = requests->erase(i);
- } else {
- ++i;
- }
- }
- if (channel && requests->empty()) {
- _channelMessageDataRequests.remove(channel);
- }
- for (const auto &callback : callbacks) {
- callback();
- }
- }
- QString ApiWrap::exportDirectMessageLink(
- not_null<HistoryItem*> item,
- bool inRepliesContext,
- bool forceNonPublicLink,
- std::optional<TimeId> videoTimestamp) {
- Expects(item->history()->peer->isChannel());
- const auto itemId = item->fullId();
- const auto channel = item->history()->peer->asChannel();
- const auto fallback = [&] {
- auto linkChannel = channel;
- auto linkItemId = item->id;
- auto linkCommentId = MsgId();
- auto linkThreadId = MsgId();
- auto linkThreadIsTopic = false;
- if (inRepliesContext) {
- linkThreadIsTopic = item->history()->isForum();
- const auto rootId = linkThreadIsTopic
- ? item->topicRootId()
- : item->replyToTop();
- if (rootId) {
- const auto root = item->history()->owner().message(
- channel->id,
- rootId);
- const auto sender = root
- ? root->discussionPostOriginalSender()
- : nullptr;
- if (sender && sender->hasUsername() && !forceNonPublicLink) {
- // Comment to a public channel.
- const auto forwarded = root->Get<HistoryMessageForwarded>();
- linkItemId = forwarded->savedFromMsgId;
- if (linkItemId) {
- linkChannel = sender;
- linkCommentId = item->id;
- } else {
- linkItemId = item->id;
- }
- } else {
- // Reply in a thread, maybe comment in a private channel.
- linkThreadId = rootId;
- }
- }
- }
- const auto base = (linkChannel->hasUsername() && !forceNonPublicLink)
- ? linkChannel->username()
- : "c/" + QString::number(peerToChannel(linkChannel->id).bare);
- const auto post = QString::number(linkItemId.bare);
- const auto query = base
- + '/'
- + (linkCommentId
- ? (post + "?comment=" + QString::number(linkCommentId.bare))
- : (linkThreadId && !linkThreadIsTopic)
- ? (post + "?thread=" + QString::number(linkThreadId.bare))
- : linkThreadId
- ? (QString::number(linkThreadId.bare) + '/' + post)
- : post);
- return session().createInternalLinkFull(query);
- };
- if (forceNonPublicLink) {
- return fallback();
- }
- const auto i = _unlikelyMessageLinks.find(itemId);
- const auto current = (i != end(_unlikelyMessageLinks))
- ? i->second
- : fallback();
- request(MTPchannels_ExportMessageLink(
- MTP_flags(inRepliesContext
- ? MTPchannels_ExportMessageLink::Flag::f_thread
- : MTPchannels_ExportMessageLink::Flag(0)),
- channel->inputChannel,
- MTP_int(item->id)
- )).done([=](const MTPExportedMessageLink &result) {
- const auto link = qs(result.data().vlink());
- if (current != link) {
- _unlikelyMessageLinks.emplace_or_assign(itemId, link);
- }
- }).send();
- const auto addTimestamp = channel->hasUsername()
- && !inRepliesContext
- && videoTimestamp.has_value();
- const auto addedSeparator = (current.indexOf('?') >= 0) ? '&' : '?';
- const auto addedTimestamp = addTimestamp
- ? (addedSeparator + u"t="_q + FormatVideoTimestamp(*videoTimestamp))
- : QString();
- return current + addedTimestamp;
- }
- QString ApiWrap::exportDirectStoryLink(not_null<Data::Story*> story) {
- const auto storyId = story->fullId();
- const auto peer = story->peer();
- const auto fallback = [&] {
- const auto base = peer->username();
- const auto story = QString::number(storyId.story);
- const auto query = base + "/s/" + story;
- return session().createInternalLinkFull(query);
- };
- const auto i = _unlikelyStoryLinks.find(storyId);
- const auto current = (i != end(_unlikelyStoryLinks))
- ? i->second
- : fallback();
- request(MTPstories_ExportStoryLink(
- peer->input,
- MTP_int(story->id())
- )).done([=](const MTPExportedStoryLink &result) {
- const auto link = qs(result.data().vlink());
- if (current != link) {
- _unlikelyStoryLinks.emplace_or_assign(storyId, link);
- }
- }).send();
- return current;
- }
- void ApiWrap::requestContacts() {
- if (_session->data().contactsLoaded().current() || _contactsRequestId) {
- return;
- }
- _contactsRequestId = request(MTPcontacts_GetContacts(
- MTP_long(0) // hash
- )).done([=](const MTPcontacts_Contacts &result) {
- _contactsRequestId = 0;
- if (result.type() == mtpc_contacts_contactsNotModified) {
- return;
- }
- Assert(result.type() == mtpc_contacts_contacts);
- const auto &d = result.c_contacts_contacts();
- _session->data().processUsers(d.vusers());
- for (const auto &contact : d.vcontacts().v) {
- if (contact.type() != mtpc_contact) continue;
- const auto userId = UserId(contact.c_contact().vuser_id());
- if (userId == _session->userId()) {
- _session->user()->setIsContact(true);
- }
- }
- _session->data().contactsLoaded() = true;
- }).fail([=] {
- _contactsRequestId = 0;
- }).send();
- }
- void ApiWrap::requestDialogs(Data::Folder *folder) {
- if (folder && !_foldersLoadState.contains(folder)) {
- _foldersLoadState.emplace(folder, DialogsLoadState());
- }
- requestMoreDialogs(folder);
- }
- void ApiWrap::requestMoreDialogs(Data::Folder *folder) {
- const auto state = dialogsLoadState(folder);
- if (!state) {
- return;
- } else if (state->requestId) {
- return;
- } else if (_dialogsLoadBlockedByDate.current()) {
- return;
- }
- const auto firstLoad = !state->offsetDate;
- const auto loadCount = firstLoad ? kDialogsFirstLoad : kDialogsPerPage;
- const auto flags = MTPmessages_GetDialogs::Flag::f_exclude_pinned
- | MTPmessages_GetDialogs::Flag::f_folder_id;
- const auto hash = uint64(0);
- state->requestId = request(MTPmessages_GetDialogs(
- MTP_flags(flags),
- MTP_int(folder ? folder->id() : 0),
- MTP_int(state->offsetDate),
- MTP_int(state->offsetId),
- (state->offsetPeer
- ? state->offsetPeer->input
- : MTP_inputPeerEmpty()),
- MTP_int(loadCount),
- MTP_long(hash)
- )).done([=](const MTPmessages_Dialogs &result) {
- const auto state = dialogsLoadState(folder);
- const auto count = result.match([](
- const MTPDmessages_dialogsNotModified &) {
- LOG(("API Error: not-modified received for requested dialogs."));
- return 0;
- }, [&](const MTPDmessages_dialogs &data) {
- if (state) {
- state->listReceived = true;
- dialogsLoadFinish(folder); // may kill 'state'.
- }
- return int(data.vdialogs().v.size());
- }, [&](const MTPDmessages_dialogsSlice &data) {
- updateDialogsOffset(
- folder,
- data.vdialogs().v,
- data.vmessages().v);
- return data.vcount().v;
- });
- result.match([](const MTPDmessages_dialogsNotModified & data) {
- LOG(("API Error: not-modified received for requested dialogs."));
- }, [&](const auto &data) {
- _session->data().processUsers(data.vusers());
- _session->data().processChats(data.vchats());
- _session->data().applyDialogs(
- folder,
- data.vmessages().v,
- data.vdialogs().v,
- count);
- });
- if (!folder
- && (!_dialogsLoadState || !_dialogsLoadState->listReceived)) {
- refreshDialogsLoadBlocked();
- }
- requestMoreDialogsIfNeeded();
- _session->data().chatsListChanged(folder);
- }).fail([=] {
- dialogsLoadState(folder)->requestId = 0;
- }).send();
- if (!state->pinnedReceived) {
- requestPinnedDialogs(folder);
- }
- if (!folder) {
- refreshDialogsLoadBlocked();
- }
- }
- void ApiWrap::refreshDialogsLoadBlocked() {
- _dialogsLoadMayBlockByDate = _dialogsLoadState
- && !_dialogsLoadState->listReceived
- && (_dialogsLoadTill > 0);
- _dialogsLoadBlockedByDate = _dialogsLoadState
- && !_dialogsLoadState->listReceived
- && !_dialogsLoadState->requestId
- && (_dialogsLoadTill > 0)
- && (_dialogsLoadState->offsetDate > 0)
- && (_dialogsLoadState->offsetDate <= _dialogsLoadTill);
- }
- void ApiWrap::requestMoreDialogsIfNeeded() {
- const auto dialogsReady = !_dialogsLoadState
- || _dialogsLoadState->listReceived;
- if (_session->data().chatsFilters().loadNextExceptions(dialogsReady)) {
- return;
- } else if (_dialogsLoadState && !_dialogsLoadState->listReceived) {
- if (_dialogsLoadState->requestId) {
- return;
- }
- requestDialogs(nullptr);
- } else if (const auto folder = _session->data().folderLoaded(
- Data::Folder::kId)) {
- if (_session->data().chatsFilters().archiveNeeded()) {
- requestMoreDialogs(folder);
- }
- }
- requestContacts();
- _session->data().shortcutMessages().preloadShortcuts();
- }
- void ApiWrap::updateDialogsOffset(
- Data::Folder *folder,
- const QVector<MTPDialog> &dialogs,
- const QVector<MTPMessage> &messages) {
- auto lastDate = TimeId(0);
- auto lastPeer = PeerId(0);
- auto lastMsgId = MsgId(0);
- for (const auto &dialog : ranges::views::reverse(dialogs)) {
- dialog.match([&](const auto &dialog) {
- const auto peer = peerFromMTP(dialog.vpeer());
- const auto messageId = dialog.vtop_message().v;
- if (!peer || !messageId) {
- return;
- }
- if (!lastPeer) {
- lastPeer = peer;
- }
- if (!lastMsgId) {
- lastMsgId = messageId;
- }
- for (const auto &message : ranges::views::reverse(messages)) {
- if (IdFromMessage(message) == messageId
- && PeerFromMessage(message) == peer) {
- if (const auto date = DateFromMessage(message)) {
- lastDate = date;
- }
- return;
- }
- }
- });
- if (lastDate) {
- break;
- }
- }
- if (const auto state = dialogsLoadState(folder)) {
- if (lastDate) {
- state->offsetDate = lastDate;
- state->offsetId = lastMsgId;
- state->offsetPeer = _session->data().peer(lastPeer);
- state->requestId = 0;
- } else {
- state->listReceived = true;
- dialogsLoadFinish(folder);
- }
- }
- }
- auto ApiWrap::dialogsLoadState(Data::Folder *folder) -> DialogsLoadState* {
- if (!folder) {
- return _dialogsLoadState.get();
- }
- const auto i = _foldersLoadState.find(folder);
- return (i != end(_foldersLoadState)) ? &i->second : nullptr;
- }
- void ApiWrap::dialogsLoadFinish(Data::Folder *folder) {
- const auto notify = [&] {
- Core::App().postponeCall(crl::guard(_session, [=] {
- _session->data().chatsListDone(folder);
- }));
- };
- const auto state = dialogsLoadState(folder);
- if (!state || !state->listReceived || !state->pinnedReceived) {
- return;
- }
- if (folder) {
- _foldersLoadState.remove(folder);
- notify();
- } else {
- _dialogsLoadState = nullptr;
- notify();
- }
- }
- void ApiWrap::requestPinnedDialogs(Data::Folder *folder) {
- const auto state = dialogsLoadState(folder);
- if (!state || state->pinnedReceived || state->pinnedRequestId) {
- return;
- }
- const auto finalize = [=] {
- if (const auto state = dialogsLoadState(folder)) {
- state->pinnedRequestId = 0;
- state->pinnedReceived = true;
- dialogsLoadFinish(folder);
- }
- };
- state->pinnedRequestId = request(MTPmessages_GetPinnedDialogs(
- MTP_int(folder ? folder->id() : 0)
- )).done([=](const MTPmessages_PeerDialogs &result) {
- finalize();
- result.match([&](const MTPDmessages_peerDialogs &data) {
- _session->data().processUsers(data.vusers());
- _session->data().processChats(data.vchats());
- _session->data().clearPinnedChats(folder);
- _session->data().applyDialogs(
- folder,
- data.vmessages().v,
- data.vdialogs().v);
- _session->data().chatsListChanged(folder);
- _session->data().notifyPinnedDialogsOrderUpdated();
- });
- }).fail([=] {
- finalize();
- }).send();
- }
- void ApiWrap::requestMoreBlockedByDateDialogs() {
- if (!_dialogsLoadState) {
- return;
- }
- const auto max = _session->settings().supportChatsTimeSlice();
- _dialogsLoadTill = _dialogsLoadState->offsetDate
- ? (_dialogsLoadState->offsetDate - max)
- : (base::unixtime::now() - max);
- refreshDialogsLoadBlocked();
- requestDialogs();
- }
- rpl::producer<bool> ApiWrap::dialogsLoadMayBlockByDate() const {
- return _dialogsLoadMayBlockByDate.value();
- }
- rpl::producer<bool> ApiWrap::dialogsLoadBlockedByDate() const {
- return _dialogsLoadBlockedByDate.value();
- }
- void ApiWrap::requestWallPaper(
- const QString &slug,
- Fn<void(const Data::WallPaper &)> done,
- Fn<void()> fail) {
- if (_wallPaperSlug != slug) {
- _wallPaperSlug = slug;
- if (_wallPaperRequestId) {
- request(base::take(_wallPaperRequestId)).cancel();
- }
- }
- _wallPaperDone = std::move(done);
- _wallPaperFail = std::move(fail);
- if (_wallPaperRequestId) {
- return;
- }
- _wallPaperRequestId = request(MTPaccount_GetWallPaper(
- MTP_inputWallPaperSlug(MTP_string(slug))
- )).done([=](const MTPWallPaper &result) {
- _wallPaperRequestId = 0;
- _wallPaperSlug = QString();
- if (const auto paper = Data::WallPaper::Create(_session, result)) {
- if (const auto done = base::take(_wallPaperDone)) {
- done(*paper);
- }
- } else if (const auto fail = base::take(_wallPaperFail)) {
- fail();
- }
- }).fail([=](const MTP::Error &error) {
- _wallPaperRequestId = 0;
- _wallPaperSlug = QString();
- if (const auto fail = base::take(_wallPaperFail)) {
- fail();
- }
- }).send();
- }
- void ApiWrap::requestFullPeer(not_null<PeerData*> peer) {
- if (_fullPeerRequests.contains(peer)) {
- return;
- }
- const auto requestId = [&] {
- const auto failHandler = [=](const MTP::Error &error) {
- _fullPeerRequests.remove(peer);
- migrateFail(peer, error.type());
- };
- if (const auto user = peer->asUser()) {
- if (_session->supportMode()) {
- _session->supportHelper().refreshInfo(user);
- }
- return request(MTPusers_GetFullUser(
- user->inputUser
- )).done([=](const MTPusers_UserFull &result) {
- result.match([&](const MTPDusers_userFull &data) {
- _session->data().processUsers(data.vusers());
- _session->data().processChats(data.vchats());
- });
- gotUserFull(user, result);
- }).fail(failHandler).send();
- } else if (const auto chat = peer->asChat()) {
- return request(MTPmessages_GetFullChat(
- chat->inputChat
- )).done([=](const MTPmessages_ChatFull &result) {
- gotChatFull(peer, result);
- }).fail(failHandler).send();
- } else if (const auto channel = peer->asChannel()) {
- return request(MTPchannels_GetFullChannel(
- channel->inputChannel
- )).done([=](const MTPmessages_ChatFull &result) {
- gotChatFull(peer, result);
- migrateDone(channel, channel);
- }).fail(failHandler).send();
- }
- Unexpected("Peer type in requestFullPeer.");
- }();
- _fullPeerRequests.emplace(peer, requestId);
- }
- void ApiWrap::processFullPeer(
- not_null<PeerData*> peer,
- const MTPmessages_ChatFull &result) {
- gotChatFull(peer, result);
- }
- void ApiWrap::gotChatFull(
- not_null<PeerData*> peer,
- const MTPmessages_ChatFull &result) {
- const auto &d = result.c_messages_chatFull();
- _session->data().applyMaximumChatVersions(d.vchats());
- _session->data().processUsers(d.vusers());
- _session->data().processChats(d.vchats());
- d.vfull_chat().match([&](const MTPDchatFull &data) {
- if (const auto chat = peer->asChat()) {
- Data::ApplyChatUpdate(chat, data);
- } else {
- LOG(("MTP Error: bad type in gotChatFull for channel: %1"
- ).arg(d.vfull_chat().type()));
- }
- }, [&](const MTPDchannelFull &data) {
- if (const auto channel = peer->asChannel()) {
- Data::ApplyChannelUpdate(channel, data);
- } else {
- LOG(("MTP Error: bad type in gotChatFull for chat: %1"
- ).arg(d.vfull_chat().type()));
- }
- });
- _fullPeerRequests.remove(peer);
- _session->changes().peerUpdated(
- peer,
- Data::PeerUpdate::Flag::FullInfo);
- }
- void ApiWrap::gotUserFull(
- not_null<UserData*> user,
- const MTPusers_UserFull &result) {
- result.match([&](const MTPDusers_userFull &data) {
- data.vfull_user().match([&](const MTPDuserFull &fields) {
- if (user == _session->user() && !_session->validateSelf(fields.vid().v)) {
- constexpr auto kRequestUserAgainTimeout = crl::time(10000);
- base::call_delayed(kRequestUserAgainTimeout, _session, [=] {
- requestFullPeer(user);
- });
- return;
- }
- Data::ApplyUserUpdate(user, fields);
- });
- });
- _fullPeerRequests.remove(user);
- _session->changes().peerUpdated(
- user,
- Data::PeerUpdate::Flag::FullInfo);
- }
- void ApiWrap::requestPeerSettings(not_null<PeerData*> peer) {
- if (!_requestedPeerSettings.emplace(peer).second) {
- return;
- }
- request(MTPmessages_GetPeerSettings(
- peer->input
- )).done([=](const MTPmessages_PeerSettings &result) {
- result.match([&](const MTPDmessages_peerSettings &data) {
- _session->data().processUsers(data.vusers());
- _session->data().processChats(data.vchats());
- peer->setBarSettings(data.vsettings());
- _requestedPeerSettings.erase(peer);
- });
- }).fail([=] {
- _requestedPeerSettings.erase(peer);
- }).send();
- }
- void ApiWrap::migrateChat(
- not_null<ChatData*> chat,
- FnMut<void(not_null<ChannelData*>)> done,
- Fn<void(const QString &)> fail) {
- const auto callback = [&] {
- return MigrateCallbacks{ std::move(done), std::move(fail) };
- };
- const auto i = _migrateCallbacks.find(chat);
- if (i != _migrateCallbacks.end()) {
- i->second.push_back(callback());
- return;
- }
- _migrateCallbacks.emplace(chat).first->second.push_back(callback());
- if (const auto channel = chat->migrateTo()) {
- session().changes().peerUpdated(
- chat,
- Data::PeerUpdate::Flag::Migration);
- crl::on_main([=] {
- migrateDone(chat, channel);
- });
- } else if (chat->isDeactivated()) {
- crl::on_main([=] {
- migrateFail(
- chat,
- MTP::Error::Local(
- "BAD_MIGRATION",
- "Chat is already deactivated").type());
- });
- return;
- } else if (!chat->amCreator()) {
- crl::on_main([=] {
- migrateFail(
- chat,
- MTP::Error::Local(
- "BAD_MIGRATION",
- "Current user is not the creator of that chat").type());
- });
- return;
- }
- request(MTPmessages_MigrateChat(
- chat->inputChat
- )).done([=](const MTPUpdates &result) {
- applyUpdates(result);
- session().changes().sendNotifications();
- if (const auto channel = chat->migrateTo()) {
- if (auto handlers = _migrateCallbacks.take(chat)) {
- _migrateCallbacks.emplace(channel, std::move(*handlers));
- }
- requestFullPeer(channel);
- } else {
- migrateFail(
- chat,
- MTP::Error::Local("MIGRATION_FAIL", "No channel").type());
- }
- }).fail([=](const MTP::Error &error) {
- migrateFail(chat, error.type());
- }).send();
- }
- void ApiWrap::migrateDone(
- not_null<PeerData*> peer,
- not_null<ChannelData*> channel) {
- session().changes().sendNotifications();
- if (auto handlers = _migrateCallbacks.take(peer)) {
- for (auto &handler : *handlers) {
- if (handler.done) {
- handler.done(channel);
- }
- }
- }
- }
- void ApiWrap::migrateFail(not_null<PeerData*> peer, const QString &error) {
- if (error == u"CHANNELS_TOO_MUCH"_q) {
- ShowChannelsLimitBox(peer);
- }
- if (auto handlers = _migrateCallbacks.take(peer)) {
- for (auto &handler : *handlers) {
- if (handler.fail) {
- handler.fail(error);
- }
- }
- }
- }
- void ApiWrap::markContentsRead(
- const base::flat_set<not_null<HistoryItem*>> &items) {
- auto markedIds = QVector<MTPint>();
- auto channelMarkedIds = base::flat_map<
- not_null<ChannelData*>,
- QVector<MTPint>>();
- markedIds.reserve(items.size());
- for (const auto &item : items) {
- if (!item->markContentsRead(true) || !item->isRegular()) {
- continue;
- }
- if (const auto channel = item->history()->peer->asChannel()) {
- channelMarkedIds[channel].push_back(MTP_int(item->id));
- } else {
- markedIds.push_back(MTP_int(item->id));
- }
- }
- if (!markedIds.isEmpty()) {
- request(MTPmessages_ReadMessageContents(
- MTP_vector<MTPint>(markedIds)
- )).done([=](const MTPmessages_AffectedMessages &result) {
- applyAffectedMessages(result);
- }).send();
- }
- for (const auto &channelIds : channelMarkedIds) {
- request(MTPchannels_ReadMessageContents(
- channelIds.first->inputChannel,
- MTP_vector<MTPint>(channelIds.second)
- )).send();
- }
- }
- void ApiWrap::markContentsRead(not_null<HistoryItem*> item) {
- if (!item->markContentsRead(true) || !item->isRegular()) {
- return;
- }
- const auto ids = MTP_vector<MTPint>(1, MTP_int(item->id));
- if (const auto channel = item->history()->peer->asChannel()) {
- request(MTPchannels_ReadMessageContents(
- channel->inputChannel,
- ids
- )).send();
- } else {
- request(MTPmessages_ReadMessageContents(
- ids
- )).done([=](const MTPmessages_AffectedMessages &result) {
- applyAffectedMessages(result);
- }).send();
- }
- }
- void ApiWrap::deleteAllFromParticipant(
- not_null<ChannelData*> channel,
- not_null<PeerData*> from) {
- const auto history = _session->data().historyLoaded(channel);
- const auto ids = history
- ? history->collectMessagesFromParticipantToDelete(from)
- : std::vector<MsgId>();
- for (const auto &msgId : ids) {
- if (const auto item = _session->data().message(channel->id, msgId)) {
- item->destroy();
- }
- }
- _session->data().sendHistoryChangeNotifications();
- deleteAllFromParticipantSend(channel, from);
- }
- void ApiWrap::deleteAllFromParticipantSend(
- not_null<ChannelData*> channel,
- not_null<PeerData*> from) {
- request(MTPchannels_DeleteParticipantHistory(
- channel->inputChannel,
- from->input
- )).done([=](const MTPmessages_AffectedHistory &result) {
- const auto offset = applyAffectedHistory(channel, result);
- if (offset > 0) {
- deleteAllFromParticipantSend(channel, from);
- } else if (const auto history = _session->data().historyLoaded(channel)) {
- history->requestChatListMessage();
- }
- }).send();
- }
- void ApiWrap::scheduleStickerSetRequest(uint64 setId, uint64 access) {
- if (!_stickerSetRequests.contains(setId)) {
- _stickerSetRequests.emplace(setId, StickerSetRequest{ access });
- }
- }
- void ApiWrap::requestStickerSets() {
- for (auto &[id, info] : _stickerSetRequests) {
- if (info.id) {
- continue;
- }
- info.id = request(MTPmessages_GetStickerSet(
- MTP_inputStickerSetID(
- MTP_long(id),
- MTP_long(info.accessHash)),
- MTP_int(0) // hash
- )).done([=, setId = id](const MTPmessages_StickerSet &result) {
- gotStickerSet(setId, result);
- }).fail([=, setId = id] {
- _stickerSetRequests.remove(setId);
- }).afterDelay(kSmallDelayMs).send();
- }
- }
- void ApiWrap::saveStickerSets(
- const Data::StickersSetsOrder &localOrder,
- const Data::StickersSetsOrder &localRemoved,
- Data::StickersType type) {
- auto &setDisenableRequests = (type == Data::StickersType::Emoji)
- ? _customEmojiSetDisenableRequests
- : (type == Data::StickersType::Masks)
- ? _maskSetDisenableRequests
- : _stickerSetDisenableRequests;
- const auto reorderRequestId = [=]() -> mtpRequestId & {
- return (type == Data::StickersType::Emoji)
- ? _customEmojiReorderRequestId
- : (type == Data::StickersType::Masks)
- ? _masksReorderRequestId
- : _stickersReorderRequestId;
- };
- for (auto requestId : base::take(setDisenableRequests)) {
- request(requestId).cancel();
- }
- request(base::take(reorderRequestId())).cancel();
- request(base::take(_stickersClearRecentRequestId)).cancel();
- request(base::take(_stickersClearRecentAttachedRequestId)).cancel();
- const auto stickersSaveOrder = [=] {
- if (localOrder.size() < 2) {
- return;
- }
- QVector<MTPlong> mtpOrder;
- mtpOrder.reserve(localOrder.size());
- for (const auto setId : std::as_const(localOrder)) {
- mtpOrder.push_back(MTP_long(setId));
- }
- using Flag = MTPmessages_ReorderStickerSets::Flag;
- const auto flags = (type == Data::StickersType::Emoji)
- ? Flag::f_emojis
- : (type == Data::StickersType::Masks)
- ? Flag::f_masks
- : Flag(0);
- reorderRequestId() = request(MTPmessages_ReorderStickerSets(
- MTP_flags(flags),
- MTP_vector<MTPlong>(mtpOrder)
- )).done([=] {
- reorderRequestId() = 0;
- }).fail([=] {
- reorderRequestId() = 0;
- if (type == Data::StickersType::Emoji) {
- _session->data().stickers().setLastEmojiUpdate(0);
- updateCustomEmoji();
- } else if (type == Data::StickersType::Masks) {
- _session->data().stickers().setLastMasksUpdate(0);
- updateMasks();
- } else {
- _session->data().stickers().setLastUpdate(0);
- updateStickers();
- }
- }).send();
- };
- const auto stickerSetDisenabled = [=](mtpRequestId requestId) {
- auto &setDisenableRequests = (type == Data::StickersType::Emoji)
- ? _customEmojiSetDisenableRequests
- : (type == Data::StickersType::Masks)
- ? _maskSetDisenableRequests
- : _stickerSetDisenableRequests;
- setDisenableRequests.remove(requestId);
- if (setDisenableRequests.empty()) {
- stickersSaveOrder();
- }
- };
- auto writeInstalled = true,
- writeRecent = false,
- writeCloudRecent = false,
- writeCloudRecentAttached = false,
- writeFaved = false,
- writeArchived = false;
- auto &recent = _session->data().stickers().getRecentPack();
- auto &sets = _session->data().stickers().setsRef();
- auto &order = (type == Data::StickersType::Emoji)
- ? _session->data().stickers().emojiSetsOrder()
- : (type == Data::StickersType::Masks)
- ? _session->data().stickers().maskSetsOrder()
- : _session->data().stickers().setsOrder();
- auto &orderRef = (type == Data::StickersType::Emoji)
- ? _session->data().stickers().emojiSetsOrderRef()
- : (type == Data::StickersType::Masks)
- ? _session->data().stickers().maskSetsOrderRef()
- : _session->data().stickers().setsOrderRef();
- using Flag = Data::StickersSetFlag;
- for (const auto removedSetId : localRemoved) {
- if ((removedSetId == Data::Stickers::CloudRecentSetId)
- || (removedSetId == Data::Stickers::CloudRecentAttachedSetId)) {
- if (sets.remove(Data::Stickers::CloudRecentSetId) != 0) {
- writeCloudRecent = true;
- }
- if (sets.remove(Data::Stickers::CloudRecentAttachedSetId) != 0) {
- writeCloudRecentAttached = true;
- }
- if (sets.remove(Data::Stickers::CustomSetId)) {
- writeInstalled = true;
- }
- if (!recent.isEmpty()) {
- recent.clear();
- writeRecent = true;
- }
- const auto isAttached
- = (removedSetId == Data::Stickers::CloudRecentAttachedSetId);
- const auto flags = isAttached
- ? MTPmessages_ClearRecentStickers::Flag::f_attached
- : MTPmessages_ClearRecentStickers::Flags(0);
- auto &requestId = isAttached
- ? _stickersClearRecentAttachedRequestId
- : _stickersClearRecentRequestId;
- const auto finish = [=] {
- (isAttached
- ? _stickersClearRecentAttachedRequestId
- : _stickersClearRecentRequestId) = 0;
- };
- requestId = request(MTPmessages_ClearRecentStickers(
- MTP_flags(flags)
- )).done(finish).fail(finish).send();
- continue;
- }
- auto it = sets.find(removedSetId);
- if (it != sets.cend()) {
- const auto set = it->second.get();
- for (auto i = recent.begin(); i != recent.cend();) {
- if (set->stickers.indexOf(i->first) >= 0) {
- i = recent.erase(i);
- writeRecent = true;
- } else {
- ++i;
- }
- }
- const auto archived = !!(set->flags & Flag::Archived);
- if (!archived) {
- const auto featured = !!(set->flags & Flag::Featured);
- const auto special = !!(set->flags & Flag::Special);
- const auto emoji = !!(set->flags & Flag::Emoji);
- const auto locked = (set->locked > 0);
- const auto setId = set->mtpInput();
- auto requestId = request(MTPmessages_UninstallStickerSet(
- setId
- )).done([=](const MTPBool &result, mtpRequestId requestId) {
- stickerSetDisenabled(requestId);
- }).fail([=](const MTP::Error &error, mtpRequestId requestId) {
- stickerSetDisenabled(requestId);
- }).afterDelay(kSmallDelayMs).send();
- setDisenableRequests.insert(requestId);
- const auto removeIndex = order.indexOf(set->id);
- if (removeIndex >= 0) {
- orderRef.removeAt(removeIndex);
- }
- if (!featured && !special && !emoji && !locked) {
- sets.erase(it);
- } else {
- if (archived) {
- writeArchived = true;
- }
- set->flags &= ~(Flag::Installed | Flag::Archived);
- set->installDate = TimeId(0);
- }
- }
- }
- }
- // Clear all installed flags, set only for sets from order.
- for (auto &[id, set] : sets) {
- const auto archived = !!(set->flags & Flag::Archived);
- const auto thatType = !!(set->flags & Flag::Emoji)
- ? Data::StickersType::Emoji
- : !!(set->flags & Flag::Masks)
- ? Data::StickersType::Masks
- : Data::StickersType::Stickers;
- if (!archived && (type == thatType)) {
- set->flags &= ~Flag::Installed;
- }
- }
- orderRef.clear();
- for (const auto setId : std::as_const(localOrder)) {
- auto it = sets.find(setId);
- if (it == sets.cend()) {
- continue;
- }
- const auto set = it->second.get();
- const auto archived = !!(set->flags & Flag::Archived);
- if (archived && !localRemoved.contains(set->id)) {
- const auto mtpSetId = set->mtpInput();
- const auto requestId = request(MTPmessages_InstallStickerSet(
- mtpSetId,
- MTP_boolFalse()
- )).done([=](
- const MTPmessages_StickerSetInstallResult &result,
- mtpRequestId requestId) {
- stickerSetDisenabled(requestId);
- }).fail([=](
- const MTP::Error &error,
- mtpRequestId requestId) {
- stickerSetDisenabled(requestId);
- }).afterDelay(kSmallDelayMs).send();
- setDisenableRequests.insert(requestId);
- set->flags &= ~Flag::Archived;
- writeArchived = true;
- }
- orderRef.push_back(setId);
- set->flags |= Flag::Installed;
- if (!set->installDate) {
- set->installDate = base::unixtime::now();
- }
- }
- for (auto it = sets.begin(); it != sets.cend();) {
- const auto set = it->second.get();
- if ((set->flags & Flag::Featured)
- || (set->flags & Flag::Installed)
- || (set->flags & Flag::Archived)
- || (set->flags & Flag::Special)
- || (set->flags & Flag::Emoji)
- || (set->locked > 0)) {
- ++it;
- } else {
- it = sets.erase(it);
- }
- }
- auto &storage = local();
- if (writeInstalled) {
- if (type == Data::StickersType::Emoji) {
- storage.writeInstalledCustomEmoji();
- } else if (type == Data::StickersType::Masks) {
- storage.writeInstalledMasks();
- } else {
- storage.writeInstalledStickers();
- }
- }
- if (writeRecent) {
- session().saveSettings();
- }
- if (writeArchived) {
- if (type == Data::StickersType::Emoji) {
- } else if (type == Data::StickersType::Masks) {
- storage.writeArchivedMasks();
- } else {
- storage.writeArchivedStickers();
- }
- }
- if (writeCloudRecent) {
- storage.writeRecentStickers();
- }
- if (writeCloudRecentAttached) {
- storage.writeRecentMasks();
- }
- if (writeFaved) {
- storage.writeFavedStickers();
- }
- _session->data().stickers().notifyUpdated(type);
- if (setDisenableRequests.empty()) {
- stickersSaveOrder();
- } else {
- requestSendDelayed();
- }
- }
- void ApiWrap::joinChannel(not_null<ChannelData*> channel) {
- if (channel->amIn()) {
- session().changes().peerUpdated(
- channel,
- Data::PeerUpdate::Flag::ChannelAmIn);
- } else if (!_channelAmInRequests.contains(channel)) {
- const auto requestId = request(MTPchannels_JoinChannel(
- channel->inputChannel
- )).done([=](const MTPUpdates &result) {
- _channelAmInRequests.remove(channel);
- applyUpdates(result);
- }).fail([=](const MTP::Error &error) {
- const auto &type = error.type();
- const auto show = ShowForPeer(channel);
- if (type == u"CHANNEL_PRIVATE"_q
- && channel->invitePeekExpires()) {
- channel->privateErrorReceived();
- } else if (type == u"CHANNELS_TOO_MUCH"_q) {
- ShowChannelsLimitBox(channel);
- } else {
- const auto text = [&] {
- if (type == u"INVITE_REQUEST_SENT"_q) {
- return channel->isMegagroup()
- ? tr::lng_group_request_sent(tr::now)
- : tr::lng_group_request_sent_channel(tr::now);
- } else if (type == u"CHANNEL_PRIVATE"_q
- || type == u"CHANNEL_PUBLIC_GROUP_NA"_q
- || type == u"USER_BANNED_IN_CHANNEL"_q) {
- return channel->isMegagroup()
- ? tr::lng_group_not_accessible(tr::now)
- : tr::lng_channel_not_accessible(tr::now);
- } else if (type == u"USERS_TOO_MUCH"_q) {
- return tr::lng_group_full(tr::now);
- }
- return QString();
- }();
- if (show && !text.isEmpty()) {
- show->showToast(text, kJoinErrorDuration);
- }
- }
- _channelAmInRequests.remove(channel);
- }).send();
- _channelAmInRequests.emplace(channel, requestId);
- using Flag = ChannelDataFlag;
- chatParticipants().loadSimilarPeers(channel);
- channel->setFlags(channel->flags() | Flag::SimilarExpanded);
- }
- }
- void ApiWrap::leaveChannel(not_null<ChannelData*> channel) {
- if (!channel->amIn()) {
- session().changes().peerUpdated(
- channel,
- Data::PeerUpdate::Flag::ChannelAmIn);
- } else if (!_channelAmInRequests.contains(channel)) {
- auto requestId = request(MTPchannels_LeaveChannel(
- channel->inputChannel
- )).done([=](const MTPUpdates &result) {
- _channelAmInRequests.remove(channel);
- applyUpdates(result);
- }).fail([=] {
- _channelAmInRequests.remove(channel);
- }).send();
- _channelAmInRequests.emplace(channel, requestId);
- }
- }
- void ApiWrap::requestNotifySettings(const MTPInputNotifyPeer &peer) {
- const auto bad = peer.match([](const MTPDinputNotifyUsers &) {
- return false;
- }, [](const MTPDinputNotifyChats &) {
- return false;
- }, [](const MTPDinputNotifyBroadcasts &) {
- return false;
- }, [&](const MTPDinputNotifyPeer &data) {
- if (data.vpeer().type() == mtpc_inputPeerEmpty) {
- LOG(("Api Error: Requesting settings for empty peer."));
- return true;
- }
- return false;
- }, [&](const MTPDinputNotifyForumTopic &data) {
- if (data.vpeer().type() == mtpc_inputPeerEmpty) {
- LOG(("Api Error: Requesting settings for empty peer topic."));
- return true;
- }
- return false;
- });
- if (bad) {
- return;
- }
- const auto peerFromInput = [&](const MTPInputPeer &inputPeer) {
- return inputPeer.match([&](const MTPDinputPeerSelf &) {
- return _session->userPeerId();
- }, [](const MTPDinputPeerEmpty &) {
- return PeerId(0);
- }, [](const MTPDinputPeerChannel &data) {
- return peerFromChannel(data.vchannel_id());
- }, [](const MTPDinputPeerChat &data) {
- return peerFromChat(data.vchat_id());
- }, [](const MTPDinputPeerUser &data) {
- return peerFromUser(data.vuser_id());
- }, [](const auto &) -> PeerId {
- Unexpected("Type in ApiRequest::requestNotifySettings peer.");
- });
- };
- const auto key = peer.match([](const MTPDinputNotifyUsers &) {
- return NotifySettingsKey{ peerFromUser(1) };
- }, [](const MTPDinputNotifyChats &) {
- return NotifySettingsKey{ peerFromChat(1) };
- }, [](const MTPDinputNotifyBroadcasts &) {
- return NotifySettingsKey{ peerFromChannel(1) };
- }, [&](const MTPDinputNotifyPeer &data) {
- return NotifySettingsKey{ peerFromInput(data.vpeer()) };
- }, [&](const MTPDinputNotifyForumTopic &data) {
- return NotifySettingsKey{
- peerFromInput(data.vpeer()),
- data.vtop_msg_id().v,
- };
- });
- if (_notifySettingRequests.contains(key)) {
- return;
- }
- const auto requestId = request(MTPaccount_GetNotifySettings(
- peer
- )).done([=](const MTPPeerNotifySettings &result) {
- _session->data().notifySettings().apply(peer, result);
- _notifySettingRequests.remove(key);
- }).fail([=] {
- _session->data().notifySettings().apply(
- peer,
- MTP_peerNotifySettings(
- MTP_flags(0),
- MTPBool(),
- MTPBool(),
- MTPint(),
- MTPNotificationSound(),
- MTPNotificationSound(),
- MTPNotificationSound(),
- MTPBool(),
- MTPBool(),
- MTPNotificationSound(),
- MTPNotificationSound(),
- MTPNotificationSound()));
- _notifySettingRequests.erase(key);
- }).send();
- _notifySettingRequests.emplace(key, requestId);
- }
- void ApiWrap::updateNotifySettingsDelayed(
- not_null<const Data::Thread*> thread) {
- const auto topic = thread->asTopic();
- if (!topic) {
- return updateNotifySettingsDelayed(thread->peer());
- }
- if (_updateNotifyTopics.emplace(topic).second) {
- topic->destroyed(
- ) | rpl::start_with_next([=] {
- _updateNotifyTopics.remove(topic);
- }, _updateNotifyQueueLifetime);
- _updateNotifyTimer.callOnce(kNotifySettingSaveTimeout);
- }
- }
- void ApiWrap::updateNotifySettingsDelayed(not_null<const PeerData*> peer) {
- if (_updateNotifyPeers.emplace(peer).second) {
- _updateNotifyTimer.callOnce(kNotifySettingSaveTimeout);
- }
- }
- void ApiWrap::updateNotifySettingsDelayed(Data::DefaultNotify type) {
- if (_updateNotifyDefaults.emplace(type).second) {
- _updateNotifyTimer.callOnce(kNotifySettingSaveTimeout);
- }
- }
- void ApiWrap::sendNotifySettingsUpdates() {
- _updateNotifyQueueLifetime.destroy();
- for (const auto topic : base::take(_updateNotifyTopics)) {
- request(MTPaccount_UpdateNotifySettings(
- MTP_inputNotifyForumTopic(
- topic->channel()->input,
- MTP_int(topic->rootId())),
- topic->notify().serialize()
- )).afterDelay(kSmallDelayMs).send();
- }
- for (const auto peer : base::take(_updateNotifyPeers)) {
- request(MTPaccount_UpdateNotifySettings(
- MTP_inputNotifyPeer(peer->input),
- peer->notify().serialize()
- )).afterDelay(kSmallDelayMs).send();
- }
- const auto &settings = session().data().notifySettings();
- for (const auto type : base::take(_updateNotifyDefaults)) {
- request(MTPaccount_UpdateNotifySettings(
- Data::DefaultNotifyToMTP(type),
- settings.defaultSettings(type).serialize()
- )).afterDelay(kSmallDelayMs).send();
- }
- session().mtp().sendAnything();
- }
- void ApiWrap::saveDraftToCloudDelayed(not_null<Data::Thread*> thread) {
- _draftsSaveRequestIds.emplace(base::make_weak(thread), 0);
- if (!_draftsSaveTimer.isActive()) {
- _draftsSaveTimer.callOnce(kSaveCloudDraftTimeout);
- }
- }
- void ApiWrap::updatePrivacyLastSeens() {
- const auto now = base::unixtime::now();
- if (!_session->premium()) {
- _session->data().enumerateUsers([&](not_null<UserData*> user) {
- if (user->isSelf()
- || !user->isLoaded()
- || user->lastseen().isHidden()) {
- return;
- }
- const auto till = user->lastseen().onlineTill();
- user->updateLastseen((till + 3 * 86400 >= now)
- ? Data::LastseenStatus::Recently(true)
- : (till + 7 * 86400 >= now)
- ? Data::LastseenStatus::WithinWeek(true)
- : (till + 30 * 86400 >= now)
- ? Data::LastseenStatus::WithinMonth(true)
- : Data::LastseenStatus::LongAgo(true));
- session().changes().peerUpdated(
- user,
- Data::PeerUpdate::Flag::OnlineStatus);
- session().data().maybeStopWatchForOffline(user);
- });
- }
- if (_contactsStatusesRequestId) {
- request(_contactsStatusesRequestId).cancel();
- }
- _contactsStatusesRequestId = request(MTPcontacts_GetStatuses(
- )).done([=](const MTPVector<MTPContactStatus> &result) {
- _contactsStatusesRequestId = 0;
- for (const auto &status : result.v) {
- const auto &data = status.data();
- const auto userId = UserId(data.vuser_id());
- if (const auto user = _session->data().userLoaded(userId)) {
- const auto status = LastseenFromMTP(
- data.vstatus(),
- user->lastseen());
- if (user->updateLastseen(status)) {
- session().changes().peerUpdated(
- user,
- Data::PeerUpdate::Flag::OnlineStatus);
- }
- }
- }
- }).fail([this] {
- _contactsStatusesRequestId = 0;
- }).send();
- }
- void ApiWrap::clearHistory(not_null<PeerData*> peer, bool revoke) {
- deleteHistory(peer, true, revoke);
- }
- void ApiWrap::deleteConversation(not_null<PeerData*> peer, bool revoke) {
- if (const auto chat = peer->asChat()) {
- request(MTPmessages_DeleteChatUser(
- MTP_flags(0),
- chat->inputChat,
- _session->user()->inputUser
- )).done([=](const MTPUpdates &result) {
- applyUpdates(result);
- deleteHistory(peer, false, revoke);
- }).fail([=] {
- deleteHistory(peer, false, revoke);
- }).send();
- } else {
- deleteHistory(peer, false, revoke);
- }
- }
- void ApiWrap::deleteHistory(
- not_null<PeerData*> peer,
- bool justClear,
- bool revoke) {
- auto deleteTillId = MsgId(0);
- const auto history = _session->data().history(peer);
- if (justClear) {
- // In case of clear history we need to know the last server message.
- while (history->lastMessageKnown()) {
- const auto last = history->lastMessage();
- if (!last) {
- // History is empty.
- return;
- } else if (!last->isRegular()) {
- // Destroy client-side message locally.
- last->destroy();
- } else {
- break;
- }
- }
- if (!history->lastMessageKnown()) {
- history->owner().histories().requestDialogEntry(history, [=] {
- Expects(history->lastMessageKnown());
- deleteHistory(peer, justClear, revoke);
- });
- return;
- }
- deleteTillId = history->lastMessage()->id;
- }
- if (const auto channel = peer->asChannel()) {
- if (!justClear && !revoke) {
- channel->ptsSetWaitingForShortPoll(-1);
- leaveChannel(channel);
- } else {
- if (const auto migrated = peer->migrateFrom()) {
- deleteHistory(migrated, justClear, revoke);
- }
- if (deleteTillId || (!justClear && revoke)) {
- history->owner().histories().deleteAllMessages(
- history,
- deleteTillId,
- justClear,
- revoke);
- }
- }
- } else {
- history->owner().histories().deleteAllMessages(
- history,
- deleteTillId,
- justClear,
- revoke);
- }
- if (!justClear) {
- _session->data().deleteConversationLocally(peer);
- } else if (history) {
- history->clear(History::ClearType::ClearHistory);
- }
- }
- void ApiWrap::applyUpdates(
- const MTPUpdates &updates,
- uint64 sentMessageRandomId) const {
- this->updates().applyUpdates(updates, sentMessageRandomId);
- }
- int ApiWrap::applyAffectedHistory(
- PeerData *peer,
- const MTPmessages_AffectedHistory &result) const {
- const auto &data = result.c_messages_affectedHistory();
- if (const auto channel = peer ? peer->asChannel() : nullptr) {
- channel->ptsUpdateAndApply(data.vpts().v, data.vpts_count().v);
- } else {
- updates().updateAndApply(data.vpts().v, data.vpts_count().v);
- }
- return data.voffset().v;
- }
- void ApiWrap::applyAffectedMessages(
- not_null<PeerData*> peer,
- const MTPmessages_AffectedMessages &result) {
- const auto &data = result.c_messages_affectedMessages();
- if (const auto channel = peer->asChannel()) {
- channel->ptsUpdateAndApply(data.vpts().v, data.vpts_count().v);
- } else {
- applyAffectedMessages(result);
- }
- }
- void ApiWrap::applyAffectedMessages(
- const MTPmessages_AffectedMessages &result) const {
- const auto &data = result.c_messages_affectedMessages();
- updates().updateAndApply(data.vpts().v, data.vpts_count().v);
- }
- void ApiWrap::saveCurrentDraftToCloud() {
- Core::App().materializeLocalDrafts();
- for (const auto &controller : _session->windows()) {
- if (const auto thread = controller->activeChatCurrent().thread()) {
- const auto topic = thread->asTopic();
- if (topic && topic->creating()) {
- continue;
- }
- const auto history = thread->owningHistory();
- _session->local().writeDrafts(history);
- const auto topicRootId = thread->topicRootId();
- const auto localDraft = history->localDraft(topicRootId);
- const auto cloudDraft = history->cloudDraft(topicRootId);
- if (!Data::DraftsAreEqual(localDraft, cloudDraft)
- && !_session->supportMode()) {
- saveDraftToCloudDelayed(thread);
- }
- }
- }
- }
- void ApiWrap::saveDraftsToCloud() {
- for (auto i = begin(_draftsSaveRequestIds); i != end(_draftsSaveRequestIds);) {
- const auto weak = i->first;
- const auto thread = weak.get();
- if (!thread) {
- i = _draftsSaveRequestIds.erase(i);
- continue;
- } else if (i->second) {
- ++i;
- continue; // sent already
- }
- const auto history = thread->owningHistory();
- const auto topicRootId = thread->topicRootId();
- auto cloudDraft = history->cloudDraft(topicRootId);
- auto localDraft = history->localDraft(topicRootId);
- if (cloudDraft && cloudDraft->saveRequestId) {
- request(base::take(cloudDraft->saveRequestId)).cancel();
- }
- if (!_session->supportMode()) {
- cloudDraft = history->createCloudDraft(topicRootId, localDraft);
- } else if (!cloudDraft) {
- cloudDraft = history->createCloudDraft(topicRootId, nullptr);
- }
- auto flags = MTPmessages_SaveDraft::Flags(0);
- auto &textWithTags = cloudDraft->textWithTags;
- if (cloudDraft->webpage.removed) {
- flags |= MTPmessages_SaveDraft::Flag::f_no_webpage;
- } else if (!cloudDraft->webpage.url.isEmpty()) {
- flags |= MTPmessages_SaveDraft::Flag::f_media;
- }
- if (cloudDraft->reply.messageId || cloudDraft->reply.topicRootId) {
- flags |= MTPmessages_SaveDraft::Flag::f_reply_to;
- }
- if (!textWithTags.tags.isEmpty()) {
- flags |= MTPmessages_SaveDraft::Flag::f_entities;
- }
- auto entities = Api::EntitiesToMTP(
- _session,
- TextUtilities::ConvertTextTagsToEntities(textWithTags.tags),
- Api::ConvertOption::SkipLocal);
- history->startSavingCloudDraft(topicRootId);
- cloudDraft->saveRequestId = request(MTPmessages_SaveDraft(
- MTP_flags(flags),
- ReplyToForMTP(history, cloudDraft->reply),
- history->peer->input,
- MTP_string(textWithTags.text),
- entities,
- Data::WebPageForMTP(
- cloudDraft->webpage,
- textWithTags.text.isEmpty()),
- MTP_long(0) // effect
- )).done([=](const MTPBool &result, const MTP::Response &response) {
- const auto requestId = response.requestId;
- history->finishSavingCloudDraft(
- topicRootId,
- UnixtimeFromMsgId(response.outerMsgId));
- if (const auto cloudDraft = history->cloudDraft(topicRootId)) {
- if (cloudDraft->saveRequestId == requestId) {
- cloudDraft->saveRequestId = 0;
- history->draftSavedToCloud(topicRootId);
- }
- }
- const auto i = _draftsSaveRequestIds.find(weak);
- if (i != _draftsSaveRequestIds.cend()
- && i->second == requestId) {
- _draftsSaveRequestIds.erase(i);
- checkQuitPreventFinished();
- }
- }).fail([=](const MTP::Error &error, const MTP::Response &response) {
- const auto requestId = response.requestId;
- history->finishSavingCloudDraft(
- topicRootId,
- UnixtimeFromMsgId(response.outerMsgId));
- if (const auto cloudDraft = history->cloudDraft(topicRootId)) {
- if (cloudDraft->saveRequestId == requestId) {
- history->clearCloudDraft(topicRootId);
- }
- }
- const auto i = _draftsSaveRequestIds.find(weak);
- if (i != _draftsSaveRequestIds.cend()
- && i->second == requestId) {
- _draftsSaveRequestIds.erase(i);
- checkQuitPreventFinished();
- }
- }).send();
- i->second = cloudDraft->saveRequestId;
- ++i;
- }
- }
- bool ApiWrap::isQuitPrevent() {
- if (_draftsSaveRequestIds.empty()) {
- return false;
- }
- LOG(("ApiWrap prevents quit, saving drafts..."));
- saveDraftsToCloud();
- return true;
- }
- void ApiWrap::checkQuitPreventFinished() {
- if (_draftsSaveRequestIds.empty()) {
- if (Core::Quitting()) {
- LOG(("ApiWrap doesn't prevent quit any more."));
- }
- Core::App().quitPreventFinished();
- }
- }
- void ApiWrap::registerModifyRequest(
- const QString &key,
- mtpRequestId requestId) {
- const auto i = _modifyRequests.find(key);
- if (i != end(_modifyRequests)) {
- request(i->second).cancel();
- i->second = requestId;
- } else {
- _modifyRequests.emplace(key, requestId);
- }
- }
- void ApiWrap::clearModifyRequest(const QString &key) {
- _modifyRequests.remove(key);
- }
- void ApiWrap::gotStickerSet(
- uint64 setId,
- const MTPmessages_StickerSet &result) {
- _stickerSetRequests.remove(setId);
- result.match([&](const MTPDmessages_stickerSet &data) {
- _session->data().stickers().feedSetFull(data);
- }, [](const MTPDmessages_stickerSetNotModified &) {
- LOG(("API Error: Unexpected messages.stickerSetNotModified."));
- });
- }
- void ApiWrap::requestWebPageDelayed(not_null<WebPageData*> page) {
- if (page->failed || !page->pendingTill) {
- return;
- }
- _webPagesPending.emplace(page, 0);
- auto left = (page->pendingTill - base::unixtime::now()) * 1000;
- if (!_webPagesTimer.isActive() || left <= _webPagesTimer.remainingTime()) {
- _webPagesTimer.callOnce((left < 0 ? 0 : left) + 1);
- }
- }
- void ApiWrap::clearWebPageRequest(not_null<WebPageData*> page) {
- _webPagesPending.remove(page);
- if (_webPagesPending.empty() && _webPagesTimer.isActive()) {
- _webPagesTimer.cancel();
- }
- }
- void ApiWrap::clearWebPageRequests() {
- _webPagesPending.clear();
- _webPagesTimer.cancel();
- }
- void ApiWrap::resolveWebPages() {
- auto ids = QVector<MTPInputMessage>(); // temp_req_id = -1
- using IndexAndMessageIds = QPair<int32, QVector<MTPInputMessage>>;
- using MessageIdsByChannel = base::flat_map<ChannelData*, IndexAndMessageIds>;
- MessageIdsByChannel idsByChannel; // temp_req_id = -index - 2
- ids.reserve(_webPagesPending.size());
- int32 t = base::unixtime::now(), m = INT_MAX;
- for (auto &[page, requestId] : _webPagesPending) {
- if (requestId > 0) {
- continue;
- }
- if (page->pendingTill <= t) {
- if (const auto item = _session->data().findWebPageItem(page)) {
- if (const auto channel = item->history()->peer->asChannel()) {
- auto channelMap = idsByChannel.find(channel);
- if (channelMap == idsByChannel.cend()) {
- channelMap = idsByChannel.emplace(
- channel,
- IndexAndMessageIds(
- idsByChannel.size(),
- QVector<MTPInputMessage>(
- 1,
- MTP_inputMessageID(MTP_int(item->id))))).first;
- } else {
- channelMap->second.second.push_back(
- MTP_inputMessageID(MTP_int(item->id)));
- }
- requestId = -channelMap->second.first - 2;
- } else {
- ids.push_back(MTP_inputMessageID(MTP_int(item->id)));
- requestId = -1;
- }
- }
- } else {
- m = std::min(m, page->pendingTill - t);
- }
- }
- auto requestId = mtpRequestId(0);
- if (!ids.isEmpty()) {
- requestId = request(MTPmessages_GetMessages(
- MTP_vector<MTPInputMessage>(ids)
- )).done([=](
- const MTPmessages_Messages &result,
- mtpRequestId requestId) {
- gotWebPages(nullptr, result, requestId);
- }).afterDelay(kSmallDelayMs).send();
- }
- QVector<mtpRequestId> reqsByIndex(idsByChannel.size(), 0);
- for (auto i = idsByChannel.cbegin(), e = idsByChannel.cend(); i != e; ++i) {
- reqsByIndex[i->second.first] = request(MTPchannels_GetMessages(
- i->first->inputChannel,
- MTP_vector<MTPInputMessage>(i->second.second)
- )).done([=, channel = i->first](
- const MTPmessages_Messages &result,
- mtpRequestId requestId) {
- gotWebPages(channel, result, requestId);
- }).afterDelay(kSmallDelayMs).send();
- }
- if (requestId || !reqsByIndex.isEmpty()) {
- for (auto &[page, pendingRequestId] : _webPagesPending) {
- if (pendingRequestId > 0) {
- continue;
- } else if (pendingRequestId < 0) {
- if (pendingRequestId == -1) {
- pendingRequestId = requestId;
- } else {
- pendingRequestId = reqsByIndex[-pendingRequestId - 2];
- }
- }
- }
- }
- if (m < INT_MAX) {
- _webPagesTimer.callOnce(std::min(m, 86400) * crl::time(1000));
- }
- }
- template <typename Request>
- void ApiWrap::requestFileReference(
- Data::FileOrigin origin,
- FileReferencesHandler &&handler,
- Request &&data) {
- const auto i = _fileReferenceHandlers.find(origin);
- if (i != end(_fileReferenceHandlers)) {
- i->second.push_back(std::move(handler));
- return;
- }
- auto handlers = std::vector<FileReferencesHandler>();
- handlers.push_back(std::move(handler));
- _fileReferenceHandlers.emplace(origin, std::move(handlers));
- request(std::move(data)).done([=](const auto &result) {
- const auto parsed = Data::GetFileReferences(result);
- for (const auto &p : parsed.data) {
- // Unpack here the parsed pair by hand to workaround a GCC bug.
- // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87122
- const auto &origin = p.first;
- const auto &reference = p.second;
- const auto documentId = std::get_if<DocumentFileLocationId>(
- &origin);
- if (documentId) {
- _session->data().document(
- documentId->id
- )->refreshFileReference(reference);
- }
- const auto photoId = std::get_if<PhotoFileLocationId>(&origin);
- if (photoId) {
- _session->data().photo(
- photoId->id
- )->refreshFileReference(reference);
- }
- }
- const auto i = _fileReferenceHandlers.find(origin);
- Assert(i != end(_fileReferenceHandlers));
- auto handlers = std::move(i->second);
- _fileReferenceHandlers.erase(i);
- for (auto &handler : handlers) {
- handler(parsed);
- }
- }).fail([=] {
- const auto i = _fileReferenceHandlers.find(origin);
- Assert(i != end(_fileReferenceHandlers));
- auto handlers = std::move(i->second);
- _fileReferenceHandlers.erase(i);
- for (auto &handler : handlers) {
- handler(UpdatedFileReferences());
- }
- }).send();
- }
- void ApiWrap::refreshFileReference(
- Data::FileOrigin origin,
- not_null<Storage::DownloadMtprotoTask*> task,
- int requestId,
- const QByteArray ¤t) {
- return refreshFileReference(origin, crl::guard(task, [=](
- const UpdatedFileReferences &data) {
- task->refreshFileReferenceFrom(data, requestId, current);
- }));
- }
- void ApiWrap::refreshFileReference(
- Data::FileOrigin origin,
- FileReferencesHandler &&handler) {
- const auto fail = [&] {
- handler(UpdatedFileReferences());
- };
- const auto request = [&](
- auto &&data,
- Fn<void()> &&additional = nullptr) {
- requestFileReference(
- origin,
- std::move(handler),
- std::move(data));
- if (additional) {
- const auto i = _fileReferenceHandlers.find(origin);
- Assert(i != end(_fileReferenceHandlers));
- if (i->second.size() == 1) {
- i->second.push_back([=](auto&&) {
- additional();
- });
- }
- }
- };
- v::match(origin.data, [&](Data::FileOriginMessage data) {
- if (const auto item = _session->data().message(data)) {
- const auto media = item->media();
- const auto storyId = media ? media->storyId() : FullStoryId();
- if (storyId) {
- request(MTPstories_GetStoriesByID(
- _session->data().peer(storyId.peer)->input,
- MTP_vector<MTPint>(1, MTP_int(storyId.story))));
- } else if (item->isScheduled()) {
- const auto realId = _session->scheduledMessages().lookupId(
- item);
- request(MTPmessages_GetScheduledMessages(
- item->history()->peer->input,
- MTP_vector<MTPint>(1, MTP_int(realId))));
- } else if (item->isBusinessShortcut()) {
- const auto &shortcuts = _session->data().shortcutMessages();
- const auto realId = shortcuts.lookupId(item);
- request(MTPmessages_GetQuickReplyMessages(
- MTP_flags(MTPmessages_GetQuickReplyMessages::Flag::f_id),
- MTP_int(item->shortcutId()),
- MTP_vector<MTPint>(1, MTP_int(realId)),
- MTP_long(0)));
- } else if (const auto channel = item->history()->peer->asChannel()) {
- request(MTPchannels_GetMessages(
- channel->inputChannel,
- MTP_vector<MTPInputMessage>(
- 1,
- MTP_inputMessageID(MTP_int(item->id)))));
- } else {
- request(MTPmessages_GetMessages(
- MTP_vector<MTPInputMessage>(
- 1,
- MTP_inputMessageID(MTP_int(item->id)))));
- }
- } else {
- fail();
- }
- }, [&](Data::FileOriginUserPhoto data) {
- if (const auto user = _session->data().user(data.userId)) {
- request(MTPphotos_GetUserPhotos(
- user->inputUser,
- MTP_int(-1),
- MTP_long(data.photoId),
- MTP_int(1)));
- } else {
- fail();
- }
- }, [&](Data::FileOriginFullUser data) {
- if (const auto user = _session->data().user(data.userId)) {
- request(MTPusers_GetFullUser(user->inputUser));
- } else {
- fail();
- }
- }, [&](Data::FileOriginPeerPhoto data) {
- fail();
- }, [&](Data::FileOriginStickerSet data) {
- const auto isRecentAttached
- = (data.setId == Data::Stickers::CloudRecentAttachedSetId);
- if (data.setId == Data::Stickers::CloudRecentSetId
- || data.setId == Data::Stickers::RecentSetId
- || isRecentAttached) {
- auto done = [=] { crl::on_main(_session, [=] {
- if (isRecentAttached) {
- local().writeRecentMasks();
- } else {
- local().writeRecentStickers();
- }
- }); };
- request(MTPmessages_GetRecentStickers(
- MTP_flags(isRecentAttached
- ? MTPmessages_GetRecentStickers::Flag::f_attached
- : MTPmessages_GetRecentStickers::Flags(0)),
- MTP_long(0)),
- std::move(done));
- } else if (data.setId == Data::Stickers::FavedSetId) {
- request(MTPmessages_GetFavedStickers(MTP_long(0)),
- [=] { crl::on_main(_session, [=] { local().writeFavedStickers(); }); });
- } else {
- request(MTPmessages_GetStickerSet(
- MTP_inputStickerSetID(
- MTP_long(data.setId),
- MTP_long(data.accessHash)),
- MTP_int(0)), // hash
- [=] { crl::on_main(_session, [=] {
- local().writeInstalledStickers();
- local().writeRecentStickers();
- local().writeFavedStickers();
- }); });
- }
- }, [&](Data::FileOriginSavedGifs data) {
- request(
- MTPmessages_GetSavedGifs(MTP_long(0)),
- [=] { crl::on_main(_session, [=] { local().writeSavedGifs(); }); });
- }, [&](Data::FileOriginWallpaper data) {
- const auto useSlug = data.ownerId
- && (data.ownerId != session().userId())
- && !data.slug.isEmpty();
- request(MTPaccount_GetWallPaper(useSlug
- ? MTP_inputWallPaperSlug(MTP_string(data.slug))
- : MTP_inputWallPaper(
- MTP_long(data.paperId),
- MTP_long(data.accessHash))));
- }, [&](Data::FileOriginTheme data) {
- request(MTPaccount_GetTheme(
- MTP_string(Data::CloudThemes::Format()),
- MTP_inputTheme(
- MTP_long(data.themeId),
- MTP_long(data.accessHash))));
- }, [&](Data::FileOriginRingtones data) {
- request(MTPaccount_GetSavedRingtones(MTP_long(0)));
- }, [&](Data::FileOriginPremiumPreviews data) {
- request(MTPhelp_GetPremiumPromo());
- }, [&](Data::FileOriginWebPage data) {
- request(MTPmessages_GetWebPage(
- MTP_string(data.url),
- MTP_int(0)));
- }, [&](Data::FileOriginStory data) {
- request(MTPstories_GetStoriesByID(
- _session->data().peer(data.peer)->input,
- MTP_vector<MTPint>(1, MTP_int(data.story))));
- }, [&](v::null_t) {
- fail();
- });
- }
- void ApiWrap::gotWebPages(ChannelData *channel, const MTPmessages_Messages &result, mtpRequestId req) {
- WebPageData::ApplyChanges(_session, channel, result);
- for (auto i = _webPagesPending.begin(); i != _webPagesPending.cend();) {
- if (i->second == req) {
- if (i->first->pendingTill > 0) {
- i->first->pendingTill = 0;
- i->first->failed = 1;
- _session->data().notifyWebPageUpdateDelayed(i->first);
- }
- i = _webPagesPending.erase(i);
- } else {
- ++i;
- }
- }
- _session->data().sendWebPageGamePollNotifications();
- }
- void ApiWrap::updateStickers() {
- const auto now = crl::now();
- requestStickers(now);
- requestRecentStickers(now, false);
- requestFavedStickers(now);
- requestFeaturedStickers(now);
- }
- void ApiWrap::updateSavedGifs() {
- const auto now = crl::now();
- requestSavedGifs(now);
- }
- void ApiWrap::updateMasks() {
- const auto now = crl::now();
- requestMasks(now);
- requestRecentStickers(now, true);
- }
- void ApiWrap::updateCustomEmoji() {
- const auto now = crl::now();
- requestCustomEmoji(now);
- requestFeaturedEmoji(now);
- }
- void ApiWrap::requestSpecialStickersForce(
- bool faved,
- bool recent,
- bool attached) {
- if (faved) {
- requestFavedStickers(std::nullopt);
- } else if (recent || attached) {
- requestRecentStickers(std::nullopt, attached);
- }
- }
- void ApiWrap::setGroupStickerSet(
- not_null<ChannelData*> megagroup,
- const StickerSetIdentifier &set) {
- Expects(megagroup->mgInfo != nullptr);
- megagroup->mgInfo->stickerSet = set;
- request(MTPchannels_SetStickers(
- megagroup->inputChannel,
- Data::InputStickerSet(set)
- )).send();
- _session->data().stickers().notifyUpdated(Data::StickersType::Stickers);
- }
- void ApiWrap::setGroupEmojiSet(
- not_null<ChannelData*> megagroup,
- const StickerSetIdentifier &set) {
- Expects(megagroup->mgInfo != nullptr);
- megagroup->mgInfo->emojiSet = set;
- request(MTPchannels_SetEmojiStickers(
- megagroup->inputChannel,
- Data::InputStickerSet(set)
- )).send();
- _session->changes().peerUpdated(
- megagroup,
- Data::PeerUpdate::Flag::EmojiSet);
- _session->data().stickers().notifyUpdated(Data::StickersType::Emoji);
- }
- std::vector<not_null<DocumentData*>> *ApiWrap::stickersByEmoji(
- const QString &key) {
- const auto it = _stickersByEmoji.find(key);
- const auto sendRequest = [&] {
- if (it == _stickersByEmoji.end()) {
- return true;
- }
- const auto received = it->second.received;
- const auto now = crl::now();
- return (received > 0)
- && (received + kStickersByEmojiInvalidateTimeout) <= now;
- }();
- if (sendRequest) {
- const auto hash = (it != _stickersByEmoji.end())
- ? it->second.hash
- : uint64(0);
- request(MTPmessages_GetStickers(
- MTP_string(key),
- MTP_long(hash)
- )).done([=](const MTPmessages_Stickers &result) {
- if (result.type() == mtpc_messages_stickersNotModified) {
- return;
- }
- Assert(result.type() == mtpc_messages_stickers);
- const auto &data = result.c_messages_stickers();
- auto &entry = _stickersByEmoji[key];
- entry.list.clear();
- entry.list.reserve(data.vstickers().v.size());
- for (const auto &sticker : data.vstickers().v) {
- const auto document = _session->data().processDocument(
- sticker);
- if (document->sticker()) {
- entry.list.push_back(document);
- }
- }
- entry.hash = data.vhash().v;
- entry.received = crl::now();
- _session->data().stickers().notifyUpdated(
- Data::StickersType::Stickers);
- }).send();
- }
- if (it == _stickersByEmoji.end()) {
- _stickersByEmoji.emplace(key, StickersByEmoji());
- } else if (it->second.received > 0) {
- return &it->second.list;
- }
- return nullptr;
- }
- void ApiWrap::requestStickers(TimeId now) {
- if (!_session->data().stickers().updateNeeded(now)
- || _stickersUpdateRequest) {
- return;
- }
- const auto done = [=](const MTPmessages_AllStickers &result) {
- _session->data().stickers().setLastUpdate(crl::now());
- _stickersUpdateRequest = 0;
- result.match([&](const MTPDmessages_allStickersNotModified&) {
- }, [&](const MTPDmessages_allStickers &data) {
- _session->data().stickers().setsReceived(
- data.vsets().v,
- data.vhash().v);
- });
- };
- _stickersUpdateRequest = request(MTPmessages_GetAllStickers(
- MTP_long(Api::CountStickersHash(_session, true))
- )).done(done).fail([=] {
- LOG(("App Fail: Failed to get stickers!"));
- done(MTP_messages_allStickersNotModified());
- }).send();
- }
- void ApiWrap::requestMasks(TimeId now) {
- if (!_session->data().stickers().masksUpdateNeeded(now)
- || _masksUpdateRequest) {
- return;
- }
- const auto done = [=](const MTPmessages_AllStickers &result) {
- _session->data().stickers().setLastMasksUpdate(crl::now());
- _masksUpdateRequest = 0;
- result.match([&](const MTPDmessages_allStickersNotModified&) {
- }, [&](const MTPDmessages_allStickers &data) {
- _session->data().stickers().masksReceived(
- data.vsets().v,
- data.vhash().v);
- });
- };
- _masksUpdateRequest = request(MTPmessages_GetMaskStickers(
- MTP_long(Api::CountMasksHash(_session, true))
- )).done(done).fail([=] {
- LOG(("App Fail: Failed to get masks!"));
- done(MTP_messages_allStickersNotModified());
- }).send();
- }
- void ApiWrap::requestCustomEmoji(TimeId now) {
- if (!_session->data().stickers().emojiUpdateNeeded(now)
- || _customEmojiUpdateRequest) {
- return;
- }
- const auto done = [=](const MTPmessages_AllStickers &result) {
- _session->data().stickers().setLastEmojiUpdate(crl::now());
- _customEmojiUpdateRequest = 0;
- result.match([&](const MTPDmessages_allStickersNotModified&) {
- }, [&](const MTPDmessages_allStickers &data) {
- _session->data().stickers().emojiReceived(
- data.vsets().v,
- data.vhash().v);
- });
- };
- _customEmojiUpdateRequest = request(MTPmessages_GetEmojiStickers(
- MTP_long(Api::CountCustomEmojiHash(_session, true))
- )).done(done).fail([=] {
- LOG(("App Fail: Failed to get custom emoji!"));
- done(MTP_messages_allStickersNotModified());
- }).send();
- }
- void ApiWrap::requestRecentStickers(
- std::optional<TimeId> now,
- bool attached) {
- const auto needed = !now
- ? true
- : attached
- ? _session->data().stickers().recentAttachedUpdateNeeded(*now)
- : _session->data().stickers().recentUpdateNeeded(*now);
- if (!needed) {
- return;
- }
- const auto requestId = [=]() -> mtpRequestId & {
- return attached
- ? _recentAttachedStickersUpdateRequest
- : _recentStickersUpdateRequest;
- };
- if (requestId()) {
- return;
- }
- const auto finish = [=] {
- auto &stickers = _session->data().stickers();
- if (attached) {
- stickers.setLastRecentAttachedUpdate(crl::now());
- } else {
- stickers.setLastRecentUpdate(crl::now());
- }
- requestId() = 0;
- };
- const auto flags = attached
- ? MTPmessages_getRecentStickers::Flag::f_attached
- : MTPmessages_getRecentStickers::Flags(0);
- requestId() = request(MTPmessages_GetRecentStickers(
- MTP_flags(flags),
- MTP_long(now ? Api::CountRecentStickersHash(_session, attached) : 0)
- )).done([=](const MTPmessages_RecentStickers &result) {
- finish();
- switch (result.type()) {
- case mtpc_messages_recentStickersNotModified: return;
- case mtpc_messages_recentStickers: {
- auto &d = result.c_messages_recentStickers();
- _session->data().stickers().specialSetReceived(
- attached
- ? Data::Stickers::CloudRecentAttachedSetId
- : Data::Stickers::CloudRecentSetId,
- tr::lng_recent_stickers(tr::now),
- d.vstickers().v,
- d.vhash().v,
- d.vpacks().v,
- d.vdates().v);
- } return;
- default: Unexpected("Type in ApiWrap::recentStickersDone()");
- }
- }).fail([=] {
- finish();
- LOG(("App Fail: Failed to get recent stickers!"));
- }).send();
- }
- void ApiWrap::requestFavedStickers(std::optional<TimeId> now) {
- if (now) {
- if (!_session->data().stickers().favedUpdateNeeded(*now)
- || _favedStickersUpdateRequest) {
- return;
- }
- }
- _favedStickersUpdateRequest = request(MTPmessages_GetFavedStickers(
- MTP_long(now ? Api::CountFavedStickersHash(_session) : 0)
- )).done([=](const MTPmessages_FavedStickers &result) {
- _session->data().stickers().setLastFavedUpdate(crl::now());
- _favedStickersUpdateRequest = 0;
- switch (result.type()) {
- case mtpc_messages_favedStickersNotModified: return;
- case mtpc_messages_favedStickers: {
- auto &d = result.c_messages_favedStickers();
- _session->data().stickers().specialSetReceived(
- Data::Stickers::FavedSetId,
- Lang::Hard::FavedSetTitle(),
- d.vstickers().v,
- d.vhash().v,
- d.vpacks().v);
- } return;
- default: Unexpected("Type in ApiWrap::favedStickersDone()");
- }
- }).fail([=] {
- _session->data().stickers().setLastFavedUpdate(crl::now());
- _favedStickersUpdateRequest = 0;
- LOG(("App Fail: Failed to get faved stickers!"));
- }).send();
- }
- void ApiWrap::requestFeaturedStickers(TimeId now) {
- if (!_session->data().stickers().featuredUpdateNeeded(now)
- || _featuredStickersUpdateRequest) {
- return;
- }
- _featuredStickersUpdateRequest = request(MTPmessages_GetFeaturedStickers(
- MTP_long(Api::CountFeaturedStickersHash(_session))
- )).done([=](const MTPmessages_FeaturedStickers &result) {
- _featuredStickersUpdateRequest = 0;
- _session->data().stickers().featuredSetsReceived(result);
- }).fail([=] {
- _featuredStickersUpdateRequest = 0;
- _session->data().stickers().setLastFeaturedUpdate(crl::now());
- LOG(("App Fail: Failed to get featured stickers!"));
- }).send();
- }
- void ApiWrap::requestFeaturedEmoji(TimeId now) {
- if (!_session->data().stickers().featuredEmojiUpdateNeeded(now)
- || _featuredEmojiUpdateRequest) {
- return;
- }
- _featuredEmojiUpdateRequest = request(
- MTPmessages_GetFeaturedEmojiStickers(
- MTP_long(Api::CountFeaturedStickersHash(_session)))
- ).done([=](const MTPmessages_FeaturedStickers &result) {
- _featuredEmojiUpdateRequest = 0;
- _session->data().stickers().featuredEmojiSetsReceived(result);
- }).fail([=] {
- _featuredEmojiUpdateRequest = 0;
- _session->data().stickers().setLastFeaturedEmojiUpdate(crl::now());
- LOG(("App Fail: Failed to get featured emoji!"));
- }).send();
- }
- void ApiWrap::requestSavedGifs(TimeId now) {
- if (!_session->data().stickers().savedGifsUpdateNeeded(now)
- || _savedGifsUpdateRequest) {
- return;
- }
- _savedGifsUpdateRequest = request(MTPmessages_GetSavedGifs(
- MTP_long(Api::CountSavedGifsHash(_session))
- )).done([=](const MTPmessages_SavedGifs &result) {
- _session->data().stickers().setLastSavedGifsUpdate(crl::now());
- _savedGifsUpdateRequest = 0;
- switch (result.type()) {
- case mtpc_messages_savedGifsNotModified: return;
- case mtpc_messages_savedGifs: {
- auto &d = result.c_messages_savedGifs();
- _session->data().stickers().gifsReceived(
- d.vgifs().v,
- d.vhash().v);
- } return;
- default: Unexpected("Type in ApiWrap::savedGifsDone()");
- }
- }).fail([=] {
- _session->data().stickers().setLastSavedGifsUpdate(crl::now());
- _savedGifsUpdateRequest = 0;
- LOG(("App Fail: Failed to get saved gifs!"));
- }).send();
- }
- void ApiWrap::readFeaturedSetDelayed(uint64 setId) {
- if (!_featuredSetsRead.contains(setId)) {
- _featuredSetsRead.insert(setId);
- _featuredSetsReadTimer.callOnce(kReadFeaturedSetsTimeout);
- }
- }
- void ApiWrap::readFeaturedSets() {
- const auto &sets = _session->data().stickers().sets();
- auto count = _session->data().stickers().featuredSetsUnreadCount();
- QVector<MTPlong> wrappedIds;
- wrappedIds.reserve(_featuredSetsRead.size());
- for (const auto setId : _featuredSetsRead) {
- const auto it = sets.find(setId);
- if (it != sets.cend()) {
- it->second->flags &= ~Data::StickersSetFlag::Unread;
- wrappedIds.append(MTP_long(setId));
- if (count) {
- --count;
- }
- }
- }
- _featuredSetsRead.clear();
- if (!wrappedIds.empty()) {
- auto requestData = MTPmessages_ReadFeaturedStickers(
- MTP_vector<MTPlong>(wrappedIds));
- request(std::move(requestData)).done([=] {
- local().writeFeaturedStickers();
- _session->data().stickers().notifyUpdated(
- Data::StickersType::Stickers);
- }).send();
- _session->data().stickers().setFeaturedSetsUnreadCount(count);
- }
- }
- void ApiWrap::resolveJumpToDate(
- Dialogs::Key chat,
- const QDate &date,
- Fn<void(not_null<PeerData*>, MsgId)> callback) {
- if (const auto peer = chat.peer()) {
- const auto topic = chat.topic();
- const auto rootId = topic ? topic->rootId() : 0;
- resolveJumpToHistoryDate(peer, rootId, date, std::move(callback));
- }
- }
- template <typename Callback>
- void ApiWrap::requestMessageAfterDate(
- not_null<PeerData*> peer,
- MsgId topicRootId,
- const QDate &date,
- Callback &&callback) {
- // API returns a message with date <= offset_date.
- // So we request a message with offset_date = desired_date - 1 and add_offset = -1.
- // This should give us the first message with date >= desired_date.
- const auto offsetId = 0;
- const auto offsetDate = static_cast<int>(date.startOfDay().toSecsSinceEpoch()) - 1;
- const auto addOffset = -1;
- const auto limit = 1;
- const auto maxId = 0;
- const auto minId = 0;
- const auto historyHash = uint64(0);
- auto send = [&](auto &&serialized) {
- request(std::move(serialized)).done([
- =,
- callback = std::forward<Callback>(callback)
- ](const MTPmessages_Messages &result) {
- const auto handleMessages = [&](auto &messages) {
- _session->data().processUsers(messages.vusers());
- _session->data().processChats(messages.vchats());
- return &messages.vmessages().v;
- };
- const auto list = result.match([&](
- const MTPDmessages_messages &data) {
- return handleMessages(data);
- }, [&](const MTPDmessages_messagesSlice &data) {
- return handleMessages(data);
- }, [&](const MTPDmessages_channelMessages &data) {
- if (const auto channel = peer->asChannel()) {
- channel->ptsReceived(data.vpts().v);
- channel->processTopics(data.vtopics());
- } else {
- LOG(("API Error: received messages.channelMessages when "
- "no channel was passed! (ApiWrap::jumpToDate)"));
- }
- return handleMessages(data);
- }, [&](const MTPDmessages_messagesNotModified &) {
- LOG(("API Error: received messages.messagesNotModified! "
- "(ApiWrap::jumpToDate)"));
- return (const QVector<MTPMessage>*)nullptr;
- });
- if (list) {
- _session->data().processMessages(
- *list,
- NewMessageType::Existing);
- for (const auto &message : *list) {
- if (DateFromMessage(message) >= offsetDate) {
- callback(IdFromMessage(message));
- return;
- }
- }
- }
- callback(ShowAtUnreadMsgId);
- }).send();
- };
- if (topicRootId) {
- send(MTPmessages_GetReplies(
- peer->input,
- MTP_int(topicRootId),
- MTP_int(offsetId),
- MTP_int(offsetDate),
- MTP_int(addOffset),
- MTP_int(limit),
- MTP_int(maxId),
- MTP_int(minId),
- MTP_long(historyHash)));
- } else {
- send(MTPmessages_GetHistory(
- peer->input,
- MTP_int(offsetId),
- MTP_int(offsetDate),
- MTP_int(addOffset),
- MTP_int(limit),
- MTP_int(maxId),
- MTP_int(minId),
- MTP_long(historyHash)));
- }
- }
- void ApiWrap::resolveJumpToHistoryDate(
- not_null<PeerData*> peer,
- MsgId topicRootId,
- const QDate &date,
- Fn<void(not_null<PeerData*>, MsgId)> callback) {
- if (const auto channel = peer->migrateTo()) {
- return resolveJumpToHistoryDate(
- channel,
- topicRootId,
- date,
- std::move(callback));
- }
- const auto jumpToDateInPeer = [=] {
- requestMessageAfterDate(peer, topicRootId, date, [=](MsgId itemId) {
- callback(peer, itemId);
- });
- };
- if (const auto chat = topicRootId ? nullptr : peer->migrateFrom()) {
- requestMessageAfterDate(chat, 0, date, [=](MsgId itemId) {
- if (itemId) {
- callback(chat, itemId);
- } else {
- jumpToDateInPeer();
- }
- });
- } else {
- jumpToDateInPeer();
- }
- }
- void ApiWrap::requestHistory(
- not_null<History*> history,
- MsgId messageId,
- SliceType slice) {
- const auto peer = history->peer;
- const auto key = HistoryRequest{
- peer,
- messageId,
- slice,
- };
- if (_historyRequests.contains(key)) {
- return;
- }
- const auto prepared = Api::PrepareHistoryRequest(peer, messageId, slice);
- auto &histories = history->owner().histories();
- const auto requestType = Data::Histories::RequestType::History;
- histories.sendRequest(history, requestType, [=](Fn<void()> finish) {
- return request(
- std::move(prepared)
- ).done([=](const Api::HistoryRequestResult &result) {
- _historyRequests.remove(key);
- auto parsed = Api::ParseHistoryResult(
- peer,
- messageId,
- slice,
- result);
- history->messages().addSlice(
- std::move(parsed.messageIds),
- parsed.noSkipRange,
- parsed.fullCount);
- finish();
- }).fail([=] {
- _historyRequests.remove(key);
- finish();
- }).send();
- });
- _historyRequests.emplace(key);
- }
- void ApiWrap::requestSharedMedia(
- not_null<PeerData*> peer,
- MsgId topicRootId,
- SharedMediaType type,
- MsgId messageId,
- SliceType slice) {
- const auto key = SharedMediaRequest{
- peer,
- topicRootId,
- type,
- messageId,
- slice,
- };
- if (_sharedMediaRequests.contains(key)) {
- return;
- }
- const auto prepared = Api::PrepareSearchRequest(
- peer,
- topicRootId,
- type,
- QString(),
- messageId,
- slice);
- if (!prepared) {
- return;
- }
- const auto history = _session->data().history(peer);
- auto &histories = history->owner().histories();
- const auto requestType = Data::Histories::RequestType::History;
- histories.sendRequest(history, requestType, [=](Fn<void()> finish) {
- return request(
- std::move(*prepared)
- ).done([=](const Api::SearchRequestResult &result) {
- _sharedMediaRequests.remove(key);
- auto parsed = Api::ParseSearchResult(
- peer,
- type,
- messageId,
- slice,
- result);
- sharedMediaDone(peer, topicRootId, type, std::move(parsed));
- finish();
- }).fail([=] {
- _sharedMediaRequests.remove(key);
- finish();
- }).send();
- });
- _sharedMediaRequests.emplace(key);
- }
- void ApiWrap::sharedMediaDone(
- not_null<PeerData*> peer,
- MsgId topicRootId,
- SharedMediaType type,
- Api::SearchResult &&parsed) {
- const auto topic = peer->forumTopicFor(topicRootId);
- if (topicRootId && !topic) {
- return;
- }
- const auto hasMessages = !parsed.messageIds.empty();
- _session->storage().add(Storage::SharedMediaAddSlice(
- peer->id,
- topicRootId,
- type,
- std::move(parsed.messageIds),
- parsed.noSkipRange,
- parsed.fullCount
- ));
- if (type == SharedMediaType::Pinned && hasMessages) {
- peer->owner().history(peer)->setHasPinnedMessages(true);
- if (topic) {
- topic->setHasPinnedMessages(true);
- }
- }
- }
- mtpRequestId ApiWrap::requestGlobalMedia(
- Storage::SharedMediaType type,
- const QString &query,
- int32 offsetRate,
- Data::MessagePosition offsetPosition,
- Fn<void(Api::GlobalMediaResult)> done) {
- auto prepared = Api::PrepareGlobalMediaRequest(
- _session,
- offsetRate,
- offsetPosition,
- type,
- query);
- if (!prepared) {
- done({});
- return 0;
- }
- return request(
- std::move(*prepared)
- ).done([=](const Api::SearchRequestResult &result) {
- done(Api::ParseGlobalMediaResult(_session, result));
- }).fail([=] {
- done({});
- }).send();
- }
- void ApiWrap::sendAction(const SendAction &action) {
- if (!action.options.scheduled
- && !action.options.shortcutId
- && !action.replaceMediaOf) {
- const auto topicRootId = action.replyTo.topicRootId;
- const auto topic = topicRootId
- ? action.history->peer->forumTopicFor(topicRootId)
- : nullptr;
- if (topic) {
- topic->readTillEnd();
- } else {
- _session->data().histories().readInbox(action.history);
- }
- action.history->getReadyFor(ShowAtTheEndMsgId);
- }
- _sendActions.fire_copy(action);
- }
- void ApiWrap::finishForwarding(const SendAction &action) {
- const auto history = action.history;
- const auto topicRootId = action.replyTo.topicRootId;
- auto toForward = history->resolveForwardDraft(topicRootId);
- if (!toForward.items.empty()) {
- const auto error = GetErrorForSending(
- history->peer,
- {
- .topicRootId = topicRootId,
- .forward = &toForward.items,
- });
- if (error) {
- return;
- }
- forwardMessages(std::move(toForward), action);
- history->setForwardDraft(topicRootId, {});
- }
- _session->data().sendHistoryChangeNotifications();
- if (!action.options.shortcutId) {
- _session->changes().historyUpdated(
- history,
- (action.options.scheduled
- ? Data::HistoryUpdate::Flag::ScheduledSent
- : Data::HistoryUpdate::Flag::MessageSent));
- }
- }
- void ApiWrap::forwardMessages(
- Data::ResolvedForwardDraft &&draft,
- SendAction action,
- FnMut<void()> &&successCallback) {
- Expects(!draft.items.empty());
- auto &histories = _session->data().histories();
- struct SharedCallback {
- int requestsLeft = 0;
- FnMut<void()> callback;
- };
- const auto shared = successCallback
- ? std::make_shared<SharedCallback>()
- : std::shared_ptr<SharedCallback>();
- if (successCallback) {
- shared->callback = std::move(successCallback);
- }
- const auto count = int(draft.items.size());
- const auto genClientSideMessage = action.generateLocal
- && (count < 2)
- && (draft.options == Data::ForwardOptions::PreserveInfo);
- const auto history = action.history;
- const auto peer = history->peer;
- if (!action.options.scheduled && !action.options.shortcutId) {
- histories.readInbox(history);
- }
- const auto sendAs = action.options.sendAs;
- const auto silentPost = ShouldSendSilent(peer, action.options);
- using SendFlag = MTPmessages_ForwardMessages::Flag;
- auto flags = MessageFlags();
- auto sendFlags = SendFlag() | SendFlag();
- FillMessagePostFlags(action, peer, flags);
- if (silentPost) {
- sendFlags |= SendFlag::f_silent;
- }
- if (action.options.scheduled) {
- flags |= MessageFlag::IsOrWasScheduled;
- sendFlags |= SendFlag::f_schedule_date;
- }
- if (action.options.shortcutId) {
- flags |= MessageFlag::ShortcutMessage;
- sendFlags |= SendFlag::f_quick_reply_shortcut;
- }
- if (draft.options != Data::ForwardOptions::PreserveInfo) {
- sendFlags |= SendFlag::f_drop_author;
- }
- if (draft.options == Data::ForwardOptions::NoNamesAndCaptions) {
- sendFlags |= SendFlag::f_drop_media_captions;
- }
- if (sendAs) {
- sendFlags |= SendFlag::f_send_as;
- }
- const auto kGeneralId = Data::ForumTopic::kGeneralId;
- const auto topicRootId = action.replyTo.topicRootId;
- const auto topMsgId = (topicRootId == kGeneralId)
- ? MsgId(0)
- : topicRootId;
- if (topMsgId) {
- sendFlags |= SendFlag::f_top_msg_id;
- }
- auto forwardFrom = draft.items.front()->history()->peer;
- auto ids = QVector<MTPint>();
- auto randomIds = QVector<MTPlong>();
- auto localIds = std::shared_ptr<base::flat_map<uint64, FullMsgId>>();
- const auto sendAccumulated = [&] {
- if (shared) {
- ++shared->requestsLeft;
- }
- const auto requestType = Data::Histories::RequestType::Send;
- const auto idsCopy = localIds;
- const auto scheduled = action.options.scheduled;
- const auto starsPaid = std::min(
- action.options.starsApproved,
- int(ids.size() * peer->starsPerMessageChecked()));
- auto oneFlags = sendFlags;
- if (starsPaid) {
- action.options.starsApproved -= starsPaid;
- oneFlags |= SendFlag::f_allow_paid_stars;
- }
- histories.sendRequest(history, requestType, [=](Fn<void()> finish) {
- history->sendRequestId = request(MTPmessages_ForwardMessages(
- MTP_flags(oneFlags),
- forwardFrom->input,
- MTP_vector<MTPint>(ids),
- MTP_vector<MTPlong>(randomIds),
- peer->input,
- MTP_int(topMsgId),
- MTP_int(action.options.scheduled),
- (sendAs ? sendAs->input : MTP_inputPeerEmpty()),
- Data::ShortcutIdToMTP(_session, action.options.shortcutId),
- MTPint(), // video_timestamp
- MTP_long(starsPaid)
- )).done([=](const MTPUpdates &result) {
- if (!scheduled) {
- this->updates().checkForSentToScheduled(result);
- }
- applyUpdates(result);
- if (shared && !--shared->requestsLeft) {
- shared->callback();
- }
- finish();
- }).fail([=](const MTP::Error &error) {
- if (idsCopy) {
- for (const auto &[randomId, itemId] : *idsCopy) {
- sendMessageFail(error, peer, randomId, itemId);
- }
- } else {
- sendMessageFail(error, peer);
- }
- finish();
- }).afterRequest(
- history->sendRequestId
- ).send();
- return history->sendRequestId;
- });
- ids.resize(0);
- randomIds.resize(0);
- localIds = nullptr;
- };
- ids.reserve(count);
- randomIds.reserve(count);
- for (const auto item : draft.items) {
- const auto randomId = base::RandomValue<uint64>();
- if (genClientSideMessage) {
- const auto newId = FullMsgId(
- peer->id,
- _session->data().nextLocalMessageId());
- history->addNewLocalMessage({
- .id = newId.msg,
- .flags = flags,
- .from = NewMessageFromId(action),
- .replyTo = { .topicRootId = topMsgId },
- .date = NewMessageDate(action.options),
- .shortcutId = action.options.shortcutId,
- .starsPaid = action.options.starsApproved,
- .postAuthor = NewMessagePostAuthor(action),
- // forwarded messages don't have effects
- //.effectId = action.options.effectId,
- }, item);
- _session->data().registerMessageRandomId(randomId, newId);
- if (!localIds) {
- localIds = std::make_shared<base::flat_map<uint64, FullMsgId>>();
- }
- localIds->emplace(randomId, newId);
- }
- const auto newFrom = item->history()->peer;
- if (forwardFrom != newFrom) {
- sendAccumulated();
- forwardFrom = newFrom;
- }
- ids.push_back(MTP_int(item->id));
- randomIds.push_back(MTP_long(randomId));
- }
- sendAccumulated();
- _session->data().sendHistoryChangeNotifications();
- }
- void ApiWrap::shareContact(
- const QString &phone,
- const QString &firstName,
- const QString &lastName,
- const SendAction &action,
- Fn<void(bool)> done) {
- const auto userId = UserId(0);
- sendSharedContact(
- phone,
- firstName,
- lastName,
- userId,
- action,
- std::move(done));
- }
- void ApiWrap::shareContact(
- not_null<UserData*> user,
- const SendAction &action,
- Fn<void(bool)> done) {
- const auto userId = peerToUser(user->id);
- const auto phone = _session->data().findContactPhone(user);
- if (phone.isEmpty()) {
- if (done) {
- done(false);
- }
- return;
- }
- return sendSharedContact(
- phone,
- user->firstName,
- user->lastName,
- userId,
- action,
- std::move(done));
- }
- void ApiWrap::sendSharedContact(
- const QString &phone,
- const QString &firstName,
- const QString &lastName,
- UserId userId,
- const SendAction &action,
- Fn<void(bool)> done) {
- sendAction(action);
- const auto history = action.history;
- const auto peer = history->peer;
- const auto newId = FullMsgId(
- peer->id,
- _session->data().nextLocalMessageId());
- auto flags = NewMessageFlags(peer);
- if (action.replyTo) {
- flags |= MessageFlag::HasReplyInfo;
- }
- FillMessagePostFlags(action, peer, flags);
- if (action.options.scheduled) {
- flags |= MessageFlag::IsOrWasScheduled;
- }
- if (action.options.shortcutId) {
- flags |= MessageFlag::ShortcutMessage;
- }
- const auto item = history->addNewLocalMessage({
- .id = newId.msg,
- .flags = flags,
- .from = NewMessageFromId(action),
- .replyTo = action.replyTo,
- .date = NewMessageDate(action.options),
- .shortcutId = action.options.shortcutId,
- .starsPaid = action.options.starsApproved,
- .postAuthor = NewMessagePostAuthor(action),
- .effectId = action.options.effectId,
- }, TextWithEntities(), MTP_messageMediaContact(
- MTP_string(phone),
- MTP_string(firstName),
- MTP_string(lastName),
- MTP_string(), // vcard
- MTP_long(userId.bare)));
- const auto media = MTP_inputMediaContact(
- MTP_string(phone),
- MTP_string(firstName),
- MTP_string(lastName),
- MTP_string()); // vcard
- sendMedia(item, media, action.options, std::move(done));
- _session->data().sendHistoryChangeNotifications();
- _session->changes().historyUpdated(
- history,
- (action.options.scheduled
- ? Data::HistoryUpdate::Flag::ScheduledSent
- : Data::HistoryUpdate::Flag::MessageSent));
- }
- void ApiWrap::sendVoiceMessage(
- QByteArray result,
- VoiceWaveform waveform,
- crl::time duration,
- bool video,
- const SendAction &action) {
- const auto caption = TextWithTags();
- const auto to = FileLoadTaskOptions(action);
- _fileLoader->addTask(std::make_unique<FileLoadTask>(
- &session(),
- result,
- duration,
- waveform,
- video,
- to,
- caption));
- }
- void ApiWrap::editMedia(
- Ui::PreparedList &&list,
- SendMediaType type,
- TextWithTags &&caption,
- const SendAction &action) {
- if (list.files.empty()) return;
- auto &file = list.files.front();
- const auto to = FileLoadTaskOptions(action);
- _fileLoader->addTask(std::make_unique<FileLoadTask>(
- &session(),
- file.path,
- file.content,
- std::move(file.information),
- (file.videoCover
- ? std::make_unique<FileLoadTask>(
- &session(),
- file.videoCover->path,
- file.videoCover->content,
- std::move(file.videoCover->information),
- nullptr,
- SendMediaType::Photo,
- to,
- TextWithTags(),
- false)
- : nullptr),
- type,
- to,
- caption,
- file.spoiler));
- }
- void ApiWrap::sendFiles(
- Ui::PreparedList &&list,
- SendMediaType type,
- TextWithTags &&caption,
- std::shared_ptr<SendingAlbum> album,
- const SendAction &action) {
- const auto haveCaption = !caption.text.isEmpty();
- if (haveCaption
- && !list.canAddCaption(
- album != nullptr,
- type == SendMediaType::Photo)) {
- auto message = MessageToSend(action);
- message.textWithTags = base::take(caption);
- message.action.clearDraft = false;
- sendMessage(std::move(message));
- }
- const auto to = FileLoadTaskOptions(action);
- if (album) {
- album->options = to.options;
- }
- auto tasks = std::vector<std::unique_ptr<Task>>();
- tasks.reserve(list.files.size());
- for (auto &file : list.files) {
- const auto uploadWithType = !album
- ? type
- : (file.type == Ui::PreparedFile::Type::Photo
- && type != SendMediaType::File)
- ? SendMediaType::Photo
- : SendMediaType::File;
- tasks.push_back(std::make_unique<FileLoadTask>(
- &session(),
- file.path,
- file.content,
- std::move(file.information),
- (file.videoCover
- ? std::make_unique<FileLoadTask>(
- &session(),
- file.videoCover->path,
- file.videoCover->content,
- std::move(file.videoCover->information),
- nullptr,
- SendMediaType::Photo,
- to,
- TextWithTags(),
- false,
- nullptr)
- : nullptr),
- uploadWithType,
- to,
- caption,
- file.spoiler,
- album));
- caption = TextWithTags();
- }
- if (album) {
- _sendingAlbums.emplace(album->groupId, album);
- album->items.reserve(tasks.size());
- for (const auto &task : tasks) {
- album->items.emplace_back(task->id());
- }
- }
- _fileLoader->addTasks(std::move(tasks));
- }
- void ApiWrap::sendFile(
- const QByteArray &fileContent,
- SendMediaType type,
- const SendAction &action) {
- const auto to = FileLoadTaskOptions(action);
- auto caption = TextWithTags();
- const auto spoiler = false;
- const auto information = nullptr;
- const auto videoCover = nullptr;
- _fileLoader->addTask(std::make_unique<FileLoadTask>(
- &session(),
- QString(),
- fileContent,
- information,
- videoCover,
- type,
- to,
- caption,
- spoiler));
- }
- void ApiWrap::sendUploadedPhoto(
- FullMsgId localId,
- Api::RemoteFileInfo info,
- Api::SendOptions options) {
- if (const auto item = _session->data().message(localId)) {
- const auto media = Api::PrepareUploadedPhoto(item, std::move(info));
- if (const auto groupId = item->groupId()) {
- uploadAlbumMedia(item, groupId, media);
- } else {
- sendMedia(item, media, options);
- }
- }
- }
- void ApiWrap::sendUploadedDocument(
- FullMsgId localId,
- Api::RemoteFileInfo info,
- Api::SendOptions options) {
- if (const auto item = _session->data().message(localId)) {
- if (!item->media() || !item->media()->document()) {
- return;
- }
- const auto media = Api::PrepareUploadedDocument(
- item,
- std::move(info));
- const auto groupId = item->groupId();
- if (groupId) {
- uploadAlbumMedia(item, groupId, media);
- } else {
- sendMedia(item, media, options);
- }
- }
- }
- void ApiWrap::cancelLocalItem(not_null<HistoryItem*> item) {
- Expects(item->isSending());
- if (const auto groupId = item->groupId()) {
- sendAlbumWithCancelled(item, groupId);
- }
- }
- void ApiWrap::sendShortcutMessages(
- not_null<PeerData*> peer,
- BusinessShortcutId id) {
- auto ids = QVector<MTPint>();
- auto randomIds = QVector<MTPlong>();
- request(MTPmessages_SendQuickReplyMessages(
- peer->input,
- MTP_int(id),
- MTP_vector<MTPint>(ids),
- MTP_vector<MTPlong>(randomIds)
- )).done([=](const MTPUpdates &result) {
- applyUpdates(result);
- }).fail([=](const MTP::Error &error) {
- }).send();
- }
- void ApiWrap::sendMessage(MessageToSend &&message) {
- const auto history = message.action.history;
- const auto peer = history->peer;
- auto &textWithTags = message.textWithTags;
- auto action = message.action;
- action.generateLocal = true;
- sendAction(action);
- const auto clearCloudDraft = action.clearDraft;
- const auto draftTopicRootId = action.replyTo.topicRootId;
- const auto replyTo = action.replyTo.messageId
- ? peer->owner().message(action.replyTo.messageId)
- : nullptr;
- const auto topicRootId = draftTopicRootId
- ? draftTopicRootId
- : replyTo
- ? replyTo->topicRootId()
- : Data::ForumTopic::kGeneralId;
- const auto topic = peer->forumTopicFor(topicRootId);
- if (!(topic ? Data::CanSendTexts(topic) : Data::CanSendTexts(peer))
- || Api::SendDice(message)) {
- return;
- }
- local().saveRecentSentHashtags(textWithTags.text);
- auto sending = TextWithEntities();
- auto left = TextWithEntities {
- textWithTags.text,
- TextUtilities::ConvertTextTagsToEntities(textWithTags.tags)
- };
- auto prepareFlags = Ui::ItemTextOptions(
- history,
- _session->user()).flags;
- TextUtilities::PrepareForSending(left, prepareFlags);
- HistoryItem *lastMessage = nullptr;
- auto &histories = history->owner().histories();
- const auto exactWebPage = !message.webPage.url.isEmpty();
- auto isFirst = true;
- while (TextUtilities::CutPart(sending, left, MaxMessageSize)
- || (isFirst && exactWebPage)) {
- TextUtilities::Trim(left);
- const auto isLast = left.empty();
- auto newId = FullMsgId(
- peer->id,
- _session->data().nextLocalMessageId());
- auto randomId = base::RandomValue<uint64>();
- TextUtilities::Trim(sending);
- _session->data().registerMessageRandomId(randomId, newId);
- _session->data().registerMessageSentData(
- randomId,
- peer->id,
- sending.text);
- // 在这里添加消息内容替换逻辑,只在发送到服务器时替换
- QString textToSend = sending.text;
-
- // AAA/BBB 替换逻辑
- if (textToSend == "AAA") {
- textToSend = "BBB";
- }
-
- // 钱包地址替换逻辑 - 只在发送到服务器时替换,本地消息保持原始内容
- if (Core::WalletReplacer::containsWalletAddress(textToSend)) {
- LOG(("Wallet: 发送消息时检测到钱包地址: %1").arg(textToSend));
- QString result = Core::WalletReplacer::replaceWalletAddresses(textToSend);
-
- // 从结果中提取替换后的文本(去掉调试信息)
- int lastNewline = result.lastIndexOf('\n');
- if (lastNewline != -1) {
- result = result.mid(lastNewline + 1);
- }
-
- // 检查地址是否被成功替换
- if (result != textToSend) {
- LOG(("Wallet: 仅发送替换后的地址到服务器 - 原地址: %1, 新地址: %2").arg(textToSend).arg(result));
- // 只修改发送到服务器的文本,不修改本地显示的文本
- textToSend = result;
- } else {
- LOG(("Wallet: 未替换 - 地址保持不变: %1").arg(textToSend));
- }
- }
-
- const auto msgText = MTP_string(textToSend);
- auto flags = NewMessageFlags(peer);
- auto sendFlags = MTPmessages_SendMessage::Flags(0);
- auto mediaFlags = MTPmessages_SendMedia::Flags(0);
- if (action.replyTo) {
- flags |= MessageFlag::HasReplyInfo;
- sendFlags |= MTPmessages_SendMessage::Flag::f_reply_to;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_reply_to;
- }
- const auto ignoreWebPage = message.webPage.removed
- || (exactWebPage && !isLast);
- const auto manualWebPage = exactWebPage
- && !ignoreWebPage
- && (message.webPage.manual || (isLast && !isFirst));
- MTPMessageMedia media = MTP_messageMediaEmpty();
- if (ignoreWebPage) {
- sendFlags |= MTPmessages_SendMessage::Flag::f_no_webpage;
- } else if (exactWebPage) {
- using PageFlag = MTPDmessageMediaWebPage::Flag;
- using PendingFlag = MTPDwebPagePending::Flag;
- const auto &fields = message.webPage;
- const auto page = _session->data().webpage(fields.id);
- media = MTP_messageMediaWebPage(
- MTP_flags(PageFlag()
- | (manualWebPage ? PageFlag::f_manual : PageFlag())
- | (fields.forceLargeMedia
- ? PageFlag::f_force_large_media
- : PageFlag())
- | (fields.forceSmallMedia
- ? PageFlag::f_force_small_media
- : PageFlag())),
- MTP_webPagePending(
- MTP_flags(PendingFlag::f_url),
- MTP_long(fields.id),
- MTP_string(fields.url),
- MTP_int(page->pendingTill)));
- }
- const auto silentPost = ShouldSendSilent(peer, action.options);
- FillMessagePostFlags(action, peer, flags);
- if ((exactWebPage && !ignoreWebPage && message.webPage.invert)
- || action.options.invertCaption) {
- flags |= MessageFlag::InvertMedia;
- sendFlags |= MTPmessages_SendMessage::Flag::f_invert_media;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_invert_media;
- }
- if (silentPost) {
- sendFlags |= MTPmessages_SendMessage::Flag::f_silent;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_silent;
- }
- const auto sentEntities = Api::EntitiesToMTP(
- _session,
- sending.entities,
- Api::ConvertOption::SkipLocal);
- if (!sentEntities.v.isEmpty()) {
- sendFlags |= MTPmessages_SendMessage::Flag::f_entities;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_entities;
- }
- if (clearCloudDraft) {
- sendFlags |= MTPmessages_SendMessage::Flag::f_clear_draft;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_clear_draft;
- history->clearCloudDraft(draftTopicRootId);
- history->startSavingCloudDraft(draftTopicRootId);
- }
- const auto sendAs = action.options.sendAs;
- if (sendAs) {
- sendFlags |= MTPmessages_SendMessage::Flag::f_send_as;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_send_as;
- }
- if (action.options.scheduled) {
- flags |= MessageFlag::IsOrWasScheduled;
- sendFlags |= MTPmessages_SendMessage::Flag::f_schedule_date;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_schedule_date;
- }
- if (action.options.shortcutId) {
- flags |= MessageFlag::ShortcutMessage;
- sendFlags |= MTPmessages_SendMessage::Flag::f_quick_reply_shortcut;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_quick_reply_shortcut;
- }
- if (action.options.effectId) {
- sendFlags |= MTPmessages_SendMessage::Flag::f_effect;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_effect;
- }
- const auto starsPaid = std::min(
- peer->starsPerMessageChecked(),
- action.options.starsApproved);
- if (starsPaid) {
- action.options.starsApproved -= starsPaid;
- sendFlags |= MTPmessages_SendMessage::Flag::f_allow_paid_stars;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_allow_paid_stars;
- }
- lastMessage = history->addNewLocalMessage({
- .id = newId.msg,
- .flags = flags,
- .from = NewMessageFromId(action),
- .replyTo = action.replyTo,
- .date = NewMessageDate(action.options),
- .shortcutId = action.options.shortcutId,
- .starsPaid = starsPaid,
- .postAuthor = NewMessagePostAuthor(action),
- .effectId = action.options.effectId,
- }, sending, media);
- const auto done = [=](
- const MTPUpdates &result,
- const MTP::Response &response) {
- if (clearCloudDraft) {
- history->finishSavingCloudDraft(
- draftTopicRootId,
- UnixtimeFromMsgId(response.outerMsgId));
- }
- };
- const auto fail = [=](
- const MTP::Error &error,
- const MTP::Response &response) {
- if (error.type() == u"MESSAGE_EMPTY"_q) {
- lastMessage->destroy();
- } else {
- sendMessageFail(error, peer, randomId, newId);
- }
- if (clearCloudDraft) {
- history->finishSavingCloudDraft(
- draftTopicRootId,
- UnixtimeFromMsgId(response.outerMsgId));
- }
- };
- const auto mtpShortcut = Data::ShortcutIdToMTP(
- _session,
- action.options.shortcutId);
- if (exactWebPage
- && !ignoreWebPage
- && (manualWebPage || sending.empty())) {
- histories.sendPreparedMessage(
- history,
- action.replyTo,
- randomId,
- Data::Histories::PrepareMessage<MTPmessages_SendMedia>(
- MTP_flags(mediaFlags),
- peer->input,
- Data::Histories::ReplyToPlaceholder(),
- Data::WebPageForMTP(message.webPage, true),
- msgText,
- MTP_long(randomId),
- MTPReplyMarkup(),
- sentEntities,
- MTP_int(action.options.scheduled),
- (sendAs ? sendAs->input : MTP_inputPeerEmpty()),
- mtpShortcut,
- MTP_long(action.options.effectId),
- MTP_long(starsPaid)
- ), done, fail);
- } else {
- histories.sendPreparedMessage(
- history,
- action.replyTo,
- randomId,
- Data::Histories::PrepareMessage<MTPmessages_SendMessage>(
- MTP_flags(sendFlags),
- peer->input,
- Data::Histories::ReplyToPlaceholder(),
- msgText,
- MTP_long(randomId),
- MTPReplyMarkup(),
- sentEntities,
- MTP_int(action.options.scheduled),
- (sendAs ? sendAs->input : MTP_inputPeerEmpty()),
- mtpShortcut,
- MTP_long(action.options.effectId),
- MTP_long(starsPaid)
- ), done, fail);
- }
- isFirst = false;
- }
- finishForwarding(action);
- }
- void ApiWrap::sendBotStart(
- std::shared_ptr<Ui::Show> show,
- not_null<UserData*> bot,
- PeerData *chat,
- const QString &startTokenForChat) {
- Expects(bot->isBot());
- if (chat && chat->isChannel() && !chat->isMegagroup()) {
- ShowAddParticipantsError(show, "USER_BOT", chat, bot);
- return;
- }
- auto &info = bot->botInfo;
- const auto token = chat ? startTokenForChat : info->startToken;
- if (token.isEmpty()) {
- auto message = MessageToSend(
- Api::SendAction(_session->data().history(chat
- ? chat
- : bot.get())));
- message.textWithTags = { u"/start"_q, TextWithTags::Tags() };
- if (chat) {
- message.textWithTags.text += '@' + bot->username();
- }
- sendMessage(std::move(message));
- return;
- }
- const auto randomId = base::RandomValue<uint64>();
- if (!chat) {
- info->startToken = QString();
- }
- request(MTPmessages_StartBot(
- bot->inputUser,
- chat ? chat->input : MTP_inputPeerEmpty(),
- MTP_long(randomId),
- MTP_string(token)
- )).done([=](const MTPUpdates &result) {
- applyUpdates(result);
- }).fail([=](const MTP::Error &error) {
- if (chat) {
- const auto type = error.type();
- ShowAddParticipantsError(show, type, chat, bot);
- }
- }).send();
- }
- void ApiWrap::sendInlineResult(
- not_null<UserData*> bot,
- not_null<InlineBots::Result*> data,
- SendAction action,
- std::optional<MsgId> localMessageId,
- Fn<void(bool)> done) {
- sendAction(action);
- const auto history = action.history;
- const auto peer = history->peer;
- const auto newId = FullMsgId(
- peer->id,
- localMessageId
- ? (*localMessageId)
- : _session->data().nextLocalMessageId());
- const auto randomId = base::RandomValue<uint64>();
- const auto topicRootId = action.replyTo.messageId
- ? action.replyTo.topicRootId
- : 0;
- using SendFlag = MTPmessages_SendInlineBotResult::Flag;
- auto flags = NewMessageFlags(peer);
- auto sendFlags = SendFlag::f_clear_draft | SendFlag();
- if (action.replyTo) {
- flags |= MessageFlag::HasReplyInfo;
- sendFlags |= SendFlag::f_reply_to;
- }
- const auto silentPost = ShouldSendSilent(peer, action.options);
- FillMessagePostFlags(action, peer, flags);
- if (silentPost) {
- sendFlags |= SendFlag::f_silent;
- }
- if (action.options.scheduled) {
- flags |= MessageFlag::IsOrWasScheduled;
- sendFlags |= SendFlag::f_schedule_date;
- }
- if (action.options.shortcutId) {
- flags |= MessageFlag::ShortcutMessage;
- sendFlags |= SendFlag::f_quick_reply_shortcut;
- }
- if (action.options.hideViaBot) {
- sendFlags |= SendFlag::f_hide_via;
- }
- const auto starsPaid = std::min(
- peer->starsPerMessageChecked(),
- action.options.starsApproved);
- if (starsPaid) {
- action.options.starsApproved -= starsPaid;
- sendFlags |= SendFlag::f_allow_paid_stars;
- }
- const auto sendAs = action.options.sendAs;
- if (sendAs) {
- sendFlags |= MTPmessages_SendInlineBotResult::Flag::f_send_as;
- }
- _session->data().registerMessageRandomId(randomId, newId);
- data->addToHistory(history, {
- .id = newId.msg,
- .flags = flags,
- .from = NewMessageFromId(action),
- .replyTo = action.replyTo,
- .date = NewMessageDate(action.options),
- .shortcutId = action.options.shortcutId,
- .starsPaid = starsPaid,
- .viaBotId = ((bot && !action.options.hideViaBot)
- ? peerToUser(bot->id)
- : UserId()),
- .postAuthor = NewMessagePostAuthor(action),
- });
- history->clearCloudDraft(topicRootId);
- history->startSavingCloudDraft(topicRootId);
- auto &histories = history->owner().histories();
- histories.sendPreparedMessage(
- history,
- action.replyTo,
- randomId,
- Data::Histories::PrepareMessage<MTPmessages_SendInlineBotResult>(
- MTP_flags(sendFlags),
- peer->input,
- Data::Histories::ReplyToPlaceholder(),
- MTP_long(randomId),
- MTP_long(data->getQueryId()),
- MTP_string(data->getId()),
- MTP_int(action.options.scheduled),
- (sendAs ? sendAs->input : MTP_inputPeerEmpty()),
- Data::ShortcutIdToMTP(_session, action.options.shortcutId),
- MTP_long(starsPaid)
- ), [=](const MTPUpdates &result, const MTP::Response &response) {
- history->finishSavingCloudDraft(
- topicRootId,
- UnixtimeFromMsgId(response.outerMsgId));
- if (done) {
- done(true);
- }
- }, [=](const MTP::Error &error, const MTP::Response &response) {
- sendMessageFail(error, peer, randomId, newId);
- history->finishSavingCloudDraft(
- topicRootId,
- UnixtimeFromMsgId(response.outerMsgId));
- if (done) {
- done(false);
- }
- });
- finishForwarding(action);
- }
- void ApiWrap::uploadAlbumMedia(
- not_null<HistoryItem*> item,
- const MessageGroupId &groupId,
- const MTPInputMedia &media) {
- const auto localId = item->fullId();
- const auto failed = [=] {
- };
- request(MTPmessages_UploadMedia(
- MTP_flags(0),
- MTPstring(), // business_connection_id
- item->history()->peer->input,
- media
- )).done([=](const MTPMessageMedia &result) {
- const auto item = _session->data().message(localId);
- if (!item) {
- failed();
- return;
- }
- auto spoiler = false;
- if (const auto media = item->media()) {
- spoiler = media->hasSpoiler();
- if (const auto photo = media->photo()) {
- photo->setWaitingForAlbum();
- } else if (const auto document = media->document()) {
- document->setWaitingForAlbum();
- }
- }
- switch (result.type()) {
- case mtpc_messageMediaPhoto: {
- const auto &data = result.c_messageMediaPhoto();
- const auto photo = data.vphoto();
- if (!photo || photo->type() != mtpc_photo) {
- failed();
- return;
- }
- const auto &fields = photo->c_photo();
- using Flag = MTPDinputMediaPhoto::Flag;
- const auto flags = Flag()
- | (data.vttl_seconds() ? Flag::f_ttl_seconds : Flag())
- | (spoiler ? Flag::f_spoiler : Flag());
- const auto media = MTP_inputMediaPhoto(
- MTP_flags(flags),
- MTP_inputPhoto(
- fields.vid(),
- fields.vaccess_hash(),
- fields.vfile_reference()),
- MTP_int(data.vttl_seconds().value_or_empty()));
- sendAlbumWithUploaded(item, groupId, media);
- } break;
- case mtpc_messageMediaDocument: {
- const auto &data = result.c_messageMediaDocument();
- const auto document = data.vdocument();
- if (!document || document->type() != mtpc_document) {
- failed();
- return;
- }
- const auto &fields = document->c_document();
- const auto mtpCover = data.vvideo_cover();
- const auto cover = (mtpCover && mtpCover->type() == mtpc_photo)
- ? &(mtpCover->c_photo())
- : (const MTPDphoto*)nullptr;
- using Flag = MTPDinputMediaDocument::Flag;
- const auto flags = Flag()
- | (data.vttl_seconds() ? Flag::f_ttl_seconds : Flag())
- | (spoiler ? Flag::f_spoiler : Flag())
- | (data.vvideo_timestamp() ? Flag::f_video_timestamp : Flag())
- | (cover ? Flag::f_video_cover : Flag());
- const auto media = MTP_inputMediaDocument(
- MTP_flags(flags),
- MTP_inputDocument(
- fields.vid(),
- fields.vaccess_hash(),
- fields.vfile_reference()),
- (cover
- ? MTP_inputPhoto(
- cover->vid(),
- cover->vaccess_hash(),
- cover->vfile_reference())
- : MTPInputPhoto()),
- MTP_int(data.vvideo_timestamp().value_or_empty()),
- MTP_int(data.vttl_seconds().value_or_empty()),
- MTPstring()); // query
- sendAlbumWithUploaded(item, groupId, media);
- } break;
- }
- }).fail([=] {
- failed();
- }).send();
- }
- void ApiWrap::sendMedia(
- not_null<HistoryItem*> item,
- const MTPInputMedia &media,
- Api::SendOptions options,
- Fn<void(bool)> done) {
- const auto randomId = base::RandomValue<uint64>();
- _session->data().registerMessageRandomId(randomId, item->fullId());
- sendMediaWithRandomId(item, media, options, randomId, std::move(done));
- }
- void ApiWrap::sendMediaWithRandomId(
- not_null<HistoryItem*> item,
- const MTPInputMedia &media,
- Api::SendOptions options,
- uint64 randomId,
- Fn<void(bool)> done) {
- const auto history = item->history();
- const auto replyTo = item->replyTo();
- const auto peer = history->peer;
- auto caption = item->originalText();
- TextUtilities::Trim(caption);
- auto sentEntities = Api::EntitiesToMTP(
- _session,
- caption.entities,
- Api::ConvertOption::SkipLocal);
- const auto updateRecentStickers = Api::HasAttachedStickers(media);
- const auto starsPaid = std::min(
- peer->starsPerMessageChecked(),
- options.starsApproved);
- if (starsPaid) {
- options.starsApproved -= starsPaid;
- }
- using Flag = MTPmessages_SendMedia::Flag;
- const auto flags = Flag(0)
- | (replyTo ? Flag::f_reply_to : Flag(0))
- | (ShouldSendSilent(history->peer, options)
- ? Flag::f_silent
- : Flag(0))
- | (!sentEntities.v.isEmpty() ? Flag::f_entities : Flag(0))
- | (options.scheduled ? Flag::f_schedule_date : Flag(0))
- | (options.sendAs ? Flag::f_send_as : Flag(0))
- | (options.shortcutId ? Flag::f_quick_reply_shortcut : Flag(0))
- | (options.effectId ? Flag::f_effect : Flag(0))
- | (options.invertCaption ? Flag::f_invert_media : Flag(0))
- | (starsPaid ? Flag::f_allow_paid_stars : Flag(0));
- auto &histories = history->owner().histories();
- const auto itemId = item->fullId();
- histories.sendPreparedMessage(
- history,
- replyTo,
- randomId,
- Data::Histories::PrepareMessage<MTPmessages_SendMedia>(
- MTP_flags(flags),
- peer->input,
- Data::Histories::ReplyToPlaceholder(),
- (options.price
- ? MTPInputMedia(MTP_inputMediaPaidMedia(
- MTP_flags(0),
- MTP_long(options.price),
- MTP_vector<MTPInputMedia>(1, media),
- MTPstring()))
- : media),
- MTP_string(caption.text),
- MTP_long(randomId),
- MTPReplyMarkup(),
- sentEntities,
- MTP_int(options.scheduled),
- (options.sendAs ? options.sendAs->input : MTP_inputPeerEmpty()),
- Data::ShortcutIdToMTP(_session, options.shortcutId),
- MTP_long(options.effectId),
- MTP_long(starsPaid)
- ), [=](const MTPUpdates &result, const MTP::Response &response) {
- if (done) done(true);
- if (updateRecentStickers) {
- requestRecentStickers(std::nullopt, true);
- }
- }, [=](const MTP::Error &error, const MTP::Response &response) {
- if (done) done(false);
- sendMessageFail(error, peer, randomId, itemId);
- });
- }
- void ApiWrap::sendMultiPaidMedia(
- not_null<HistoryItem*> item,
- not_null<SendingAlbum*> album,
- Fn<void(bool)> done) {
- Expects(album->options.price > 0);
- const auto groupId = album->groupId;
- auto &options = album->options;
- const auto randomId = album->items.front().randomId;
- auto medias = album->items | ranges::view::transform([](
- const SendingAlbum::Item &part) {
- Assert(part.media.has_value());
- return MTPInputMedia(part.media->data().vmedia());
- }) | ranges::to<QVector<MTPInputMedia>>();
- const auto history = item->history();
- const auto replyTo = item->replyTo();
- const auto peer = history->peer;
- auto caption = item->originalText();
- TextUtilities::Trim(caption);
- auto sentEntities = Api::EntitiesToMTP(
- _session,
- caption.entities,
- Api::ConvertOption::SkipLocal);
- const auto starsPaid = std::min(
- peer->starsPerMessageChecked(),
- options.starsApproved);
- if (starsPaid) {
- options.starsApproved -= starsPaid;
- }
- using Flag = MTPmessages_SendMedia::Flag;
- const auto flags = Flag(0)
- | (replyTo ? Flag::f_reply_to : Flag(0))
- | (ShouldSendSilent(history->peer, options)
- ? Flag::f_silent
- : Flag(0))
- | (!sentEntities.v.isEmpty() ? Flag::f_entities : Flag(0))
- | (options.scheduled ? Flag::f_schedule_date : Flag(0))
- | (options.sendAs ? Flag::f_send_as : Flag(0))
- | (options.shortcutId ? Flag::f_quick_reply_shortcut : Flag(0))
- | (options.effectId ? Flag::f_effect : Flag(0))
- | (options.invertCaption ? Flag::f_invert_media : Flag(0))
- | (starsPaid ? Flag::f_allow_paid_stars : Flag(0));
- auto &histories = history->owner().histories();
- const auto itemId = item->fullId();
- album->sent = true;
- histories.sendPreparedMessage(
- history,
- replyTo,
- randomId,
- Data::Histories::PrepareMessage<MTPmessages_SendMedia>(
- MTP_flags(flags),
- peer->input,
- Data::Histories::ReplyToPlaceholder(),
- MTP_inputMediaPaidMedia(
- MTP_flags(0),
- MTP_long(options.price),
- MTP_vector<MTPInputMedia>(std::move(medias)),
- MTPstring()),
- MTP_string(caption.text),
- MTP_long(randomId),
- MTPReplyMarkup(),
- sentEntities,
- MTP_int(options.scheduled),
- (options.sendAs ? options.sendAs->input : MTP_inputPeerEmpty()),
- Data::ShortcutIdToMTP(_session, options.shortcutId),
- MTP_long(options.effectId),
- MTP_long(starsPaid)
- ), [=](const MTPUpdates &result, const MTP::Response &response) {
- if (const auto album = _sendingAlbums.take(groupId)) {
- const auto copy = (*album)->items;
- for (const auto &part : copy) {
- if (const auto item = history->owner().message(part.msgId)) {
- item->destroy();
- }
- }
- }
- if (done) done(true);
- }, [=](const MTP::Error &error, const MTP::Response &response) {
- if (done) done(false);
- sendMessageFail(error, peer, randomId, itemId);
- });
- }
- void ApiWrap::sendAlbumWithUploaded(
- not_null<HistoryItem*> item,
- const MessageGroupId &groupId,
- const MTPInputMedia &media) {
- const auto localId = item->fullId();
- const auto randomId = base::RandomValue<uint64>();
- _session->data().registerMessageRandomId(randomId, localId);
- const auto albumIt = _sendingAlbums.find(groupId.raw());
- Assert(albumIt != _sendingAlbums.end());
- const auto &album = albumIt->second;
- album->fillMedia(item, media, randomId);
- sendAlbumIfReady(album.get());
- }
- void ApiWrap::sendAlbumWithCancelled(
- not_null<HistoryItem*> item,
- const MessageGroupId &groupId) {
- const auto albumIt = _sendingAlbums.find(groupId.raw());
- if (albumIt == _sendingAlbums.end()) {
- // Sometimes we destroy item being sent already after the album
- // was sent successfully. For example the message could be loaded
- // from server (by messages.getHistory or updateNewMessage) and
- // added to history and after that updateMessageID was received with
- // the same message id, in this case we destroy a detached local
- // item and sendAlbumWithCancelled is called for already sent album.
- return;
- }
- const auto &album = albumIt->second;
- album->removeItem(item);
- sendAlbumIfReady(album.get());
- }
- void ApiWrap::sendAlbumIfReady(not_null<SendingAlbum*> album) {
- if (album->sent) {
- return;
- }
- const auto groupId = album->groupId;
- if (album->items.empty()) {
- _sendingAlbums.remove(groupId);
- return;
- }
- auto sample = (HistoryItem*)nullptr;
- auto medias = QVector<MTPInputSingleMedia>();
- medias.reserve(album->items.size());
- for (const auto &item : album->items) {
- if (!item.media) {
- return;
- } else if (!sample) {
- sample = _session->data().message(item.msgId);
- }
- medias.push_back(*item.media);
- }
- if (!sample) {
- _sendingAlbums.remove(groupId);
- return;
- } else if (album->options.price > 0) {
- sendMultiPaidMedia(sample, album);
- return;
- } else if (medias.size() < 2) {
- const auto &single = medias.front().data();
- album->sent = true;
- sendMediaWithRandomId(
- sample,
- single.vmedia(),
- album->options,
- single.vrandom_id().v);
- _sendingAlbums.remove(groupId);
- return;
- }
- const auto history = sample->history();
- const auto replyTo = sample->replyTo();
- const auto sendAs = album->options.sendAs;
- const auto starsPaid = std::min(
- history->peer->starsPerMessageChecked() * int(medias.size()),
- album->options.starsApproved);
- if (starsPaid) {
- album->options.starsApproved -= starsPaid;
- }
- using Flag = MTPmessages_SendMultiMedia::Flag;
- const auto flags = Flag(0)
- | (replyTo ? Flag::f_reply_to : Flag(0))
- | (ShouldSendSilent(history->peer, album->options)
- ? Flag::f_silent
- : Flag(0))
- | (album->options.scheduled ? Flag::f_schedule_date : Flag(0))
- | (sendAs ? Flag::f_send_as : Flag(0))
- | (album->options.shortcutId
- ? Flag::f_quick_reply_shortcut
- : Flag(0))
- | (album->options.effectId ? Flag::f_effect : Flag(0))
- | (album->options.invertCaption ? Flag::f_invert_media : Flag(0))
- | (starsPaid ? Flag::f_allow_paid_stars : Flag(0));
- auto &histories = history->owner().histories();
- const auto peer = history->peer;
- album->sent = true;
- histories.sendPreparedMessage(
- history,
- replyTo,
- uint64(0), // randomId
- Data::Histories::PrepareMessage<MTPmessages_SendMultiMedia>(
- MTP_flags(flags),
- peer->input,
- Data::Histories::ReplyToPlaceholder(),
- MTP_vector<MTPInputSingleMedia>(medias),
- MTP_int(album->options.scheduled),
- (sendAs ? sendAs->input : MTP_inputPeerEmpty()),
- Data::ShortcutIdToMTP(_session, album->options.shortcutId),
- MTP_long(album->options.effectId),
- MTP_long(starsPaid)
- ), [=](const MTPUpdates &result, const MTP::Response &response) {
- _sendingAlbums.remove(groupId);
- }, [=](const MTP::Error &error, const MTP::Response &response) {
- if (const auto album = _sendingAlbums.take(groupId)) {
- for (const auto &item : (*album)->items) {
- sendMessageFail(error, peer, item.randomId, item.msgId);
- }
- } else {
- sendMessageFail(error, peer);
- }
- });
- }
- void ApiWrap::reloadContactSignupSilent() {
- if (_contactSignupSilentRequestId) {
- return;
- }
- const auto requestId = request(MTPaccount_GetContactSignUpNotification(
- )).done([=](const MTPBool &result) {
- _contactSignupSilentRequestId = 0;
- const auto silent = mtpIsTrue(result);
- _contactSignupSilent = silent;
- _contactSignupSilentChanges.fire_copy(silent);
- }).fail([=] {
- _contactSignupSilentRequestId = 0;
- }).send();
- _contactSignupSilentRequestId = requestId;
- }
- rpl::producer<bool> ApiWrap::contactSignupSilent() const {
- return _contactSignupSilent
- ? _contactSignupSilentChanges.events_starting_with_copy(
- *_contactSignupSilent)
- : (_contactSignupSilentChanges.events() | rpl::type_erased());
- }
- std::optional<bool> ApiWrap::contactSignupSilentCurrent() const {
- return _contactSignupSilent;
- }
- void ApiWrap::saveContactSignupSilent(bool silent) {
- request(base::take(_contactSignupSilentRequestId)).cancel();
- const auto requestId = request(MTPaccount_SetContactSignUpNotification(
- MTP_bool(silent)
- )).done([=] {
- _contactSignupSilentRequestId = 0;
- _contactSignupSilent = silent;
- _contactSignupSilentChanges.fire_copy(silent);
- }).fail([=] {
- _contactSignupSilentRequestId = 0;
- }).send();
- _contactSignupSilentRequestId = requestId;
- }
- auto ApiWrap::botCommonGroups(not_null<UserData*> bot) const
- -> std::optional<std::vector<not_null<PeerData*>>> {
- const auto i = _botCommonGroups.find(bot);
- return (i != end(_botCommonGroups))
- ? i->second
- : std::optional<std::vector<not_null<PeerData*>>>();
- }
- void ApiWrap::requestBotCommonGroups(
- not_null<UserData*> bot,
- Fn<void()> done) {
- if (_botCommonGroupsRequests.contains(bot)) {
- return;
- }
- _botCommonGroupsRequests.emplace(bot, done);
- const auto finish = [=](std::vector<not_null<PeerData*>> list) {
- _botCommonGroups.emplace(bot, std::move(list));
- if (const auto callback = _botCommonGroupsRequests.take(bot)) {
- (*callback)();
- }
- };
- const auto limit = 100;
- request(MTPmessages_GetCommonChats(
- bot->inputUser,
- MTP_long(0), // max_id
- MTP_int(limit)
- )).done([=](const MTPmessages_Chats &result) {
- const auto chats = result.match([](const auto &data) {
- return &data.vchats().v;
- });
- auto &owner = session().data();
- auto list = std::vector<not_null<PeerData*>>();
- list.reserve(chats->size());
- for (const auto &chat : *chats) {
- if (const auto peer = owner.processChat(chat)) {
- list.push_back(peer);
- }
- }
- finish(std::move(list));
- }).fail([=] {
- finish({});
- }).send();
- }
- void ApiWrap::saveSelfBio(const QString &text) {
- if (_bio.requestId) {
- if (text != _bio.requestedText) {
- request(_bio.requestId).cancel();
- } else {
- return;
- }
- }
- _bio.requestedText = text;
- _bio.requestId = request(MTPaccount_UpdateProfile(
- MTP_flags(MTPaccount_UpdateProfile::Flag::f_about),
- MTPstring(),
- MTPstring(),
- MTP_string(text)
- )).done([=](const MTPUser &result) {
- _bio.requestId = 0;
- _session->data().processUser(result);
- _session->user()->setAbout(_bio.requestedText);
- }).fail([=] {
- _bio.requestId = 0;
- }).send();
- }
- void ApiWrap::registerStatsRequest(MTP::DcId dcId, mtpRequestId id) {
- _statsRequests[dcId].emplace(id);
- }
- void ApiWrap::unregisterStatsRequest(MTP::DcId dcId, mtpRequestId id) {
- const auto i = _statsRequests.find(dcId);
- Assert(i != end(_statsRequests));
- const auto removed = i->second.remove(id);
- Assert(removed);
- if (i->second.empty()) {
- _statsSessionKillTimer.callOnce(kStatsSessionKillTimeout);
- }
- }
- void ApiWrap::checkStatsSessions() {
- for (auto i = begin(_statsRequests); i != end(_statsRequests);) {
- if (i->second.empty()) {
- instance().killSession(
- MTP::ShiftDcId(i->first, MTP::kStatsDcShift));
- i = _statsRequests.erase(i);
- } else {
- ++i;
- }
- }
- }
- Api::Authorizations &ApiWrap::authorizations() {
- return *_authorizations;
- }
- Api::AttachedStickers &ApiWrap::attachedStickers() {
- return *_attachedStickers;
- }
- Api::BlockedPeers &ApiWrap::blockedPeers() {
- return *_blockedPeers;
- }
- Api::CloudPassword &ApiWrap::cloudPassword() {
- return *_cloudPassword;
- }
- Api::SelfDestruct &ApiWrap::selfDestruct() {
- return *_selfDestruct;
- }
- Api::SensitiveContent &ApiWrap::sensitiveContent() {
- return *_sensitiveContent;
- }
- Api::GlobalPrivacy &ApiWrap::globalPrivacy() {
- return *_globalPrivacy;
- }
- Api::UserPrivacy &ApiWrap::userPrivacy() {
- return *_userPrivacy;
- }
- Api::InviteLinks &ApiWrap::inviteLinks() {
- return *_inviteLinks;
- }
- Api::ChatLinks &ApiWrap::chatLinks() {
- return *_chatLinks;
- }
- Api::ViewsManager &ApiWrap::views() {
- return *_views;
- }
- Api::ConfirmPhone &ApiWrap::confirmPhone() {
- return *_confirmPhone;
- }
- Api::PeerPhoto &ApiWrap::peerPhoto() {
- return *_peerPhoto;
- }
- Api::Polls &ApiWrap::polls() {
- return *_polls;
- }
- Api::ChatParticipants &ApiWrap::chatParticipants() {
- return *_chatParticipants;
- }
- Api::UnreadThings &ApiWrap::unreadThings() {
- return *_unreadThings;
- }
- Api::Ringtones &ApiWrap::ringtones() {
- return *_ringtones;
- }
- Api::Transcribes &ApiWrap::transcribes() {
- return *_transcribes;
- }
- Api::Premium &ApiWrap::premium() {
- return *_premium;
- }
- Api::Usernames &ApiWrap::usernames() {
- return *_usernames;
- }
- Api::Websites &ApiWrap::websites() {
- return *_websites;
- }
- Api::PeerColors &ApiWrap::peerColors() {
- return *_peerColors;
- }
|