Handled category updates

This commit is contained in:
harshil Sharma 2023-11-03 15:46:52 +05:30
parent e171c82703
commit ea34873d5f
5 changed files with 71 additions and 23 deletions

View file

@ -3,12 +3,13 @@
import {CHANNELS_CATEGORY, DMS_CATEGORY} from '@constants/categories';
import DatabaseManager from '@database/manager';
import {prepareCategoryChannels, queryCategoriesByTeamIds, getCategoryById, prepareCategoriesAndCategoriesChannels} from '@queries/servers/categories';
import {prepareCategoryChannels, queryCategoriesByTeamIds, getCategoryById, prepareCategoriesAndCategoriesChannels, queryAllCategories, queryCategoryChannelsByChannelId} from '@queries/servers/categories';
import {getCurrentUserId} from '@queries/servers/system';
import {queryMyTeams} from '@queries/servers/team';
import {isDMorGM} from '@utils/channel';
import {logError} from '@utils/log';
import {logDebug, logError} from '@utils/log';
import type {Database} from '@nozbe/watermelondb';
import type ChannelModel from '@typings/database/models/servers/channel';
export const deleteCategory = async (serverUrl: string, categoryId: string) => {
@ -92,11 +93,8 @@ export async function addChannelToDefaultCategory(serverUrl: string, channel: Ch
categoriesWithChannels.push(cwc);
}
} else {
const categories = await queryCategoriesByTeamIds(database, [teamId]).fetch();
const channelCategory = categories.find((c) => c.type === CHANNELS_CATEGORY);
if (channelCategory) {
const cwc = await channelCategory.toCategoryWithChannels();
cwc.channel_ids.unshift(channel.id);
const cwc = await prepareAddNonGMDMChannelToDefaultCategory(database, teamId, channel.id);
if (cwc) {
categoriesWithChannels.push(cwc);
}
}
@ -112,3 +110,39 @@ export async function addChannelToDefaultCategory(serverUrl: string, channel: Ch
return {error};
}
}
async function prepareAddNonGMDMChannelToDefaultCategory(database: Database, teamId: string, channelId: string): Promise<CategoryWithChannels | undefined> {
const categories = await queryCategoriesByTeamIds(database, [teamId]).fetch();
const channelCategory = categories.find((category) => category.type === CHANNELS_CATEGORY);
if (channelCategory) {
const cwc = await channelCategory.toCategoryWithChannels();
cwc.channel_ids.unshift(channelId);
return cwc;
}
return undefined;
}
export async function putGMInCorrectCategory(serverUrl: string, channelId: string, targetTeamID: string, prepareRecordsOnly = false) {
try {
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const categoryChannels = await queryCategoryChannelsByChannelId(database, channelId).fetch();
categoryChannels.forEach((categoryChannel) => categoryChannel.prepareDestroyPermanently());
const models: any[] = categoryChannels;
const cwc = await prepareAddNonGMDMChannelToDefaultCategory(database, targetTeamID, channelId);
if (cwc) {
const model = await prepareCategoryChannels(operator, [cwc]);
models.push(...model);
}
if (models.length > 0 && !prepareRecordsOnly) {
await operator.batchRecords(models, 'putGMInCorrectCategory');
}
return {models};
} catch (error) {
return {error};
}
}

View file

@ -4,7 +4,7 @@
/* eslint-disable max-lines */
import {DeviceEventEmitter} from 'react-native';
import {addChannelToDefaultCategory, storeCategories} from '@actions/local/category';
import {addChannelToDefaultCategory, putGMInCorrectCategory, storeCategories} from '@actions/local/category';
import {markChannelAsViewed, removeCurrentUserFromChannel, setChannelDeleteAt, storeMyChannelsForTeam, switchToChannel} from '@actions/local/channel';
import {switchToGlobalThreads} from '@actions/local/thread';
import {loadCallForChannel} from '@calls/actions/calls';
@ -1279,7 +1279,6 @@ export const getGroupMessageMembersCommonTeams = async (serverUrl: string, chann
export const convertGroupMessageToPrivateChannel = async (serverUrl: string, channelId: string, teamId: string, displayName: string) => {
try {
const client = NetworkManager.getClient(serverUrl);
const name = generateChannelNameFromDisplayName(displayName);
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
@ -1288,6 +1287,7 @@ export const convertGroupMessageToPrivateChannel = async (serverUrl: string, cha
EphemeralStore.addConvertingChannel(channelId);
}
const client = NetworkManager.getClient(serverUrl);
const updatedChannel = await client.convertGroupMessageToPrivateChannel(channelId, teamId, displayName, name);
if (existingChannel) {
@ -1297,7 +1297,14 @@ export const convertGroupMessageToPrivateChannel = async (serverUrl: string, cha
channel.name = name;
});
await operator.batchRecords([existingChannel], 'convertGroupMessageToPrivateChannel');
const models: any[] = [existingChannel];
const {models: categoryUpdateModels} = await putGMInCorrectCategory(serverUrl, channelId, teamId, true);
if (categoryUpdateModels) {
models.push(...categoryUpdateModels);
}
await operator.batchRecords(models, 'convertGroupMessageToPrivateChannel');
}
return {updatedChannel};

View file

@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {addChannelToDefaultCategory} from '@actions/local/category';
import {addChannelToDefaultCategory, putGMInCorrectCategory} from '@actions/local/category';
import {
markChannelAsViewed, removeCurrentUserFromChannel, setChannelDeleteAt,
storeMyChannelsForTeam, updateChannelInfoFromChannel, updateMyChannelFromWebsocket,
@ -12,7 +12,7 @@ import {fetchPostsForChannel} from '@actions/remote/post';
import {fetchRolesIfNeeded} from '@actions/remote/role';
import {fetchUsersByIds, updateUsersNoLongerVisible} from '@actions/remote/user';
import {loadCallForChannel} from '@calls/actions/calls';
import {Events} from '@constants';
import {Events, General} from '@constants';
import DatabaseManager from '@database/manager';
import {deleteChannelMembership, getChannelById, prepareMyChannelsForTeam, getCurrentChannel} from '@queries/servers/channel';
import {getConfig, getCurrentChannelId} from '@queries/servers/system';
@ -93,14 +93,29 @@ export async function handleChannelConvertedEvent(serverUrl: string, msg: any) {
export async function handleChannelUpdatedEvent(serverUrl: string, msg: any) {
try {
const {operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const updatedChannel = JSON.parse(msg.data.channel) as Channel;
// if (EphemeralStore.isConvertingChannel(updatedChannel.id)) {
// return;
// }
logDebug(`updatedChannel.id: ${updatedChannel.id}`);
const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const existingChannel = await getChannelById(database, updatedChannel.id);
const existingChannelType = existingChannel?.type;
const updatedChannel = JSON.parse(msg.data.channel);
const models: Model[] = await operator.handleChannel({channels: [updatedChannel], prepareRecordsOnly: true});
const infoModel = await updateChannelInfoFromChannel(serverUrl, updatedChannel, true);
if (infoModel.model) {
models.push(...infoModel.model);
}
operator.batchRecords(models, 'handleChannelUpdatedEvent');
if (existingChannelType === General.GM_CHANNEL && updatedChannel.type === General.PRIVATE_CHANNEL) {
logDebug('Yo yoyo!');
await putGMInCorrectCategory(serverUrl, updatedChannel.id, updatedChannel.team_id);
}
} catch {
// Do nothing
}

View file

@ -34,8 +34,8 @@ export const queryCategoriesByTeamIds = (database: Database, teamIds: string[])
return database.get<CategoryModel>(CATEGORY).query(Q.where('team_id', Q.oneOf(teamIds)));
};
export const queryAllCategories = (database: Database) => {
return database.get<CategoryModel>(CATEGORY).query();
export const queryCategoryChannelsByChannelId = (database: Database, channelId: string) => {
return database.get<CategoryChannelModel>(CATEGORY_CHANNEL).query(Q.where('channel_id', Q.eq(channelId)));
};
export async function prepareCategoriesAndCategoriesChannels(operator: ServerDataOperator, categories: CategoryWithChannels[], prune = false) {

View file

@ -6,16 +6,13 @@ import {switchMap, distinctUntilChanged} from '@nozbe/watermelondb/utils/rx';
import withObservables from '@nozbe/with-observables';
import {of as of$} from 'rxjs';
import {queryAllCategories} from '@app/queries/servers/categories';
import {observeCurrentUser, observeTeammateNameDisplay} from '@app/queries/servers/user';
import {logDebug} from '@app/utils/log';
import {ConvertGMToChannelForm} from './convert_gm_to_channel_form';
import type {WithDatabaseArgs} from '@typings/database/database';
const enhanced = withObservables([], ({database}: WithDatabaseArgs) => {
logDebug('CCCCCCCCCCC');
const locale = observeCurrentUser(database).pipe(
switchMap((user) => of$(user?.locale)),
distinctUntilChanged(),
@ -23,11 +20,6 @@ const enhanced = withObservables([], ({database}: WithDatabaseArgs) => {
const teammateNameDisplay = observeTeammateNameDisplay(database);
logDebug('AAAAAAAAAAAAAAAAAAAAAA');
const allCategories = queryAllCategories(database);
logDebug(allCategories);
return {
locale,
teammateNameDisplay,