Files
upstream-mattermost/webapp/channels/src/actions/channel_actions.ts
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

193 lines
6.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {batchActions} from 'redux-batched-actions';
import type {Channel} from '@mattermost/types/channels';
import type {ServerError} from '@mattermost/types/errors';
import type {UserProfile} from '@mattermost/types/users';
import {PreferenceTypes} from 'mattermost-redux/action_types';
import * as ChannelActions from 'mattermost-redux/actions/channels';
import {savePreferences} from 'mattermost-redux/actions/preferences';
import {getChannelByName, getUnreadChannelIds, getChannel} from 'mattermost-redux/selectors/entities/channels';
import {getCurrentTeamUrl, getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {loadNewDMIfNeeded, loadNewGMIfNeeded, loadProfilesForSidebar} from 'actions/user_actions';
import {getHistory} from 'utils/browser_history';
import {Constants, Preferences, NotificationLevels} from 'utils/constants';
import {getDirectChannelName} from 'utils/utils';
import type {ActionFuncAsync} from 'types/store';
export function openDirectChannelToUserId(userId: UserProfile['id']): ActionFuncAsync<Channel> {
return async (dispatch, getState) => {
const state = getState();
const currentUserId = getCurrentUserId(state);
const channelName = getDirectChannelName(currentUserId, userId);
const channel = getChannelByName(state, channelName);
if (!channel) {
return dispatch(ChannelActions.createDirectChannel(currentUserId, userId));
}
const now = Date.now();
const prefDirect = {
category: Preferences.CATEGORY_DIRECT_CHANNEL_SHOW,
name: userId,
value: 'true',
};
const prefOpenTime = {
category: Preferences.CATEGORY_CHANNEL_OPEN_TIME,
name: channel.id,
value: now.toString(),
};
const actions = [{
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: [prefDirect],
}, {
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: [prefOpenTime],
}];
dispatch(batchActions(actions));
dispatch(savePreferences(currentUserId, [
{user_id: currentUserId, ...prefDirect},
{user_id: currentUserId, ...prefOpenTime},
]));
return {data: channel};
};
}
export function openGroupChannelToUserIds(userIds: Array<UserProfile['id']>): ActionFuncAsync<Channel> {
return async (dispatch, getState) => {
const result = await dispatch(ChannelActions.createGroupChannel(userIds));
if (result.error) {
getHistory().push(getCurrentTeamUrl(getState()));
}
return result;
};
}
export function loadChannelsForCurrentUser(): ActionFuncAsync {
return async (dispatch, getState) => {
const state = getState();
const unreads = getUnreadChannelIds(state);
await dispatch(ChannelActions.fetchChannelsAndMembers(getCurrentTeamId(state)));
for (const id of unreads) {
const channel = getChannel(state, id);
if (channel && channel.type === Constants.DM_CHANNEL) {
dispatch(loadNewDMIfNeeded(channel.id));
} else if (channel && channel.type === Constants.GM_CHANNEL) {
dispatch(loadNewGMIfNeeded(channel.id));
}
}
loadProfilesForSidebar();
return {data: true};
};
}
export function autocompleteChannels(term: string, success: (channels: Channel[]) => void, error?: (err: ServerError) => void): ActionFuncAsync<boolean> {
return async (dispatch, getState) => {
const state = getState();
const teamId = getCurrentTeamId(state);
if (!teamId) {
return {data: false};
}
const {data, error: err} = await dispatch(ChannelActions.autocompleteChannels(teamId, term));
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
return {data: true};
};
}
export function autocompleteActiveChannels(term: string, success: (channels: Channel[]) => void, error?: (err: ServerError) => void): ActionFuncAsync {
return async (dispatch, getState) => {
const state = getState();
const teamId = getCurrentTeamId(state);
if (!teamId) {
return {data: false};
}
const {data, error: err} = await dispatch(ChannelActions.autocompleteChannels(teamId, term));
if (data && success) {
const activeChannels = data.filter((channel: Channel) => channel.delete_at === 0);
success(activeChannels);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
return {data: true};
};
}
export function autocompleteChannelsForSearch(term: string, success?: (channels: Channel[]) => void, error?: (err: ServerError) => void): ActionFuncAsync {
return async (dispatch, getState) => {
const state = getState();
const teamId = getCurrentTeamId(state);
if (!teamId) {
return {data: false};
}
const {data, error: err} = await dispatch(ChannelActions.autocompleteChannelsForSearch(teamId, term));
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
return {data: true};
};
}
export function autocompleteChannelsForSearchInTeam(term: string, teamId: string, success?: (channels: Channel[]) => void, error?: (err: ServerError) => void): ActionFuncAsync {
return async (dispatch) => {
if (!teamId) {
return {data: false};
}
const {data, error: err} = await dispatch(ChannelActions.autocompleteChannelsForSearch(teamId, term));
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
return {data: true};
};
}
export function addUsersToChannel(channelId: Channel['id'], userIds: Array<UserProfile['id']>): ActionFuncAsync {
return async (dispatch) => {
const error = await dispatch(ChannelActions.addChannelMembers(channelId, userIds));
if (error) {
return error;
}
return {data: true};
};
}
export function unmuteChannel(userId: UserProfile['id'], channelId: Channel['id']) {
return ChannelActions.updateChannelNotifyProps(userId, channelId, {
mark_unread: NotificationLevels.ALL,
});
}
export function muteChannel(userId: UserProfile['id'], channelId: Channel['id']) {
return ChannelActions.updateChannelNotifyProps(userId, channelId, {
mark_unread: NotificationLevels.MENTION,
});
}