From df2f809565ca76f815223f4aa65ecafa1c84cbb4 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Mon, 15 Aug 2022 23:34:14 +0200 Subject: [PATCH] Archive and mute --- .../sidebarLeft/tabs/privacyAndSecurity.ts | 158 ++++++++++++------ src/lib/appManagers/appPrivacyManager.ts | 10 +- 2 files changed, 114 insertions(+), 54 deletions(-) diff --git a/src/components/sidebarLeft/tabs/privacyAndSecurity.ts b/src/components/sidebarLeft/tabs/privacyAndSecurity.ts index aeb6fb1e..65c8b003 100644 --- a/src/components/sidebarLeft/tabs/privacyAndSecurity.ts +++ b/src/components/sidebarLeft/tabs/privacyAndSecurity.ts @@ -250,79 +250,90 @@ export default class AppPrivacyAndSecurityTab extends SliderSuperTabEventable { } const promises: Promise[] = []; + { - const section = new SettingSection({name: 'Privacy.SensitiveContent'}); - section.container.classList.add('hide'); + const section = new SettingSection({name: 'NewChatsFromNonContacts', caption: 'ArchiveAndMuteInfo'}); - promises.push(this.managers.apiManager.invokeApi('account.getContentSettings').then((settings) => { - if(!settings.pFlags.sensitive_can_change) { + const checkboxField = new CheckboxField({text: 'ArchiveAndMute'}); + const row = new Row({ + checkboxField, + noCheckboxSubtitle: true + }); + + section.content.append(row.container); + + let enabled: boolean, destroyed: boolean; + this.eventListener.addEventListener('destroy', () => { + destroyed = true; + if(enabled === undefined) return; + this.managers.appPrivacyManager.setGlobalPrivacySettings({ + _: 'globalPrivacySettings', + archive_and_mute_new_noncontact_peers: checkboxField.checked + }); + }, {once: true}); + + const promise = Promise.all([ + this.managers.apiManager.getAppConfig(), + this.managers.appPrivacyManager.getGlobalPrivacySettings() + ]).then(([appConfig, settings]) => { + if(destroyed) { return; } - const enabled = settings.pFlags.sensitive_enabled; + const onPremiumToggle = (isPremium: boolean) => { + section.container.classList.toggle('hide', !isPremium && !appConfig.autoarchive_setting_available); + }; - const sensitiveRow = new Row({ - checkboxField: new CheckboxField({text: 'PrivacyAndSecurity.SensitiveText', checked: enabled}), - subtitleLangKey: 'PrivacyAndSecurity.SensitiveDesc', - noCheckboxSubtitle: true - }); + this.listenerSetter.add(rootScope)('premium_toggle', onPremiumToggle); + onPremiumToggle(rootScope.premium); - section.content.append(sensitiveRow.container); - section.container.classList.remove('hide'); + enabled = settings.archive_and_mute_new_noncontact_peers; - this.eventListener.addEventListener('destroy', () => { - const _enabled = sensitiveRow.checkboxField.checked; - const isChanged = _enabled !== enabled; - if(!isChanged) { - return; - } + checkboxField.setValueSilently(enabled); + }); - this.managers.apiManager.invokeApi('account.setContentSettings', { - sensitive_enabled: _enabled - }); - }, {once: true}); - })); + promises.push(promise); this.scrollable.append(section.container); } { - const section = new SettingSection({name: 'FilterChats'}); + const section = new SettingSection({name: 'Privacy.SensitiveContent', caption: 'PrivacyAndSecurity.SensitiveDesc'}); + section.container.classList.add('hide'); - const onDeleteClick = () => { - const popup = new PopupPeer('popup-delete-drafts', { - buttons: [{ - langKey: 'Delete', - callback: () => { - const toggle = toggleDisability([deleteButton], true); - this.managers.appDraftsManager.clearAllDrafts().then(() => { - toggle(); - }); - }, - isDanger: true - }], - titleLangKey: 'AreYouSureClearDraftsTitle', - descriptionLangKey: 'AreYouSureClearDrafts' - }); + const checkboxField = new CheckboxField({text: 'PrivacyAndSecurity.SensitiveText'}); + const row = new Row({ + checkboxField, + noCheckboxSubtitle: true + }); - popup.show(); - }; + section.content.append(row.container); - const deleteButton = Button('btn-primary btn-transparent', {icon: 'delete', text: 'PrivacyDeleteCloudDrafts'}); - this.listenerSetter.add(deleteButton)('click', onDeleteClick); - section.content.append(deleteButton); + let enabled: boolean; + this.eventListener.addEventListener('destroy', () => { + if(enabled === undefined) return; + const _enabled = row.checkboxField.checked; + const isChanged = _enabled !== enabled; + if(!isChanged) { + return; + } - /* promises.push(apiManager.invokeApi('messages.getAllDrafts').then((drafts) => { - const draftsRow = new Row({ - titleLangKey: 'PrivacyDeleteCloudDrafts', - subtitleLangKey: 'Drafts', - subtitleLangArgs: [(drafts as Updates.updates).updates.length], - icon: 'delete', - clickable: onDeleteClick + this.managers.apiManager.invokeApi('account.setContentSettings', { + sensitive_enabled: _enabled }); + }, {once: true}); - section.content.append(draftsRow.container); - })); */ + const promise = this.managers.apiManager.invokeApi('account.getContentSettings').then((settings) => { + if(!settings.pFlags.sensitive_can_change) { + return; + } + + enabled = settings.pFlags.sensitive_enabled; + checkboxField.setValueSilently(enabled); + section.container.classList.remove('hide'); + }); + + promises.push(promise); this.scrollable.append(section.container); } @@ -369,6 +380,47 @@ export default class AppPrivacyAndSecurityTab extends SliderSuperTabEventable { this.scrollable.append(section.container); } + { + const section = new SettingSection({name: 'FilterChats'}); + + const onDeleteClick = () => { + const popup = new PopupPeer('popup-delete-drafts', { + buttons: [{ + langKey: 'Delete', + callback: () => { + const toggle = toggleDisability([deleteButton], true); + this.managers.appDraftsManager.clearAllDrafts().then(() => { + toggle(); + }); + }, + isDanger: true + }], + titleLangKey: 'AreYouSureClearDraftsTitle', + descriptionLangKey: 'AreYouSureClearDrafts' + }); + + popup.show(); + }; + + const deleteButton = Button('btn-primary btn-transparent', {icon: 'delete', text: 'PrivacyDeleteCloudDrafts'}); + this.listenerSetter.add(deleteButton)('click', onDeleteClick); + section.content.append(deleteButton); + + /* promises.push(apiManager.invokeApi('messages.getAllDrafts').then((drafts) => { + const draftsRow = new Row({ + titleLangKey: 'PrivacyDeleteCloudDrafts', + subtitleLangKey: 'Drafts', + subtitleLangArgs: [(drafts as Updates.updates).updates.length], + icon: 'delete', + clickable: onDeleteClick + }); + + section.content.append(draftsRow.container); + })); */ + + this.scrollable.append(section.container); + } + return Promise.all(promises); } diff --git a/src/lib/appManagers/appPrivacyManager.ts b/src/lib/appManagers/appPrivacyManager.ts index 970a17da..65e39f99 100644 --- a/src/lib/appManagers/appPrivacyManager.ts +++ b/src/lib/appManagers/appPrivacyManager.ts @@ -4,7 +4,7 @@ * https://github.com/morethanwords/tweb/blob/master/LICENSE */ -import {InputPrivacyKey, InputPrivacyRule, PrivacyRule, PrivacyKey} from '../../layer'; +import {InputPrivacyKey, InputPrivacyRule, PrivacyRule, PrivacyKey, GlobalPrivacySettings} from '../../layer'; import convertInputKeyToKey from '../../helpers/string/convertInputKeyToKey'; import {AppManager} from './manager'; @@ -72,4 +72,12 @@ export class AppPrivacyManager extends AppManager { return this.privacy[privacyKey] = privacyRules.rules; }); } + + public getGlobalPrivacySettings() { + return this.apiManager.invokeApi('account.getGlobalPrivacySettings') + } + + public setGlobalPrivacySettings(settings: GlobalPrivacySettings) { + return this.apiManager.invokeApi('account.setGlobalPrivacySettings', {settings}); + } }