mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
456 lines (455 loc) • 17.2 kB
JavaScript
"use strict";
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.linkGroupSyncable = linkGroupSyncable;
exports.unlinkGroupSyncable = unlinkGroupSyncable;
exports.getGroupSyncables = getGroupSyncables;
exports.patchGroupSyncable = patchGroupSyncable;
exports.getGroup = getGroup;
exports.getGroups = getGroups;
exports.getGroupsNotAssociatedToTeam = getGroupsNotAssociatedToTeam;
exports.getGroupsNotAssociatedToChannel = getGroupsNotAssociatedToChannel;
exports.getAllGroupsAssociatedToTeam = getAllGroupsAssociatedToTeam;
exports.getAllGroupsAssociatedToChannelsInTeam = getAllGroupsAssociatedToChannelsInTeam;
exports.getAllGroupsAssociatedToChannel = getAllGroupsAssociatedToChannel;
exports.getGroupsAssociatedToTeam = getGroupsAssociatedToTeam;
exports.getGroupsAssociatedToChannel = getGroupsAssociatedToChannel;
exports.patchGroup = patchGroup;
exports.getGroupsByUserId = getGroupsByUserId;
exports.getGroupsByNames = getGroupsByNames;
exports.getGroupsByUserIdPaginated = getGroupsByUserIdPaginated;
exports.getGroupStats = getGroupStats;
exports.createGroupWithUserIds = createGroupWithUserIds;
exports.addUsersToGroup = addUsersToGroup;
exports.removeUsersFromGroup = removeUsersFromGroup;
exports.searchGroups = searchGroups;
exports.archiveGroup = archiveGroup;
exports.restoreGroup = restoreGroup;
exports.createGroupTeamsAndChannels = createGroupTeamsAndChannels;
const redux_batched_actions_1 = require("redux-batched-actions");
const groups_1 = require("@mattermost/types/groups");
const action_types_1 = require("mattermost-redux/action_types");
const client_1 = require("mattermost-redux/client");
const constants_1 = require("mattermost-redux/constants");
const errors_1 = require("./errors");
const helpers_1 = require("./helpers");
function linkGroupSyncable(groupID, syncableID, syncableType, patch) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.linkGroupSyncable(groupID, syncableID, syncableType, patch);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
const dispatches = [];
let type = '';
switch (syncableType) {
case groups_1.SyncableType.Team:
dispatches.push({ type: action_types_1.GroupTypes.RECEIVED_GROUP_ASSOCIATED_TO_TEAM, data: { teamID: syncableID, groups: [{ id: groupID }] } });
type = action_types_1.GroupTypes.LINKED_GROUP_TEAM;
break;
case groups_1.SyncableType.Channel:
type = action_types_1.GroupTypes.LINKED_GROUP_CHANNEL;
break;
default:
console.warn(`unhandled syncable type ${syncableType}`); // eslint-disable-line no-console
}
dispatches.push({ type, data });
dispatch((0, redux_batched_actions_1.batchActions)(dispatches));
return { data: true };
};
}
function unlinkGroupSyncable(groupID, syncableID, syncableType) {
return async (dispatch, getState) => {
try {
await client_1.Client4.unlinkGroupSyncable(groupID, syncableID, syncableType);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
const dispatches = [];
let type = '';
const data = { group_id: groupID, syncable_id: syncableID };
switch (syncableType) {
case groups_1.SyncableType.Team:
type = action_types_1.GroupTypes.UNLINKED_GROUP_TEAM;
data.syncable_id = syncableID;
dispatches.push({ type: action_types_1.GroupTypes.RECEIVED_GROUPS_NOT_ASSOCIATED_TO_TEAM, data: { teamID: syncableID, groups: [{ id: groupID }] } });
break;
case groups_1.SyncableType.Channel:
type = action_types_1.GroupTypes.UNLINKED_GROUP_CHANNEL;
data.syncable_id = syncableID;
break;
default:
console.warn(`unhandled syncable type ${syncableType}`); // eslint-disable-line no-console
}
dispatches.push({ type, data });
dispatch((0, redux_batched_actions_1.batchActions)(dispatches));
return { data: true };
};
}
function getGroupSyncables(groupID, syncableType) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.getGroupSyncables(groupID, syncableType);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
let type = '';
switch (syncableType) {
case groups_1.SyncableType.Team:
type = action_types_1.GroupTypes.RECEIVED_GROUP_TEAMS;
break;
case groups_1.SyncableType.Channel:
type = action_types_1.GroupTypes.RECEIVED_GROUP_CHANNELS;
break;
default:
console.warn(`unhandled syncable type ${syncableType}`); // eslint-disable-line no-console
}
dispatch((0, redux_batched_actions_1.batchActions)([
{ type, data, group_id: groupID },
]));
return { data: true };
};
}
function patchGroupSyncable(groupID, syncableID, syncableType, patch) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.patchGroupSyncable(groupID, syncableID, syncableType, patch);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
return { error };
}
const dispatches = [];
let type = '';
switch (syncableType) {
case groups_1.SyncableType.Team:
type = action_types_1.GroupTypes.PATCHED_GROUP_TEAM;
break;
case groups_1.SyncableType.Channel:
type = action_types_1.GroupTypes.PATCHED_GROUP_CHANNEL;
break;
default:
console.warn(`unhandled syncable type ${syncableType}`); // eslint-disable-line no-console
}
dispatches.push({ type, data });
dispatch((0, redux_batched_actions_1.batchActions)(dispatches));
return { data: true };
};
}
function getGroup(id, includeMemberCount = false) {
return (0, helpers_1.bindClientFunc)({
clientFunc: client_1.Client4.getGroup,
onSuccess: [action_types_1.GroupTypes.RECEIVED_GROUP],
params: [
id,
includeMemberCount,
],
});
}
function getGroups(opts) {
return (0, helpers_1.bindClientFunc)({
clientFunc: async (opts) => {
const result = await client_1.Client4.getGroups(opts);
return result;
},
onSuccess: [action_types_1.GroupTypes.RECEIVED_GROUPS],
params: [
opts,
],
});
}
function getGroupsNotAssociatedToTeam(teamID, q = '', page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT, source = groups_1.GroupSource.Ldap, onlySyncableSources = false) {
return (0, helpers_1.bindClientFunc)({
clientFunc: client_1.Client4.getGroupsNotAssociatedToTeam,
onSuccess: [action_types_1.GroupTypes.RECEIVED_GROUPS],
params: [
teamID,
q,
page,
perPage,
source,
onlySyncableSources,
],
});
}
function getGroupsNotAssociatedToChannel(channelID, q = '', page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT, filterParentTeamPermitted = false, source = groups_1.GroupSource.Ldap, onlySyncableSources = false) {
return (0, helpers_1.bindClientFunc)({
clientFunc: client_1.Client4.getGroupsNotAssociatedToChannel,
onSuccess: [action_types_1.GroupTypes.RECEIVED_GROUPS],
params: [
channelID,
q,
page,
perPage,
filterParentTeamPermitted,
source,
onlySyncableSources,
],
});
}
function getAllGroupsAssociatedToTeam(teamID, filterAllowReference = false, includeMemberCount = false) {
return (0, helpers_1.bindClientFunc)({
clientFunc: async (param1, param2, param3) => {
const result = await client_1.Client4.getAllGroupsAssociatedToTeam(param1, param2, param3);
result.teamID = param1;
return result;
},
onSuccess: [action_types_1.GroupTypes.RECEIVED_ALL_GROUPS_ASSOCIATED_TO_TEAM],
params: [
teamID,
filterAllowReference,
includeMemberCount,
],
});
}
function getAllGroupsAssociatedToChannelsInTeam(teamID, filterAllowReference = false) {
return (0, helpers_1.bindClientFunc)({
clientFunc: async (param1, param2) => {
const result = await client_1.Client4.getAllGroupsAssociatedToChannelsInTeam(param1, param2);
return { groupsByChannelId: result.groups };
},
onSuccess: [action_types_1.GroupTypes.RECEIVED_ALL_GROUPS_ASSOCIATED_TO_CHANNELS_IN_TEAM],
params: [
teamID,
filterAllowReference,
],
});
}
function getAllGroupsAssociatedToChannel(channelID, filterAllowReference = false, includeMemberCount = false) {
return (0, helpers_1.bindClientFunc)({
clientFunc: async (param1, param2, param3) => {
const result = await client_1.Client4.getAllGroupsAssociatedToChannel(param1, param2, param3);
result.channelID = param1;
return result;
},
onSuccess: [action_types_1.GroupTypes.RECEIVED_ALL_GROUPS_ASSOCIATED_TO_CHANNEL],
params: [
channelID,
filterAllowReference,
includeMemberCount,
],
});
}
function getGroupsAssociatedToTeam(teamID, q = '', page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT, filterAllowReference = false) {
return (0, helpers_1.bindClientFunc)({
clientFunc: async (param1, param2, param3, param4, param5) => {
const result = await client_1.Client4.getGroupsAssociatedToTeam(param1, param2, param3, param4, param5);
return { groups: result.groups, totalGroupCount: result.total_group_count, teamID: param1 };
},
onSuccess: [action_types_1.GroupTypes.RECEIVED_GROUPS_ASSOCIATED_TO_TEAM],
params: [
teamID,
q,
page,
perPage,
filterAllowReference,
],
});
}
function getGroupsAssociatedToChannel(channelID, q = '', page = 0, perPage = constants_1.General.PAGE_SIZE_DEFAULT, filterAllowReference = false) {
return (0, helpers_1.bindClientFunc)({
clientFunc: async (param1, param2, param3, param4, param5) => {
const result = await client_1.Client4.getGroupsAssociatedToChannel(param1, param2, param3, param4, param5);
return { groups: result.groups, totalGroupCount: result.total_group_count, channelID: param1 };
},
onSuccess: [action_types_1.GroupTypes.RECEIVED_GROUPS_ASSOCIATED_TO_CHANNEL],
params: [
channelID,
q,
page,
perPage,
filterAllowReference,
],
});
}
function patchGroup(groupID, patch) {
return (0, helpers_1.bindClientFunc)({
clientFunc: client_1.Client4.patchGroup,
onSuccess: [action_types_1.GroupTypes.PATCHED_GROUP],
params: [
groupID,
patch,
],
});
}
function getGroupsByUserId(userID) {
return (0, helpers_1.bindClientFunc)({
clientFunc: client_1.Client4.getGroupsByUserId,
onSuccess: [action_types_1.GroupTypes.RECEIVED_MY_GROUPS],
params: [
userID,
],
});
}
function getGroupsByNames(names) {
return async (dispatch, getState) => {
let groups;
try {
groups = await client_1.Client4.getGroupsByNames(names);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
dispatch({
type: action_types_1.GroupTypes.RECEIVED_GROUPS,
data: groups,
});
return { data: groups };
};
}
function getGroupsByUserIdPaginated(opts) {
return (0, helpers_1.bindClientFunc)({
clientFunc: async (opts) => {
const result = await client_1.Client4.getGroups(opts);
return result;
},
onSuccess: [action_types_1.GroupTypes.RECEIVED_MY_GROUPS, action_types_1.GroupTypes.RECEIVED_GROUPS],
params: [
opts,
],
});
}
function getGroupStats(groupID) {
return (0, helpers_1.bindClientFunc)({
clientFunc: client_1.Client4.getGroupStats,
onSuccess: [action_types_1.GroupTypes.RECEIVED_GROUP_STATS],
params: [
groupID,
],
});
}
function createGroupWithUserIds(group) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.createGroupWithUserIds(group);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
return { error };
}
dispatch({ type: action_types_1.GroupTypes.CREATE_GROUP_SUCCESS, data });
return { data };
};
}
function addUsersToGroup(groupId, userIds) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.addUsersToGroup(groupId, userIds);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
return { error };
}
dispatch({
type: action_types_1.UserTypes.RECEIVED_PROFILES_FOR_GROUP,
data,
id: groupId,
});
return { data };
};
}
function removeUsersFromGroup(groupId, userIds) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.removeUsersFromGroup(groupId, userIds);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
return { error };
}
dispatch({
type: action_types_1.UserTypes.RECEIVED_PROFILES_LIST_TO_REMOVE_FROM_GROUP,
data,
id: groupId,
});
return { data };
};
}
function searchGroups(params) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.searchGroups(params);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
const dispatches = [{ type: action_types_1.GroupTypes.RECEIVED_GROUPS, data }];
if (params.filter_has_member) {
dispatches.push({ type: action_types_1.GroupTypes.RECEIVED_MY_GROUPS, data });
}
if (params.include_channel_member_count) {
dispatches.push({ type: action_types_1.ChannelTypes.RECEIVED_CHANNEL_MEMBER_COUNTS_FROM_GROUPS_LIST, data, channelId: params.include_channel_member_count });
}
dispatch((0, redux_batched_actions_1.batchActions)(dispatches));
return { data };
};
}
function archiveGroup(groupId) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.archiveGroup(groupId);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
return { error };
}
dispatch({
type: action_types_1.GroupTypes.ARCHIVED_GROUP,
id: groupId,
data,
});
return { data };
};
}
function restoreGroup(groupId) {
return async (dispatch, getState) => {
let data;
try {
data = await client_1.Client4.restoreGroup(groupId);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
return { error };
}
dispatch({
type: action_types_1.GroupTypes.RESTORED_GROUP,
id: groupId,
data,
});
return { data };
};
}
function createGroupTeamsAndChannels(userID) {
return async (dispatch, getState) => {
try {
await client_1.Client4.createGroupTeamsAndChannels(userID);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
return { error };
}
return { user_id: userID };
};
}