Settings / General: Show sensitive content toggle (#1195)

This commit is contained in:
Alexander Zinchuk 2021-06-21 16:40:01 +03:00
parent 731c0330ad
commit d854db7ca2
8 changed files with 101 additions and 21 deletions

View File

@ -47,7 +47,7 @@ export {
fetchAuthorizations, terminateAuthorization, terminateAllAuthorizations,
fetchNotificationExceptions, fetchNotificationSettings, updateContactSignUpNotification, updateNotificationSettings,
fetchLanguages, fetchLangPack, fetchPrivacySettings, setPrivacySettings, registerDevice, unregisterDevice,
updateIsOnline,
updateIsOnline, fetchContentSettings, updateContentSettings,
} from './settings';
export {

View File

@ -375,6 +375,24 @@ export async function updateIsOnline(isOnline: boolean) {
await invokeRequest(new GramJs.account.UpdateStatus({ offline: !isOnline }));
}
export async function fetchContentSettings() {
const result = await invokeRequest(new GramJs.account.GetContentSettings());
if (!result) {
return undefined;
}
return {
isSensitiveEnabled: Boolean(result.sensitiveEnabled),
canChangeSensitive: Boolean(result.sensitiveCanChange),
};
}
export function updateContentSettings(isEnabled: boolean) {
return invokeRequest(new GramJs.account.SetContentSettings({
sensitiveEnabled: isEnabled || undefined,
}));
}
function updateLocalDb(
result: (
GramJs.account.PrivacyRules | GramJs.contacts.Blocked | GramJs.contacts.BlockedSlice |

View File

@ -24,12 +24,28 @@ type OwnProps = {
onScreenSelect: (screen: SettingsScreens) => void;
};
type StateProps = ISettings['byKey'] & {
type StateProps = Pick<ISettings, (
'messageTextSize' |
'animationLevel' |
'messageSendKeyCombo' |
'shouldAutoDownloadMediaFromContacts' |
'shouldAutoDownloadMediaInPrivateChats' |
'shouldAutoDownloadMediaInGroups' |
'shouldAutoDownloadMediaInChannels' |
'shouldAutoPlayGifs' |
'shouldAutoPlayVideos' |
'shouldSuggestStickers' |
'shouldLoopStickers' |
'isSensitiveEnabled' |
'canChangeSensitive'
)> & {
stickerSetIds?: string[];
stickerSetsById?: Record<string, ApiStickerSet>;
};
type DispatchProps = Pick<GlobalActions, 'setSettingOption' | 'loadStickerSets' | 'loadAddedStickers'>;
type DispatchProps = Pick<GlobalActions, (
'setSettingOption' | 'loadStickerSets' | 'loadAddedStickers' | 'loadContentSettings' | 'updateContentSettings'
)>;
const ANIMATION_LEVEL_OPTIONS = [
'Solid and Steady',
@ -52,9 +68,13 @@ const SettingsGeneral: FC<OwnProps & StateProps & DispatchProps> = ({
shouldAutoPlayVideos,
shouldSuggestStickers,
shouldLoopStickers,
isSensitiveEnabled,
canChangeSensitive,
setSettingOption,
loadStickerSets,
loadAddedStickers,
loadContentSettings,
updateContentSettings,
}) => {
// eslint-disable-next-line no-null/no-null
const stickerSettingsRef = useRef<HTMLDivElement>(null);
@ -75,7 +95,8 @@ const SettingsGeneral: FC<OwnProps & StateProps & DispatchProps> = ({
useEffect(() => {
loadStickerSets();
}, [loadStickerSets]);
loadContentSettings();
}, [loadContentSettings, loadStickerSets]);
useEffect(() => {
if (stickerSetIds && stickerSetIds.length) {
@ -104,7 +125,7 @@ const SettingsGeneral: FC<OwnProps & StateProps & DispatchProps> = ({
const stickerSets = stickerSetIds && stickerSetIds.map((id: string) => {
return stickerSetsById && stickerSetsById[id] && stickerSetsById[id].installedDate ? stickerSetsById[id] : false;
}).filter(Boolean);
}).filter<ApiStickerSet>(Boolean as any);
return (
<div className="settings-content custom-scroll">
@ -155,6 +176,19 @@ const SettingsGeneral: FC<OwnProps & StateProps & DispatchProps> = ({
</div>
)}
<div className="settings-item">
<h4 className="settings-item-header" dir={lang.isRtl ? 'rtl' : undefined}>
{lang('lng_settings_sensitive_title')}
</h4>
<Checkbox
label={lang('lng_settings_sensitive_disable_filtering')}
subLabel={lang('lng_settings_sensitive_about')}
checked={Boolean(isSensitiveEnabled)}
disabled={!canChangeSensitive}
onCheck={updateContentSettings}
/>
</div>
<div className="settings-item">
<h4 className="settings-item-header" dir={lang.isRtl ? 'rtl' : undefined}>{lang('AutoDownloadMedia')}</h4>
@ -234,25 +268,26 @@ const SettingsGeneral: FC<OwnProps & StateProps & DispatchProps> = ({
export default memo(withGlobal<OwnProps>(
(global): StateProps => {
return {
...pick(global.settings.byKey,
[
'messageTextSize',
'animationLevel',
'messageSendKeyCombo',
'shouldAutoDownloadMediaFromContacts',
'shouldAutoDownloadMediaInPrivateChats',
'shouldAutoDownloadMediaInGroups',
'shouldAutoDownloadMediaInChannels',
'shouldAutoPlayGifs',
'shouldAutoPlayVideos',
'shouldSuggestStickers',
'shouldLoopStickers',
]),
...pick(global.settings.byKey, [
'messageTextSize',
'animationLevel',
'messageSendKeyCombo',
'shouldAutoDownloadMediaFromContacts',
'shouldAutoDownloadMediaInPrivateChats',
'shouldAutoDownloadMediaInGroups',
'shouldAutoDownloadMediaInChannels',
'shouldAutoPlayGifs',
'shouldAutoPlayVideos',
'shouldSuggestStickers',
'shouldLoopStickers',
'isSensitiveEnabled',
'canChangeSensitive',
]),
stickerSetIds: global.stickers.added.setIds,
stickerSetsById: global.stickers.setsById,
};
},
(setGlobal, actions): DispatchProps => pick(actions, [
'setSettingOption', 'loadStickerSets', 'loadAddedStickers',
'setSettingOption', 'loadStickerSets', 'loadAddedStickers', 'loadContentSettings', 'updateContentSettings',
]),
)(SettingsGeneral));

View File

@ -450,7 +450,8 @@ export type ActionTypes = (
'loadAuthorizations' | 'terminateAuthorization' | 'terminateAllAuthorizations' |
'loadNotificationSettings' | 'updateContactSignUpNotification' | 'updateNotificationSettings' |
'loadLanguages' | 'loadPrivacySettings' | 'setPrivacyVisibility' | 'setPrivacySettings' |
'loadNotificationExceptions' | 'setThemeSettings' | 'updateIsOnline' |
'loadNotificationExceptions' | 'setThemeSettings' | 'updateIsOnline' | 'loadContentSettings' |
'updateContentSettings' |
// Stickers & GIFs
'loadStickerSets' | 'loadAddedStickers' | 'loadRecentStickers' | 'loadFavoriteStickers' | 'loadFeaturedStickers' |
'loadStickers' | 'setStickerSearchQuery' | 'loadSavedGifs' | 'setGifSearchQuery' | 'searchMoreGifs' |

View File

@ -942,6 +942,8 @@ account.getContactSignUpNotification#9f07c728 = Bool;
account.setContactSignUpNotification#cff43f61 silent:Bool = Bool;
account.getNotifyExceptions#53577479 flags:# compare_sound:flags.1?true peer:flags.0?InputNotifyPeer = Updates;
account.uploadWallPaper#dd853661 file:InputFile mime_type:string settings:WallPaperSettings = WallPaper;
account.setContentSettings#b574b16b flags:# sensitive_enabled:flags.0?true = Bool;
account.getContentSettings#8b9b4dae = account.ContentSettings;
users.getUsers#d91a548 id:Vector<InputUser> = Vector<User>;
users.getFullUser#ca30a5b1 id:InputUser = UserFull;
contacts.getContacts#c023849f hash:int = contacts.Contacts;

View File

@ -942,6 +942,8 @@ account.getContactSignUpNotification#9f07c728 = Bool;
account.setContactSignUpNotification#cff43f61 silent:Bool = Bool;
account.getNotifyExceptions#53577479 flags:# compare_sound:flags.1?true peer:flags.0?InputNotifyPeer = Updates;
account.uploadWallPaper#dd853661 file:InputFile mime_type:string settings:WallPaperSettings = WallPaper;
account.setContentSettings#b574b16b flags:# sensitive_enabled:flags.0?true = Bool;
account.getContentSettings#8b9b4dae = account.ContentSettings;
users.getUsers#d91a548 id:Vector<InputUser> = Vector<User>;
users.getFullUser#ca30a5b1 id:InputUser = UserFull;
contacts.getContacts#c023849f hash:int = contacts.Contacts;

View File

@ -520,3 +520,23 @@ function buildInputPrivacyRules(global: GlobalState, {
addReducer('updateIsOnline', (global, actions, payload) => {
callApi('updateIsOnline', payload);
});
addReducer('loadContentSettings', () => {
(async () => {
const result = await callApi('fetchContentSettings');
if (!result) return;
setGlobal(replaceSettings(getGlobal(), result));
})();
});
addReducer('updateContentSettings', (global, actions, payload) => {
(async () => {
setGlobal(replaceSettings(getGlobal(), { isSensitiveEnabled: payload }));
const result = await callApi('updateContentSettings', payload);
if (!result) {
setGlobal(replaceSettings(getGlobal(), { isSensitiveEnabled: !payload }));
}
})();
});

View File

@ -60,6 +60,8 @@ export interface ISettings extends NotifySettings, Record<string, any> {
hasPassword?: boolean;
languages?: ApiLanguage[];
language: LangCode;
isSensitiveEnabled?: boolean;
canChangeSensitive?: boolean;
}
export interface ApiPrivacySettings {