Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
PLT-6023: Add Users to Team in WebApp. (#5956)
Browse files Browse the repository at this point in the history
* PLT-6198: Use added to channel system message on default channels.

Use a different sytem message when a user was added to a default channel
by someone else than when they joined themselves.

* PLT-6023: Add Users to Team in WebApp.

* Fix string text.

* Handle added_to_team websocket message.

* Fix unread flag on new channel.
  • Loading branch information
grundleborg authored and crspeller committed Apr 4, 2017
1 parent 16685c3 commit 536b182
Show file tree
Hide file tree
Showing 12 changed files with 770 additions and 81 deletions.
25 changes: 25 additions & 0 deletions actions/team_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ export function addUserToTeamFromInvite(data, hash, inviteId, success, error) {
);
}

export function addUsersToTeam(teamId, userIds, success, error) {
Client.addUsersToTeam(
teamId,
userIds,
(teamMembers) => {
teamMembers.forEach((member) => {
TeamStore.removeMemberNotInTeam(teamId, member.user_id);
UserStore.removeProfileNotInTeam(teamId, member.user_id);
});
UserStore.emitNotInTeamChange();

if (success) {
success(teamMembers);
}
},
(err) => {
AsyncClient.dispatchError(err, 'addUsersToTeam');

if (error) {
error(err);
}
}
);
}

export function getInviteInfo(inviteId, success, error) {
Client.getInviteInfo(
inviteId,
Expand Down
22 changes: 22 additions & 0 deletions actions/user_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,28 @@ export function searchUsers(term, teamId = TeamStore.getCurrentId(), options = {
);
}

export function searchUsersNotInTeam(term, teamId = TeamStore.getCurrentId(), options = {}, success, error) {
Client.searchUsersNotInTeam(
term,
teamId,
options,
(data) => {
loadStatusesForProfilesList(data);

if (success) {
success(data);
}
},
(err) => {
AsyncClient.dispatchError(err, 'searchUsersNotInTeam');

if (error) {
error(err);
}
}
);
}

export function autocompleteUsersInChannel(username, channelId, success, error) {
Client.autocompleteUsersInChannel(
username,
Expand Down
25 changes: 25 additions & 0 deletions actions/websocket_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ function handleEvent(msg) {
handleUpdateTeamEvent(msg);
break;

case SocketEvents.ADDED_TO_TEAM:
handleTeamAddedEvent(msg);
break;

case SocketEvents.USER_ADDED:
handleUserAddedEvent(msg);
break;
Expand Down Expand Up @@ -241,6 +245,27 @@ function handlePostDeleteEvent(msg) {
GlobalActions.emitPostDeletedEvent(post);
}

function handleTeamAddedEvent(msg) {
Client.getTeam(msg.data.team_id, (team) => {
AppDispatcher.handleServerAction({
type: ActionTypes.RECEIVED_TEAM,
team
});

Client.getMyTeamMembers((data) => {
AppDispatcher.handleServerAction({
type: ActionTypes.RECEIVED_MY_TEAM_MEMBERS,
team_members: data
});
AsyncClient.getMyTeamsUnread();
}, (err) => {
AsyncClient.dispatchError(err, 'getMyTeamMembers');
});
}, (err) => {
AsyncClient.dispatchError(err, 'getTeam');
});
}

function handleLeaveTeamEvent(msg) {
if (UserStore.getCurrentId() === msg.data.user_id) {
TeamStore.removeMyTeamMember(msg.data.team_id);
Expand Down
69 changes: 69 additions & 0 deletions client/client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,15 @@ export default class Client {

// Team Routes Section

getTeam(teamId, success, error) {
request.
get(`${this.getTeamsRoute()}/${teamId}/me`).
set(this.defaultHeaders).
type('application/json').
accept('application/json').
end(this.handleResponse.bind(this, 'getTeam', success, error));
}

findTeamByName(teamName, success, error) {
request.
post(`${this.getTeamsRoute()}/find_team_by_name`).
Expand Down Expand Up @@ -681,6 +690,30 @@ export default class Client {
this.trackEvent('api', 'api_teams_invite_members');
}

addUsersToTeam(teamId, userIds, success, error) {
let nonEmptyTeamId = teamId;
if (nonEmptyTeamId === '') {
nonEmptyTeamId = this.getTeamId();
}

const teamMembers = userIds.map((userId) => {
return {
team_id: nonEmptyTeamId,
user_id: userId
};
});

request.
post(`${this.url}/api/v4/teams/${nonEmptyTeamId}/members/batch`).
set(this.defaultHeaders).
type('application/json').
accept('application/json').
send(teamMembers).
end(this.handleResponse.bind(this, 'addUsersToTeam', success, error));

this.trackEvent('api', 'api_teams_batch_add_members', {team_id: nonEmptyTeamId, count: teamMembers.length});
}

removeUserFromTeam(teamId, userId, success, error) {
let nonEmptyTeamId = teamId;
if (nonEmptyTeamId === '') {
Expand Down Expand Up @@ -1124,6 +1157,29 @@ export default class Client {
this.trackEvent('api', 'api_profiles_get_in_team', {team_id: teamId});
}

getProfilesNotInTeam(teamId, offset, limit, success, error) {
// Super hacky, but this option only exists in api v4
function wrappedSuccess(data, res) {
// Convert the profile list provided by api v4 to a map to match similar v3 calls
const profiles = {};

for (const profile of data) {
profiles[profile.id] = profile;
}

success(profiles, res);
}

request.
get(`${this.url}/api/v4/users?not_in_team=${this.getTeamId()}&page=${offset}&per_page=${limit}`).
set(this.defaultHeaders).
type('application/json').
accept('application/json').
end(this.handleResponse.bind(this, 'getProfilesNotInTeam', wrappedSuccess, error));

this.trackEvent('api', 'api_profiles_get_not_in_team', {team_id: teamId});
}

getProfilesInChannel(channelId, offset, limit, success, error) {
request.
get(`${this.getChannelNeededRoute(channelId)}/users/${offset}/${limit}`).
Expand Down Expand Up @@ -1191,6 +1247,19 @@ export default class Client {
end(this.handleResponse.bind(this, 'searchUsers', success, error));
}

searchUsersNotInTeam(term, teamId, options, success, error) {
// Note that this is calling an APIv4 Endpoint since no APIv3 equivalent exists.
request.
post(`${this.url}/api/v4/users/search`).
set(this.defaultHeaders).
type('application/json').
accept('application/json').
send({term, not_in_team_id: teamId, ...options}).
end(this.handleResponse.bind(this, 'searchUsersNotInTeam', success, error));

this.trackEvent('api', 'api_search_users_not_in_team', {team_id: teamId});
}

autocompleteUsersInChannel(term, channelId, success, error) {
request.
get(`${this.getChannelNeededRoute(channelId)}/users/autocomplete?term=${encodeURIComponent(term)}`).
Expand Down
Loading

0 comments on commit 536b182

Please sign in to comment.