mattermost-mobile/app/actions/local/team.ts
Elias Nahum 790b1beb22
[Gekidou] push notifications (#5779)
* Push notifications entry point

* Process android notification natively only if RN is not initialized

* Database changes to store local channel viewed_at

* EphemeralStore wait until screen removed

* Move schedule session notification to utility

* Fix channel remote & local actions + added actions for markChannelAsViewed & fetchMyChannel

* Add fetchMyTeam remote action

* Add dismissAllModalsAndPopToScreen to navigation

* Improve post list component & add app state to re-trigger queries

* Improve WS implementation

* Handle push notification events

* Fix postsInChannel since handler

* Handle in-app notifications

* Post list to listen to column changes

* Track selected bottom tab in ephemeral store

* add useIsTablet hook

* in-app notifications on tablets
2021-10-27 17:53:11 -03:00

83 lines
2.9 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {DeviceEventEmitter} from 'react-native';
import {fetchMyChannelsForTeam} from '@actions/remote/channel';
import {fetchPostsForChannel, fetchPostsForUnreadChannels} from '@actions/remote/post';
import {fetchAllTeams} from '@actions/remote/team';
import Events from '@constants/events';
import DatabaseManager from '@database/manager';
import {prepareCommonSystemValues, queryCurrentTeamId} from '@queries/servers/system';
import {prepareDeleteTeam, queryMyTeamById, removeTeamFromTeamHistory, queryLastChannelFromTeam, addTeamToTeamHistory} from '@queries/servers/team';
import {isTablet} from '@utils/helpers';
import type TeamModel from '@typings/database/models/servers/team';
export const handleTeamChange = async (serverUrl: string, teamId: string) => {
const {operator, database} = DatabaseManager.serverDatabases[serverUrl];
const currentTeamId = await queryCurrentTeamId(database);
if (currentTeamId === teamId) {
return;
}
let channelId = '';
if (await isTablet()) {
channelId = await queryLastChannelFromTeam(database, teamId);
if (channelId) {
fetchPostsForChannel(serverUrl, channelId);
}
}
const models = [];
const system = await prepareCommonSystemValues(operator, {currentChannelId: channelId, currentTeamId: teamId});
if (system?.length) {
models.push(...system);
}
const history = await addTeamToTeamHistory(operator, teamId, true);
if (history.length) {
models.push(...history);
}
if (models.length) {
operator.batchRecords(models);
}
const {channels, memberships, error} = await fetchMyChannelsForTeam(serverUrl, teamId);
if (error) {
DeviceEventEmitter.emit(Events.TEAM_LOAD_ERROR, serverUrl, error);
}
if (channels?.length && memberships?.length) {
fetchPostsForUnreadChannels(serverUrl, channels, memberships, channelId);
}
};
export const localRemoveUserFromTeam = async (serverUrl: string, teamId: string) => {
const serverDatabase = DatabaseManager.serverDatabases[serverUrl];
if (!serverDatabase) {
return;
}
const {operator, database} = serverDatabase;
const myTeam = await queryMyTeamById(database, teamId);
if (myTeam) {
const team = await myTeam.team.fetch() as TeamModel;
const models = await prepareDeleteTeam(team);
const system = await removeTeamFromTeamHistory(operator, team.id, true);
if (system) {
models.push(...system);
}
if (models.length) {
try {
await operator.batchRecords(models);
} catch {
// eslint-disable-next-line no-console
console.log('FAILED TO BATCH CHANGES FOR REMOVE USER FROM TEAM');
}
}
fetchAllTeams(serverUrl);
}
};