From b44792afab17a9cd2b9b1b034d42e289471641c5 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 16 Jul 2021 17:44:21 +0300 Subject: [PATCH] Composer: Show translated ban due date (#1286) --- src/api/types/chats.ts | 1 + src/components/middle/MiddleColumn.tsx | 19 +++++++++---- .../management/ManageGroupPermissions.tsx | 2 +- .../management/ManageGroupUserPermissions.tsx | 4 +-- src/modules/helpers/chats.ts | 28 +++++++++++++++---- src/util/dateFormat.ts | 4 +-- src/util/langProvider.ts | 2 ++ 7 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/api/types/chats.ts b/src/api/types/chats.ts index b70f696d..5e2d3a2b 100644 --- a/src/api/types/chats.ts +++ b/src/api/types/chats.ts @@ -135,6 +135,7 @@ export interface ApiChatBannedRights { changeInfo?: true; inviteUsers?: true; pinMessages?: true; + untilDate?: number; } export interface ApiRestrictionReason { diff --git a/src/components/middle/MiddleColumn.tsx b/src/components/middle/MiddleColumn.tsx index a3d3ce15..7c87da25 100644 --- a/src/components/middle/MiddleColumn.tsx +++ b/src/components/middle/MiddleColumn.tsx @@ -3,7 +3,7 @@ import React, { } from '../../lib/teact/teact'; import { withGlobal } from '../../lib/teact/teactn'; -import { MAIN_THREAD_ID } from '../../api/types'; +import { ApiChatBannedRights, MAIN_THREAD_ID } from '../../api/types'; import { GlobalActions, MessageListType } from '../../global/types'; import { ThemeKey } from '../../types'; @@ -69,7 +69,8 @@ type StateProps = { isPinnedMessageList?: boolean; isScheduledMessageList?: boolean; canPost?: boolean; - messageSendingRestrictionReason?: string; + currentUserBannedRights?: ApiChatBannedRights; + defaultBannedRights?: ApiChatBannedRights; hasPinnedOrAudioMessage?: boolean; pinnedMessagesCount?: number; theme: ThemeKey; @@ -104,7 +105,8 @@ const MiddleColumn: FC = ({ isPinnedMessageList, isScheduledMessageList, canPost, - messageSendingRestrictionReason, + currentUserBannedRights, + defaultBannedRights, hasPinnedOrAudioMessage, pinnedMessagesCount, customBackground, @@ -127,6 +129,7 @@ const MiddleColumn: FC = ({ }) => { const { width: windowWidth } = useWindowSize(); + const lang = useLang(); const [dropAreaState, setDropAreaState] = useState(DropAreaState.None); const [isFabShown, setIsFabShown] = useState(); const [isNotchShown, setIsNotchShown] = useState(); @@ -235,6 +238,11 @@ const MiddleColumn: FC = ({ !isSelectModeActive && 'shown', ); + + const messageSendingRestrictionReason = getMessageSendingRestrictionReason( + lang, currentUserBannedRights, defaultBannedRights, + ); + // CSS Variables calculation doesn't work properly with transforms, so we calculate transform values in JS const { composerHiddenScale, toolbarHiddenScale, @@ -245,8 +253,6 @@ const MiddleColumn: FC = ({ [renderingCanPost, windowWidth], ); - const lang = useLang(); - const footerClassName = buildClassName( 'middle-column-footer', !renderingCanPost && 'no-composer', @@ -443,7 +449,8 @@ export default memo(withGlobal( canPost: !isPinnedMessageList && (!chat || canPost) && (!isBotNotStarted || IS_SINGLE_COLUMN_LAYOUT), isPinnedMessageList, isScheduledMessageList, - messageSendingRestrictionReason: chat && getMessageSendingRestrictionReason(chat), + currentUserBannedRights: chat && chat.currentUserBannedRights, + defaultBannedRights: chat && chat.defaultBannedRights, hasPinnedOrAudioMessage: ( threadId !== MAIN_THREAD_ID || Boolean(pinnedIds && pinnedIds.length) diff --git a/src/components/right/management/ManageGroupPermissions.tsx b/src/components/right/management/ManageGroupPermissions.tsx index c3563277..fb68b34a 100644 --- a/src/components/right/management/ManageGroupPermissions.tsx +++ b/src/components/right/management/ManageGroupPermissions.tsx @@ -104,7 +104,7 @@ const ManageGroupPermissions: FC = ({ setPermissions((p) => ({ ...p, - [name]: getUpdatedPermissionValue(p[name as keyof ApiChatBannedRights]), + [name]: getUpdatedPermissionValue(p[name as Exclude]), ...(name === 'sendStickers' && { sendGifs: getUpdatedPermissionValue(p[name]), }), diff --git a/src/components/right/management/ManageGroupUserPermissions.tsx b/src/components/right/management/ManageGroupUserPermissions.tsx index 72143476..66e23f65 100644 --- a/src/components/right/management/ManageGroupUserPermissions.tsx +++ b/src/components/right/management/ManageGroupUserPermissions.tsx @@ -82,7 +82,7 @@ const ManageGroupUserPermissions: FC = ({ setPermissions((p) => ({ ...p, - [name]: getUpdatedPermissionValue(p[name as keyof ApiChatBannedRights]), + [name]: getUpdatedPermissionValue(p[name as Exclude]), ...(name === 'sendStickers' && { sendGifs: getUpdatedPermissionValue(p[name]), }), @@ -117,7 +117,7 @@ const ManageGroupUserPermissions: FC = ({ }); }, [chat, selectedChatMemberId, updateChatMemberBannedRights]); - const getControlIsDisabled = useCallback((key: keyof ApiChatBannedRights) => { + const getControlIsDisabled = useCallback((key: Exclude) => { if (isFormFullyDisabled) { return true; } diff --git a/src/modules/helpers/chats.ts b/src/modules/helpers/chats.ts index 6b7a9e2f..3eac76f5 100644 --- a/src/modules/helpers/chats.ts +++ b/src/modules/helpers/chats.ts @@ -6,12 +6,16 @@ import { ApiChatFolder, MAIN_THREAD_ID, } from '../../api/types'; + import { NotifyException, NotifySettings } from '../../types'; +import { LangFn } from '../../hooks/useLang'; import { ARCHIVED_FOLDER_ID } from '../../config'; import { orderBy } from '../../util/iteratees'; import { getUserFirstOrLastName } from './users'; -import { LangFn } from '../../hooks/useLang'; +import { formatDateToString, formatTime } from '../../util/dateFormat'; + +const FOREVER_BANNED_DATE = Date.now() / 1000 + 31622400; // 366 days const VEIFIED_PRIORITY_BASE = 3e9; const PINNED_PRIORITY_BASE = 3e8; @@ -176,12 +180,24 @@ export function getAllowedAttachmentOptions(chat?: ApiChat, isChatWithBot = fals }; } -export function getMessageSendingRestrictionReason(chat: ApiChat) { - if (chat.currentUserBannedRights && chat.currentUserBannedRights.sendMessages) { - return 'You are not allowed to send messages in this chat.'; +export function getMessageSendingRestrictionReason( + lang: LangFn, currentUserBannedRights?: ApiChatBannedRights, defaultBannedRights?: ApiChatBannedRights, +) { + if (currentUserBannedRights && currentUserBannedRights.sendMessages) { + const { untilDate } = currentUserBannedRights; + return untilDate && untilDate < FOREVER_BANNED_DATE + ? lang( + 'Channel.Persmission.Denied.SendMessages.Until', + lang( + 'formatDateAtTime', + [formatDateToString(new Date(untilDate * 1000), lang.code), formatTime(untilDate * 1000)], + ), + ) + : lang('Channel.Persmission.Denied.SendMessages.Forever'); } - if (chat.defaultBannedRights && chat.defaultBannedRights.sendMessages) { - return 'Sending messages is not allowed in this chat.'; + + if (defaultBannedRights && defaultBannedRights.sendMessages) { + return lang('Channel.Persmission.Denied.SendMessages.DefaultRestrictedText'); } return undefined; diff --git a/src/util/dateFormat.ts b/src/util/dateFormat.ts index 7950247d..c84f9e43 100644 --- a/src/util/dateFormat.ts +++ b/src/util/dateFormat.ts @@ -176,9 +176,9 @@ export function formatVoiceRecordDuration(durationInMs: number) { return `${parts.join(':')},${String(milliseconds).padStart(2, '0')}`; } -export function formatDateToString(date: Date) { +export function formatDateToString(date: Date, locale = 'en-US') { return date.toLocaleString( - 'en-US', + locale, { year: 'numeric', month: 'short', diff --git a/src/util/langProvider.ts b/src/util/langProvider.ts index ebf548b1..a812327e 100644 --- a/src/util/langProvider.ts +++ b/src/util/langProvider.ts @@ -11,6 +11,7 @@ interface LangFn { (key: string, value?: any, format?: 'i'): any; isRtl?: boolean; + code?: string; } const FALLBACK_LANG_CODE = 'en'; @@ -124,6 +125,7 @@ export async function setLanguage(langCode: string, callback?: NoneToVoidFunctio const { languages } = getGlobal().settings.byKey; const langInfo = languages ? languages.find((l) => l.langCode === langCode) : undefined; getTranslation.isRtl = Boolean(langInfo && langInfo.rtl); + getTranslation.code = langCode; if (callback) { callback();