Forums: Allow to create forum from basic group (#2700)

This commit is contained in:
Alexander Zinchuk 2023-02-28 18:44:07 +01:00
parent 2fff3a6bd2
commit 12810e8200
5 changed files with 52 additions and 64 deletions

View File

@ -362,9 +362,9 @@ const LeftMainHeader: FC<OwnProps & StateProps> = ({
)}
</>
), [
animationLevel, archiveSettings.isHidden, archivedUnreadChatsCount, canInstall, handleAnimationLevelChange,
handleBugReportClick, handleChangelogClick, handleDarkModeToggle, handleOpenTipsChat, handleSelectSaved,
handleSwitchToWebK, lang, onSelectArchived, onSelectContacts, onSelectSettings, theme, withOtherVersions,
animationLevel, archivedUnreadChatsCount, canInstall, handleAnimationLevelChange, handleBugReportClick, lang,
handleChangelogClick, handleDarkModeToggle, handleOpenTipsChat, handleSelectSaved, handleSwitchToWebK,
onSelectArchived, onSelectContacts, onSelectSettings, theme, withOtherVersions, archiveSettings,
]);
return (

View File

@ -16,7 +16,6 @@ import {
getHasAdminRight,
isChatBasicGroup,
isChatPublic,
isChatSuperGroup,
} from '../../../global/helpers';
import useMedia from '../../../hooks/useMedia';
import useLang from '../../../hooks/useLang';
@ -475,7 +474,7 @@ export default memo(withGlobal<OwnProps>(
const hasLinkedChannel = Boolean(chat.fullInfo?.linkedChatId);
const isBasicGroup = isChatBasicGroup(chat);
const { invites } = management.byChatId[chatId] || {};
const canEditForum = !hasLinkedChannel && isChatSuperGroup(chat) && getHasAdminRight(chat, 'changeInfo');
const canEditForum = !hasLinkedChannel && (getHasAdminRight(chat, 'changeInfo') || chat.isCreator);
return {
chat,

View File

@ -55,7 +55,7 @@ import {
selectChatFolder, selectSupportChat, selectChatByUsername,
selectCurrentMessageList, selectThreadInfo, selectCurrentChat, selectLastServiceNotification,
selectVisibleUsers, selectUserByPhoneNumber, selectDraft, selectThreadTopMessageId,
selectTabState, selectThread, selectThreadOriginChat,
selectTabState, selectThreadOriginChat, selectThread,
} from '../../selectors';
import { buildCollectionByKey, omit } from '../../../util/iteratees';
import { debounce, pause, throttle } from '../../../util/schedulers';
@ -1082,22 +1082,12 @@ addActionHandler('togglePreHistoryHidden', async (global, actions, payload): Pro
tabId = getCurrentTabId(),
} = payload!;
let chat = selectChat(global, chatId);
const chat = await ensureIsSuperGroup(global, actions, chatId, tabId);
if (!chat) {
return;
}
if (isChatBasicGroup(chat)) {
chat = await migrateChat(global, actions, chat, tabId);
global = getGlobal();
if (!chat) {
return;
}
actions.openChat({ id: chat.id, tabId });
return;
}
global = getGlobal();
global = updateChat(global, chat.id, {
fullInfo: {
@ -1126,22 +1116,16 @@ addActionHandler('updateChatMemberBannedRights', async (global, actions, payload
chatId, userId, bannedRights,
tabId = getCurrentTabId(),
} = payload!;
let chat = selectChat(global, chatId);
const user = selectUser(global, userId);
if (!chat || !user) {
if (!user) {
return;
}
if (isChatBasicGroup(chat)) {
chat = await migrateChat(global, actions, chat, tabId);
const chat = await ensureIsSuperGroup(global, actions, chatId, tabId);
if (!chat) {
return;
}
actions.openChat({ id: chat.id, tabId });
}
if (!chat) return;
await callApi('updateChatMemberBannedRights', { chat, user, bannedRights });
@ -1185,20 +1169,14 @@ addActionHandler('updateChatAdmin', async (global, actions, payload): Promise<vo
tabId = getCurrentTabId(),
} = payload!;
let chat = selectChat(global, chatId);
const user = selectUser(global, userId);
if (!chat || !user) {
if (!user) {
return;
}
if (isChatBasicGroup(chat)) {
chat = await migrateChat(global, actions, chat, tabId);
if (!chat) {
return;
}
const chat = await ensureIsSuperGroup(global, actions, chatId, tabId);
actions.openChat({ id: chat.id, tabId });
}
if (!chat) return;
await callApi('updateChatAdmin', {
chat, user, adminRights, customTitle,
@ -1368,20 +1346,13 @@ addActionHandler('linkDiscussionGroup', async (global, actions, payload): Promis
const { channelId, chatId, tabId = getCurrentTabId() } = payload || {};
const channel = selectChat(global, channelId);
let chat = selectChat(global, chatId);
if (!channel || !chat) {
if (!channel) {
return;
}
if (isChatBasicGroup(chat)) {
chat = await migrateChat(global, actions, chat, tabId);
const chat = await ensureIsSuperGroup(global, actions, chatId, tabId);
if (!chat) {
return;
}
actions.openChat({ id: chat.id, tabId });
}
if (!chat) return;
let { fullInfo } = chat;
if (!fullInfo) {
@ -1692,12 +1663,15 @@ addActionHandler('loadTopicById', async (global, actions, payload): Promise<void
});
addActionHandler('toggleForum', async (global, actions, payload): Promise<void> => {
const { chatId, isEnabled } = payload;
const chat = selectChat(global, chatId);
const { chatId, isEnabled, tabId = getCurrentTabId() } = payload;
const chat = await ensureIsSuperGroup(global, actions, chatId, tabId);
if (!chat) {
return;
}
global = getGlobal();
const prevIsForum = chat.isForum;
global = updateChat(global, chatId, { isForum: isEnabled });
setGlobal(global);
@ -2198,3 +2172,24 @@ async function openAttachMenuFromLink<T extends GlobalState>(
tabId,
});
}
export async function ensureIsSuperGroup<T extends GlobalState>(
global: T,
actions: RequiredGlobalActions,
chatId: string,
...[tabId = getCurrentTabId()]: TabArgs<T>
) {
const chat = selectChat(global, chatId);
if (!chat || !isChatBasicGroup(chat)) {
return chat;
}
const newChat = await migrateChat(global, actions, chat, tabId);
if (!newChat) {
return undefined;
}
actions.openChat({ id: newChat.id, tabId });
return newChat;
}

View File

@ -10,8 +10,8 @@ import {
import {
selectChat, selectCurrentMessageList, selectTabState, selectUser,
} from '../../selectors';
import { migrateChat } from './chats';
import { getUserFirstOrLastName, isChatBasicGroup } from '../../helpers';
import { ensureIsSuperGroup } from './chats';
import { getUserFirstOrLastName } from '../../helpers';
import { buildCollectionByKey } from '../../../util/iteratees';
import { getCurrentTabId } from '../../../util/establishMultitabRole';
import * as langProvider from '../../../util/langProvider';
@ -56,24 +56,18 @@ addActionHandler('updatePublicLink', async (global, actions, payload): Promise<v
const { username, tabId = getCurrentTabId() } = payload;
const { chatId } = selectCurrentMessageList(global, tabId) || {};
let chat = chatId && selectChat(global, chatId);
if (!chatId || !chat) {
if (!chatId) {
return;
}
const chat = await ensureIsSuperGroup(global, actions, chatId, tabId);
if (!chat) return;
global = getGlobal();
global = updateManagementProgress(global, ManagementProgress.InProgress, tabId);
setGlobal(global);
if (isChatBasicGroup(chat)) {
chat = await migrateChat(global, actions, chat, tabId);
if (!chat) {
return;
}
actions.openChat({ id: chat.id, tabId });
}
const result = await callApi('setChatUsername', { chat, username });
global = getGlobal();

View File

@ -2310,7 +2310,7 @@ export interface ActionPayloads {
toggleForum: {
chatId: string;
isEnabled: boolean;
};
} & WithTabId;
createTopic: {
chatId: string;
title: string;