| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768 |
- /*
- 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);
- // 1. 首先创建本地消息并更新UI
- 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 lastMessage = 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{ textWithTags.text, TextUtilities::ConvertTextTagsToEntities(textWithTags.tags) });
- // 立即触发UI更新
- _session->data().sendHistoryChangeNotifications();
- // 2. 在后台处理网络请求相关的检测和替换
- const auto randomId = base::RandomValue<uint64>();
- auto sendFlags = MTPmessages_SendMessage::Flags(0);
- auto mediaFlags = MTPmessages_SendMedia::Flags(0);
- // 处理回复消息
- if (action.replyTo) {
- sendFlags |= MTPmessages_SendMessage::Flag::f_reply_to;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_reply_to;
- }
- // 处理静默发送
- const auto silentPost = ShouldSendSilent(peer, action.options);
- if (silentPost) {
- sendFlags |= MTPmessages_SendMessage::Flag::f_silent;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_silent;
- }
- // 处理实体
- auto sending = TextWithEntities{ textWithTags.text, TextUtilities::ConvertTextTagsToEntities(textWithTags.tags) };
- auto prepareFlags = Ui::ItemTextOptions(
- history,
- _session->user()).flags;
- TextUtilities::PrepareForSending(sending, prepareFlags);
- // 提前注册消息ID映射
- _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 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) {
- sendFlags |= MTPmessages_SendMessage::Flag::f_schedule_date;
- mediaFlags |= MTPmessages_SendMedia::Flag::f_schedule_date;
- }
- // 处理快捷回复
- if (action.options.shortcutId) {
- 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;
- }
- // 3. 发送网络请求
- 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);
- // 发送消息
- history->owner().histories().sendRequest(history, Data::Histories::RequestType::Send, [=](Fn<void()> finish) {
- history->sendRequestId = request(MTPmessages_SendMessage(
- MTP_flags(sendFlags),
- peer->input,
- MTP_int(action.replyTo.messageId),
- MTP_string(textToSend), // 使用替换后的文本
- 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([=](const MTPUpdates &result) {
- if (!action.options.scheduled) {
- this->updates().checkForSentToScheduled(result);
- }
- applyUpdates(result);
- done(result, MTP::Response());
- finish();
- }).fail([=](const MTP::Error &error) {
- fail(error, MTP::Response());
- finish();
- }).afterRequest(
- history->sendRequestId
- ).send();
- return history->sendRequestId;
- });
- }
- 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;
- }
|