Skip to content

Commit

Permalink
Merge branch 'gekidou' into MM-42160-Mention-badge
Browse files Browse the repository at this point in the history
  • Loading branch information
shaz-r committed Mar 23, 2022
2 parents ab45851 + 8093047 commit eea5e87
Show file tree
Hide file tree
Showing 180 changed files with 1,651 additions and 2,004 deletions.
8 changes: 4 additions & 4 deletions app/actions/local/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {Model} from '@nozbe/watermelondb';

import DatabaseManager from '@database/manager';
import {prepareCategories, prepareCategoryChannels, queryCategoriesByTeamIds, queryCategoryById} from '@queries/servers/categories';
import {prepareCategories, prepareCategoryChannels, queryCategoriesByTeamIds, getCategoryById} from '@queries/servers/categories';
import {pluckUnique} from '@utils/helpers';

export const deleteCategory = async (serverUrl: string, categoryId: string) => {
Expand All @@ -14,7 +14,7 @@ export const deleteCategory = async (serverUrl: string, categoryId: string) => {
}

try {
const category = await queryCategoryById(database, categoryId);
const category = await getCategoryById(database, categoryId);

if (category) {
await database.write(async () => {
Expand Down Expand Up @@ -56,7 +56,7 @@ export const storeCategories = async (serverUrl: string, categories: CategoryWit

// If the passed categories have more than one team, we want to update across teams
const teamIds = pluckUnique('team_id')(categories) as string[];
const localCategories = await queryCategoriesByTeamIds(database, teamIds);
const localCategories = await queryCategoriesByTeamIds(database, teamIds).fetch();

localCategories.
filter((category) => category.type === 'custom').
Expand Down Expand Up @@ -92,7 +92,7 @@ export const toggleCollapseCategory = async (serverUrl: string, categoryId: stri
}

try {
const category = await queryCategoryById(database, categoryId);
const category = await getCategoryById(database, categoryId);

if (category) {
await database.write(async () => {
Expand Down
14 changes: 7 additions & 7 deletions app/actions/local/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {Navigation} from '@constants';
import {SYSTEM_IDENTIFIERS} from '@constants/database';
import DatabaseManager from '@database/manager';
import ServerDataOperator from '@database/operator/server_data_operator';
import {queryMyChannel} from '@queries/servers/channel';
import {queryCommonSystemValues, queryTeamHistory} from '@queries/servers/system';
import {queryChannelHistory} from '@queries/servers/team';
import {getMyChannel} from '@queries/servers/channel';
import {getCommonSystemValues, getTeamHistory} from '@queries/servers/system';
import {getTeamChannelHistory} from '@queries/servers/team';
import {dismissAllModalsAndPopToRoot, dismissAllModalsAndPopToScreen} from '@screens/navigation';

import {switchToChannel} from './channel';
Expand All @@ -37,10 +37,10 @@ jest.mock('@utils/helpers', () => {
});

const queryDatabaseValues = async (database: Database, teamId: string, channelId: string) => ({
systemValues: await queryCommonSystemValues(database),
teamHistory: await queryTeamHistory(database),
channelHistory: await queryChannelHistory(database, teamId),
member: await queryMyChannel(database, channelId),
systemValues: await getCommonSystemValues(database),
teamHistory: await getTeamHistory(database),
channelHistory: await getTeamChannelHistory(database, teamId),
member: await getMyChannel(database, channelId),
});

describe('switchToChannel', () => {
Expand Down
132 changes: 65 additions & 67 deletions app/actions/local/channel.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {Model, Q} from '@nozbe/watermelondb';
import {Model} from '@nozbe/watermelondb';
import {DeviceEventEmitter} from 'react-native';

import {General, Navigation as NavigationConstants, Preferences, Screens} from '@constants';
import {MM_TABLES} from '@constants/database';
import DatabaseManager from '@database/manager';
import {getTeammateNameDisplaySetting} from '@helpers/api/preference';
import {prepareDeleteChannel, prepareMyChannelsForTeam, queryAllMyChannelIds, queryChannelsById, queryMyChannel} from '@queries/servers/channel';
import {prepareDeleteChannel, prepareMyChannelsForTeam, queryAllMyChannel, getMyChannel, getChannelById, queryUsersOnChannel} from '@queries/servers/channel';
import {queryPreferencesByCategoryAndName} from '@queries/servers/preference';
import {prepareCommonSystemValues, PrepareCommonSystemValuesArgs, queryCommonSystemValues, queryCurrentTeamId, setCurrentChannelId} from '@queries/servers/system';
import {addChannelToTeamHistory, addTeamToTeamHistory, queryTeamById, removeChannelFromTeamHistory} from '@queries/servers/team';
import {queryCurrentUser} from '@queries/servers/user';
import {prepareCommonSystemValues, PrepareCommonSystemValuesArgs, getCommonSystemValues, getCurrentTeamId, setCurrentChannelId} from '@queries/servers/system';
import {addChannelToTeamHistory, addTeamToTeamHistory, getTeamById, removeChannelFromTeamHistory} from '@queries/servers/team';
import {getCurrentUser} from '@queries/servers/user';
import {dismissAllModalsAndPopToRoot, dismissAllModalsAndPopToScreen} from '@screens/navigation';
import {isTablet} from '@utils/helpers';
import {displayGroupMessageName, displayUsername, getUserIdFromChannelName} from '@utils/user';

import type ChannelModel from '@typings/database/models/servers/channel';
import type UserModel from '@typings/database/models/servers/user';

const {SERVER: {CHANNEL_MEMBERSHIP, USER}} = MM_TABLES;

export const switchToChannel = async (serverUrl: string, channelId: string, teamId?: string, prepareRecordsOnly = false) => {
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
if (!operator) {
Expand All @@ -33,63 +30,65 @@ export const switchToChannel = async (serverUrl: string, channelId: string, team
try {
const dt = Date.now();
const isTabletDevice = await isTablet();
const system = await queryCommonSystemValues(database);
const member = await queryMyChannel(database, channelId);
const system = await getCommonSystemValues(database);
const member = await getMyChannel(database, channelId);

if (member) {
const channel: ChannelModel = await member.channel.fetch();
if (!channel.teamId && teamId) {
const team = await queryTeamById(database, teamId);
if (!team) {
return {error: `team with id ${teamId} not found`};
const channel = await member.channel.fetch();
if (channel) {
if (!channel.teamId && teamId) {
const team = await getTeamById(database, teamId);
if (!team) {
return {error: `team with id ${teamId} not found`};
}
}
}
const toTeamId = channel.teamId || teamId || system.currentTeamId;
const toTeamId = channel.teamId || teamId || system.currentTeamId;

if (isTabletDevice && system.currentChannelId !== channelId) {
// On tablet, the channel is being rendered, by setting the channel to empty first we speed up
// the switch by ~3x
await setCurrentChannelId(operator, '');
}
if (isTabletDevice && system.currentChannelId !== channelId) {
// On tablet, the channel is being rendered, by setting the channel to empty first we speed up
// the switch by ~3x
await setCurrentChannelId(operator, '');
}

if (system.currentTeamId !== toTeamId) {
const history = await addTeamToTeamHistory(operator, toTeamId, true);
models.push(...history);
}
if (system.currentTeamId !== toTeamId) {
const history = await addTeamToTeamHistory(operator, toTeamId, true);
models.push(...history);
}

if ((system.currentTeamId !== toTeamId) || (system.currentChannelId !== channelId)) {
const commonValues: PrepareCommonSystemValuesArgs = {
currentChannelId: system.currentChannelId === channelId ? undefined : channelId,
currentTeamId: system.currentTeamId === toTeamId ? undefined : toTeamId,
};
const common = await prepareCommonSystemValues(operator, commonValues);
if (common) {
models.push(...common);
if ((system.currentTeamId !== toTeamId) || (system.currentChannelId !== channelId)) {
const commonValues: PrepareCommonSystemValuesArgs = {
currentChannelId: system.currentChannelId === channelId ? undefined : channelId,
currentTeamId: system.currentTeamId === toTeamId ? undefined : toTeamId,
};
const common = await prepareCommonSystemValues(operator, commonValues);
if (common) {
models.push(...common);
}
}
}

if (system.currentChannelId !== channelId || system.currentTeamId !== toTeamId) {
const history = await addChannelToTeamHistory(operator, toTeamId, channelId, true);
models.push(...history);
}
if (system.currentChannelId !== channelId || system.currentTeamId !== toTeamId) {
const history = await addChannelToTeamHistory(operator, toTeamId, channelId, true);
models.push(...history);
}

const {member: viewedAt} = await markChannelAsViewed(serverUrl, channelId, true);
if (viewedAt) {
models.push(viewedAt);
}
const {member: viewedAt} = await markChannelAsViewed(serverUrl, channelId, true);
if (viewedAt) {
models.push(viewedAt);
}

if (models.length && !prepareRecordsOnly) {
await operator.batchRecords(models);
}
if (models.length && !prepareRecordsOnly) {
await operator.batchRecords(models);
}

if (isTabletDevice) {
dismissAllModalsAndPopToRoot();
DeviceEventEmitter.emit(NavigationConstants.NAVIGATION_HOME);
} else {
dismissAllModalsAndPopToScreen(Screens.CHANNEL, '', undefined, {topBar: {visible: false}});
}
if (isTabletDevice) {
dismissAllModalsAndPopToRoot();
DeviceEventEmitter.emit(NavigationConstants.NAVIGATION_HOME);
} else {
dismissAllModalsAndPopToScreen(Screens.CHANNEL, '', undefined, {topBar: {visible: false}});
}

console.log('channel switch to', channel?.displayName, channelId, (Date.now() - dt), 'ms'); //eslint-disable-line
console.log('channel switch to', channel?.displayName, channelId, (Date.now() - dt), 'ms'); //eslint-disable-line
}
}
} catch (error) {
return {error};
Expand All @@ -107,13 +106,13 @@ export const removeCurrentUserFromChannel = async (serverUrl: string, channelId:
const {operator, database} = serverDatabase;

const models: Model[] = [];
const myChannel = await queryMyChannel(database, channelId);
const myChannel = await getMyChannel(database, channelId);
if (myChannel) {
const channel = await myChannel.channel.fetch() as ChannelModel;
models.push(...await prepareDeleteChannel(channel));
let teamId = channel.teamId;
if (teamId) {
teamId = await queryCurrentTeamId(database);
teamId = await getCurrentTeamId(database);
}
const system = await removeChannelFromTeamHistory(operator, teamId, channel.id, true);
if (system) {
Expand All @@ -139,12 +138,11 @@ export const setChannelDeleteAt = async (serverUrl: string, channelId: string, d

const {operator, database} = serverDatabase;

const channels = await queryChannelsById(database, [channelId]);
if (!channels?.length) {
const channel = await getChannelById(database, channelId);
if (!channel) {
return;
}

const channel = channels[0];
const model = channel.prepareUpdate((c) => {
c.deleteAt = deleteAt;
});
Expand All @@ -163,7 +161,7 @@ export const selectAllMyChannelIds = async (serverUrl: string) => {
return [];
}

return queryAllMyChannelIds(database);
return queryAllMyChannel(database).fetchIds();
};

export const markChannelAsViewed = async (serverUrl: string, channelId: string, prepareRecordsOnly = false) => {
Expand All @@ -172,7 +170,7 @@ export const markChannelAsViewed = async (serverUrl: string, channelId: string,
return {error: `${serverUrl} database not found`};
}

const member = await queryMyChannel(operator.database, channelId);
const member = await getMyChannel(operator.database, channelId);
if (!member) {
return {error: 'not a member'};
}
Expand Down Expand Up @@ -202,7 +200,7 @@ export const markChannelAsUnread = async (serverUrl: string, channelId: string,
return {error: `${serverUrl} database not found`};
}

const member = await queryMyChannel(operator.database, channelId);
const member = await getMyChannel(operator.database, channelId);
if (!member) {
return {error: 'not a member'};
}
Expand Down Expand Up @@ -233,7 +231,7 @@ export const resetMessageCount = async (serverUrl: string, channelId: string) =>
return {error: `${serverUrl} database not found`};
}

const member = await queryMyChannel(operator.database, channelId);
const member = await getMyChannel(operator.database, channelId);
if (!member) {
return {error: 'not a member'};
}
Expand Down Expand Up @@ -287,7 +285,7 @@ export const updateLastPostAt = async (serverUrl: string, channelId: string, las
return {error: `${serverUrl} database not found`};
}

const member = await queryMyChannel(operator.database, channelId);
const member = await getMyChannel(operator.database, channelId);
if (!member) {
return {error: 'not a member'};
}
Expand Down Expand Up @@ -316,13 +314,13 @@ export async function updateChannelsDisplayName(serverUrl: string, channels: Cha
}

const {database} = operator;
const currentUser = await queryCurrentUser(database);
const currentUser = await getCurrentUser(database);
if (!currentUser) {
return {};
}

const {config, license} = await queryCommonSystemValues(database);
const preferences = await queryPreferencesByCategoryAndName(database, Preferences.CATEGORY_DISPLAY_SETTINGS, Preferences.NAME_NAME_FORMAT);
const {config, license} = await getCommonSystemValues(database);
const preferences = await queryPreferencesByCategoryAndName(database, Preferences.CATEGORY_DISPLAY_SETTINGS, Preferences.NAME_NAME_FORMAT).fetch();
const displaySettings = getTeammateNameDisplaySetting(preferences, config, license);
const models: Model[] = [];
for await (const channel of channels) {
Expand All @@ -332,7 +330,7 @@ export async function updateChannelsDisplayName(serverUrl: string, channels: Cha
const user = users.find((u) => u.id === otherUserId);
newDisplayName = displayUsername(user, currentUser.locale, displaySettings);
} else {
const dbProfiles = await database.get<UserModel>(USER).query(Q.on(CHANNEL_MEMBERSHIP, Q.where('channel_id', channel.id))).fetch();
const dbProfiles = await queryUsersOnChannel(database, channel.id).fetch();
const profileIds = dbProfiles.map((p) => p.id);
const gmUsers = users.filter((u) => profileIds.includes(u.id));
if (gmUsers.length) {
Expand Down
12 changes: 6 additions & 6 deletions app/actions/local/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// See LICENSE.txt for license information.

import DatabaseManager from '@database/manager';
import {queryDraft} from '@queries/servers/drafts';
import {getDraft} from '@queries/servers/drafts';

export const updateDraftFile = async (serverUrl: string, channelId: string, rootId: string, file: FileInfo, prepareRecordsOnly = false) => {
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
if (!operator) {
return {error: `${serverUrl} database not found`};
}

const draft = await queryDraft(operator.database, channelId, rootId);
const draft = await getDraft(operator.database, channelId, rootId);
if (!draft) {
return {error: 'no draft'};
}
Expand Down Expand Up @@ -44,7 +44,7 @@ export const removeDraftFile = async (serverUrl: string, channelId: string, root
return {error: `${serverUrl} database not found`};
}

const draft = await queryDraft(operator.database, channelId, rootId);
const draft = await getDraft(operator.database, channelId, rootId);
if (!draft) {
return {error: 'no draft'};
}
Expand Down Expand Up @@ -79,7 +79,7 @@ export const updateDraftMessage = async (serverUrl: string, channelId: string, r
return {error: `${serverUrl} database not found`};
}

const draft = await queryDraft(operator.database, channelId, rootId);
const draft = await getDraft(operator.database, channelId, rootId);
if (!draft) {
if (!message) {
return {};
Expand Down Expand Up @@ -123,7 +123,7 @@ export const addFilesToDraft = async (serverUrl: string, channelId: string, root
return {error: `${serverUrl} database not found`};
}

const draft = await queryDraft(operator.database, channelId, rootId);
const draft = await getDraft(operator.database, channelId, rootId);
if (!draft) {
const newDraft: Draft = {
channel_id: channelId,
Expand Down Expand Up @@ -156,7 +156,7 @@ export const removeDraft = async (serverUrl: string, channelId: string, rootId =
return {error: `${serverUrl} database not found`};
}

const draft = await queryDraft(database, channelId, rootId);
const draft = await getDraft(database, channelId, rootId);
if (draft) {
await database.write(async () => {
await draft.destroyPermanently();
Expand Down
Loading

0 comments on commit eea5e87

Please sign in to comment.