Repository navigation
Expand file tree
/
Copy pathappBusinessManager-DiXsYF_a.js
More file actions
3 lines (3 loc) · 12.8 KB
/
Copy pathappBusinessManager-DiXsYF_a.js
File metadata and controls
3 lines (3 loc) · 12.8 KB
1
2
3
//# allFunctionsCalledOnLoad
import{d as e}from"./userAgent-DC86zy_h.js";import{r as t}from"./eventListenerBase-OLQI584v.js";import{u as n}from"./getChatMigratedTo-BkdTXEMB.js";import{A as r}from"./formatStarsAmount-CRaPhHSW.js";var i=class{getAccountNumber(){return this.accountNumber}createLogger(e,n,r){return t(`ACC-${this.accountNumber}-${e}`,n,r)}setManagersAndAccountNumber(e,t){Object.assign(this,{...e,accountNumber:t}),this.name=this.name??``,this.log=this.createLogger(this.name,this.logTypes,this.logIgnoreDebugReset)}},a=604800;function o(e){return e>0?e:a}var s=2147483647;function c(e,t){let r=e.filter(e=>!n(e.botId,t.bot_id));return t.pFlags.confirmed?r:[{botId:t.bot_id,date:t.date,device:t.device,location:t.location},...r]}function l(e,t,n=a){return e.filter(e=>!e.date||e.date+n>t)}function u(e){return e.trim().replace(/^(?:https?:\/\/)?(?:www\.)?(?:t|telegram)\.me\//i,``).replace(/^@/,``).split(/[/?#]/)[0].match(/^[a-z\d_]+$/i)?.[0]}function d(e,t){return{_:`inputBusinessBotRecipients`,pFlags:{...e?.pFlags},users:e?.users?.map(e=>t(e)),exclude_users:e?.exclude_users?.map(e=>t(e))}}function f(e,t){let r=(e=[])=>e.filter(e=>!n(e,t)),i=(e=[])=>[...r(e),t],a=e.pFlags.exclude_selected?i(e.users):r(e.users),o=e.pFlags.exclude_selected?r(e.exclude_users):i(e.exclude_users);return{...e,pFlags:{...e.pFlags},users:a.length?a:void 0,exclude_users:o.length?o:void 0}}var p=class extends i{constructor(...e){super(...e),this.connectedBotRequestId=0,this.connectedBotMutationQueue=Promise.resolve(),this.pendingConnectedBotPeerRevokes=new Map,this.pendingConnectedBotPeerRevokeId=0,this.botConnectionReviews=[],this.botConnectionReviewExpirationSchedule=0,this.botConnectionReviewsLoaded=!1,this.authorizationAutoconfirmPeriod=a,this.authorizationAutoconfirmPeriodVersion=0}after(){return this.rootScope.addEventListener(`app_config`,e=>{++this.authorizationAutoconfirmPeriodVersion,this.setAuthorizationAutoconfirmPeriod(e.authorization_autoconfirm_period,this.botConnectionReviewsLoaded)}),this.apiUpdatesManager.addMultipleEventsListeners({updateNewBotConnection:e=>{this.loadBotConnectionReviews().then(()=>{this.setBotConnectionReviews(c(this.botConnectionReviews,e)),this.connectedBotPromise=void 0,this.refreshConnectedBot()})}}),this.loadBotConnectionReviews()}saveBusinessIntro(e,t){if(t)return t.sticker=this.appDocsManager.saveDoc(t.sticker,{type:`userFull`,userId:e}),t}async updateBusinessWorkHours(e){let t=e?.weekly_open?.length?e:void 0;if(!await this.apiManager.invokeApi(`account.updateBusinessWorkHours`,{business_work_hours:t}))throw Error(`ACCOUNT_UPDATE_BUSINESS_WORK_HOURS_FAILED`);return this.appProfileManager.modifyCachedFullUser(this.rootScope.myId.toUserId(),e=>{t?e.business_work_hours=t:delete e.business_work_hours}),t}async updateBusinessLocation(e){let t=e?.address?.trim(),n=t?e.geoPoint:void 0;if(!await this.apiManager.invokeApi(`account.updateBusinessLocation`,{address:t||void 0,geo_point:n&&{_:`inputGeoPoint`,lat:n.lat,long:n.long}}))throw Error(`ACCOUNT_UPDATE_BUSINESS_LOCATION_FAILED`);let r=t?{_:`businessLocation`,address:t,geo_point:n}:void 0;return this.appProfileManager.modifyCachedFullUser(this.rootScope.myId.toUserId(),e=>{r?e.business_location=r:delete e.business_location}),r}resolveBusinessChatLink(e){return this.apiManager.invokeApiSingleProcess({method:`account.resolveBusinessChatLink`,params:{slug:e},processResult:e=>{this.appPeersManager.saveApiPeers(e);let t=r(e.peer),{message:n,entities:i}=e;return{peerId:t,message:n,entities:i}}})}getConnectedBot(e){if(this.connectedBotPromise&&!e)return this.connectedBotPromise;let t=this.connectedBotRequestId,n=this.connectedBotRequestPendingId,r=this.connectedBotRequestId=t+1;this.connectedBotRequestPendingId=r;let i=this.getConfirmedConnectedBotPeerRevokeIds(),a;try{a=this.requestConnectedBot(e)}catch(e){throw this.connectedBotRequestId===r&&(this.connectedBotRequestId=t,this.connectedBotRequestPendingId=n),e}let o=a.then(e=>r===this.connectedBotRequestId?(this.connectedBotRequestPendingId=void 0,this.clearConnectedBotPeerRevokes(i),this.commitConnectedBotResult(e)):this.connectedBotRequestOutcome,e=>{if(r!==this.connectedBotRequestId)return this.connectedBotRequestOutcome;throw this.connectedBotRequestPendingId===r&&(this.connectedBotRequestPendingId=void 0),this.connectedBotPromise===o&&(this.connectedBotPromise=this.connectedBotBase||this.connectedBot?Promise.resolve(this.connectedBot):void 0),e});return this.connectedBotRequestOutcome=o,this.connectedBotPromise=o,this.connectedBotPromise}getBotConnectionReviews(){return this.loadBotConnectionReviews().then(()=>this.botConnectionReviews)}async confirmBotConnection(e){let t=await this.getConnectedBot(!0);if(!t||!n(t.bot_id,e))return this.recomputeConnectedBot(!0),!1;let r=await this.apiManager.invokeApiSingle(`account.confirmBotConnection`,{bot_id:this.appUsersManager.getUserInput(e)});return r&&this.removeBotConnectionReview(e),r}async rejectBotConnection(e){let t=await this.getConnectedBot(!0);return!t||!n(t.bot_id,e)?(this.recomputeConnectedBot(!0),!1):(await this.updateConnectedBot({previousBotId:e}),!0)}async searchBusinessBots(e){let t=u(e);if(!t)return{userIds:[]};let n=await this.appUsersManager.resolveUsername(t);return n?._===`user`?this.isBusinessBot(n)?{userIds:[n.id]}:{userIds:[],unsupportedUserId:n.pFlags?.bot?n.id:void 0}:{userIds:[]}}updateConnectedBot(e){return this.enqueueConnectedBotMutation(()=>this.updateConnectedBotInternal(e))}async updateConnectedBotInternal(e){let{botId:t,previousBotId:r}=e,i=!t,a=t||r;if(!a)return;if(!await this.hasConnectedBotPrincipal(r))throw this.recomputeConnectedBot(!0),Error(`CONNECTED_BOT_CHANGED`);let o=d(i?void 0:e.recipients,e=>this.appUsersManager.getUserInput(e));await this.apiManager.invokeApiSingleProcess({method:`account.updateConnectedBot`,params:{deleted:i||void 0,rights:i?void 0:e.rights,bot:this.appUsersManager.getUserInput(a),recipients:o},processResult:e=>{this.apiUpdatesManager.processUpdateMessage(e)}}),this.clearConfirmedConnectedBotPeerRevokes();let s,c=!0;try{s=await this.getConnectedBot(!0)}catch{c=!1;let t=this.connectedBotBase||this.connectedBot,r=!i&&t&&n(t.bot_id,a);s=i?void 0:{...r?t:{},_:`connectedBot`,bot_id:a,recipients:e.recipients||(r?t.recipients:{_:`businessBotRecipients`,pFlags:{}}),rights:e.rights||(r?t.rights:{_:`businessBotRights`,pFlags:{}})},s=this.commitConnectedBotBase(s)}s=this.connectedBot,this.rootScope.dispatchEvent(`chat_automation_update`,s);let l=s&&n(s.bot_id,a);i&&(!c||!l)?this.removeBotConnectionReview(a):!i&&r&&!n(r,t)&&(!c||!n(s?.bot_id,r))&&this.removeBotConnectionReview(r),c||this.refreshConnectedBot()}toggleConnectedBotPaused(e,t,n){return this.enqueueConnectedBotMutation(()=>this.toggleConnectedBotPausedInternal(e,t,n))}async toggleConnectedBotPausedInternal(e,t,n){if(!await this.hasConnectedBotPrincipal(n))throw this.recomputeConnectedBot(!0),Error(`CONNECTED_BOT_CHANGED`);let r=await this.apiManager.invokeApiSingle(`account.toggleConnectedBotPaused`,{peer:this.appPeersManager.getInputPeerById(e),paused:t});if(!r)throw Error(`ACCOUNT_TOGGLE_CONNECTED_BOT_PAUSED_FAILED`);return this.appProfileManager.modifyCachedPeerSettings(e,e=>{t?(e.pFlags.business_bot_paused=!0,delete e.pFlags.business_bot_can_reply):(delete e.pFlags.business_bot_paused,e.pFlags.business_bot_can_reply=!0)})||Promise.resolve(this.appProfileManager.refreshPeerSettings(e)).catch(()=>{}),r}disablePeerConnectedBot(e,t){let n;return e.isUser()&&(!this.connectedBotBase&&this.connectedBot&&(this.connectedBotBase=this.connectedBot),n=++this.pendingConnectedBotPeerRevokeId,this.pendingConnectedBotPeerRevokes.set(n,{botId:t,userId:e.toUserId(),confirmed:!1}),this.recomputeConnectedBot(!0)),this.enqueueConnectedBotMutation(()=>this.disablePeerConnectedBotInternal(e,t,n))}async disablePeerConnectedBotInternal(e,t,n){let r;try{r=await this.hasConnectedBotPrincipal(t)}catch(e){throw n&&this.pendingConnectedBotPeerRevokes.delete(n),this.recomputeConnectedBot(!0),e}if(!r)return n&&this.pendingConnectedBotPeerRevokes.delete(n),this.recomputeConnectedBot(!0),!0;n&&this.recomputeConnectedBot(!0);let i=!1,a;try{if(i=await this.apiManager.invokeApiSingle(`account.disablePeerConnectedBot`,{peer:this.appPeersManager.getInputPeerById(e)}),!i)throw Error(`ACCOUNT_DISABLE_PEER_CONNECTED_BOT_FAILED`)}catch(e){a=e}if(a)n&&this.pendingConnectedBotPeerRevokes.delete(n),this.recomputeConnectedBot(!0);else{let t=n&&this.pendingConnectedBotPeerRevokes.get(n);t&&(t.confirmed=!0),this.appProfileManager.modifyCachedPeerSettings(e,e=>{delete e.pFlags.business_bot_paused,delete e.pFlags.business_bot_can_reply,delete e.business_bot_id,delete e.business_bot_manage_url})}if(await this.reconcileConnectedBot(),a)throw a;return i}requestConnectedBot(e){return this.apiManager.invokeApiSingleProcess({method:`account.getConnectedBots`,params:{},processResult:e=>e,options:{overwrite:e}})}commitConnectedBotResult(e){return this.appPeersManager.saveApiPeers(e),this.commitConnectedBotBase(e.connected_bots[0])}commitConnectedBotBase(e,t=!1){return this.connectedBotBase=e,this.recomputeConnectedBot(t)}recomputeConnectedBot(e=!1){return this.connectedBot=this.applyPendingConnectedBotPeerRevokes(this.connectedBotBase),this.connectedBotRequestPendingId===void 0&&(this.connectedBotPromise=Promise.resolve(this.connectedBot)),e&&this.rootScope.dispatchEvent(`chat_automation_update`,this.connectedBot),this.connectedBot}applyPendingConnectedBotPeerRevokes(e){if(!e)return e;let t=e.recipients;return this.pendingConnectedBotPeerRevokes.forEach(r=>{n(e.bot_id,r.botId)&&(t=f(t,r.userId))}),t===e.recipients?e:{...e,recipients:t}}clearConfirmedConnectedBotPeerRevokes(){this.pendingConnectedBotPeerRevokes.forEach((e,t)=>{e.confirmed&&this.pendingConnectedBotPeerRevokes.delete(t)})}getConfirmedConnectedBotPeerRevokeIds(){let e=[];return this.pendingConnectedBotPeerRevokes.forEach((t,n)=>{t.confirmed&&e.push(n)}),e}clearConnectedBotPeerRevokes(e){e.forEach(e=>this.pendingConnectedBotPeerRevokes.delete(e))}async hasConnectedBotPrincipal(e){return await this.getConnectedBot(!0),e?!!this.connectedBot&&n(this.connectedBot.bot_id,e):!this.connectedBot}async reconcileConnectedBot(){try{await this.getConnectedBot(!0),this.recomputeConnectedBot(!0)}catch{this.retryConnectedBotReconciliation()}}retryConnectedBotReconciliation(){this.getConnectedBot(!0).then(()=>{this.recomputeConnectedBot(!0)}).catch(()=>{})}enqueueConnectedBotMutation(e){let t=this.connectedBotMutationQueue.then(e,e);return this.connectedBotMutationQueue=t.then(()=>{},()=>{}),t}isBusinessBot(e){return e?._===`user`&&!!e.pFlags?.bot_business}loadBotConnectionReviews(){let e=this.authorizationAutoconfirmPeriodVersion;return this.botConnectionReviewsPromise??(this.botConnectionReviewsPromise=this.appStateManager.getState().then(t=>{e===this.authorizationAutoconfirmPeriodVersion&&this.setAuthorizationAutoconfirmPeriod(t.appConfig?.authorization_autoconfirm_period,!1);let n=t.botConnectionReviews||[],r=this.filterExpiredBotConnectionReviews(n);this.botConnectionReviewsLoaded=!0,this.setBotConnectionReviews(r,r.length!==n.length),r.length&&this.refreshConnectedBot()}))}setAuthorizationAutoconfirmPeriod(e,t=!0){let n=o(e);if(this.authorizationAutoconfirmPeriod!==n&&(this.authorizationAutoconfirmPeriod=n,t)){let e=this.filterExpiredBotConnectionReviews(this.botConnectionReviews);e.length===this.botConnectionReviews.length?this.scheduleBotConnectionReviewsExpiration():this.setBotConnectionReviews(e)}}filterExpiredBotConnectionReviews(e){return l(e,this.timeManager.getServerTime(),this.authorizationAutoconfirmPeriod)}setBotConnectionReviews(e,t=!0){this.botConnectionReviews=e,t&&this.appStateManager.pushToState(`botConnectionReviews`,e),this.rootScope.dispatchEvent(`bot_connection_reviews_update`,e),this.scheduleBotConnectionReviewsExpiration()}removeBotConnectionReview(e){let t=this.botConnectionReviews.filter(t=>!n(t.botId,e));t.length!==this.botConnectionReviews.length&&this.setBotConnectionReviews(t)}scheduleBotConnectionReviewsExpiration(){let t=++this.botConnectionReviewExpirationSchedule;this.botConnectionReviewExpirationTimeout!==void 0&&(e.clearTimeout(this.botConnectionReviewExpirationTimeout),this.botConnectionReviewExpirationTimeout=void 0);let n=this.botConnectionReviews.filter(e=>e.date);if(!n.length)return;let r=this.timeManager.getServerTime(),i=Math.min(...n.map(e=>e.date+this.authorizationAutoconfirmPeriod)),a=Math.min(Math.max(0,(i-r)*1e3),s);this.botConnectionReviewExpirationTimeout=e.setTimeout(()=>{if(t!==this.botConnectionReviewExpirationSchedule)return;this.botConnectionReviewExpirationTimeout=void 0;let e=this.filterExpiredBotConnectionReviews(this.botConnectionReviews);e.length===this.botConnectionReviews.length?this.scheduleBotConnectionReviewsExpiration():this.setBotConnectionReviews(e)},a)}refreshConnectedBot(){this.getConnectedBot(!0).then(e=>{this.rootScope.dispatchEvent(`chat_automation_update`,e)}).catch(()=>{})}};export{i as a,o as i,u as n,a as r,p as t};
//# sourceMappingURL=appBusinessManager-DiXsYF_a.js.map