Skip to content

Commit

Permalink
Remove MakeDirectChannelVisible and add client handling for showing D…
Browse files Browse the repository at this point in the history
…Ms for 3.6 (#5434)
  • Loading branch information
jwilander authored and hmhealey committed Feb 15, 2017
1 parent 71b53f9 commit 44b7fad
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 97 deletions.
1 change: 0 additions & 1 deletion api/command_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (me *msgProvider) DoCommand(c *Context, args *model.CommandArgs, message st
targetChannelId = channel.Data.(*model.Channel).Id
}

makeDirectChannelVisible(targetChannelId)
if len(parsedMessage) > 0 {
post := &model.Post{}
post.Message = parsedMessage
Expand Down
59 changes: 0 additions & 59 deletions api/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,65 +317,6 @@ func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) {
if triggerWebhooks {
go handleWebhookEvents(c, post, team, channel, user)
}

if channel.Type == model.CHANNEL_DIRECT {
go makeDirectChannelVisible(post.ChannelId)
}
}

func makeDirectChannelVisible(channelId string) {
var members []model.ChannelMember
if result := <-Srv.Store.Channel().GetMembers(channelId); result.Err != nil {
l4g.Error(utils.T("api.post.make_direct_channel_visible.get_members.error"), channelId, result.Err.Message)
return
} else {
members = result.Data.([]model.ChannelMember)
}

if len(members) != 2 {
l4g.Error(utils.T("api.post.make_direct_channel_visible.get_2_members.error"), channelId)
return
}

// make sure the channel is visible to both members
for i, member := range members {
otherUserId := members[1-i].UserId

if result := <-Srv.Store.Preference().Get(member.UserId, model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, otherUserId); result.Err != nil {
// create a new preference since one doesn't exist yet
preference := &model.Preference{
UserId: member.UserId,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
Name: otherUserId,
Value: "true",
}

if saveResult := <-Srv.Store.Preference().Save(&model.Preferences{*preference}); saveResult.Err != nil {
l4g.Error(utils.T("api.post.make_direct_channel_visible.save_pref.error"), member.UserId, otherUserId, saveResult.Err.Message)
} else {
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCE_CHANGED, "", "", member.UserId, nil)
message.Add("preference", preference.ToJson())

go Publish(message)
}
} else {
preference := result.Data.(model.Preference)

if preference.Value != "true" {
// update the existing preference to make the channel visible
preference.Value = "true"

if updateResult := <-Srv.Store.Preference().Save(&model.Preferences{preference}); updateResult.Err != nil {
l4g.Error(utils.T("api.post.make_direct_channel_visible.update_pref.error"), member.UserId, otherUserId, updateResult.Err.Message)
} else {
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCE_CHANGED, "", "", member.UserId, nil)
message.Add("preference", preference.ToJson())

go Publish(message)
}
}
}
}
}

func handleWebhookEvents(c *Context, post *model.Post, team *model.Team, channel *model.Channel, user *model.User) {
Expand Down
34 changes: 0 additions & 34 deletions api/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,40 +868,6 @@ func TestFuzzyPosts(t *testing.T) {
}
}

func TestMakeDirectChannelVisible(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := th.BasicTeam
user1 := th.BasicUser
user2 := th.BasicUser2

th.LoginBasic2()

preferences := &model.Preferences{
{
UserId: user2.Id,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
Name: user1.Id,
Value: "false",
},
}
Client.Must(Client.SetPreferences(preferences))

Client.Must(Client.Logout())
th.LoginBasic()
th.BasicClient.SetTeamId(team.Id)

channel := Client.Must(Client.CreateDirectChannel(user2.Id)).Data.(*model.Channel)

makeDirectChannelVisible(channel.Id)

if result, err := Client.GetPreference(model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, user2.Id); err != nil {
t.Fatal("Errored trying to set direct channel to be visible for user1")
} else if pref := result.Data.(*model.Preference); pref.Value != "true" {
t.Fatal("Failed to set direct channel to be visible for user1")
}
}

func TestGetMentionKeywords(t *testing.T) {
// user with username or custom mentions enabled
user1 := &model.User{
Expand Down
5 changes: 5 additions & 0 deletions webapp/actions/post_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import TeamStore from 'stores/team_store.jsx';
import UserStore from 'stores/user_store.jsx';

import {loadStatusesForChannel} from 'actions/status_actions.jsx';
import {loadNewDMIfNeeded} from 'actions/user_actions.jsx';

import Client from 'client/web_client.jsx';
import * as AsyncClient from 'utils/async_client.jsx';
Expand Down Expand Up @@ -42,6 +43,10 @@ export function handleNewPost(post, msg) {
AsyncClient.getMyTeamsUnread(teamId);
}

if (msg && msg.data && msg.data.channel_type === Constants.DM_CHANNEL) {
loadNewDMIfNeeded(post.user_id);
}

if (post.root_id && PostStore.getPost(post.channel_id, post.root_id) == null) {
Client.getPost(
post.channel_id,
Expand Down
46 changes: 45 additions & 1 deletion webapp/actions/user_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {getDirectChannelName} from 'utils/utils.jsx';
import * as AsyncClient from 'utils/async_client.jsx';
import Client from 'client/web_client.jsx';

import {ActionTypes, Preferences} from 'utils/constants.jsx';
import {Constants, ActionTypes, Preferences} from 'utils/constants.jsx';

export function switchFromLdapToEmail(email, password, token, ldapPassword, onSuccess, onError) {
Client.ldapToEmail(
Expand Down Expand Up @@ -144,6 +144,19 @@ function populateDMChannelsWithProfiles(userIds) {
}
}

export function loadNewDMIfNeeded(userId) {
if (userId === UserStore.getCurrentId()) {
return;
}

const pref = PreferenceStore.get(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, userId, 'false');
if (pref === 'false') {
PreferenceStore.setPreference(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, userId, 'true');
AsyncClient.savePreference(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, userId, 'true');
loadProfilesAndTeamMembersForDMSidebar();
}
}

export function loadProfilesAndTeamMembersForDMSidebar() {
const dmPrefs = PreferenceStore.getCategory(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW);
const teamId = TeamStore.getCurrentId();
Expand All @@ -159,6 +172,37 @@ export function loadProfilesAndTeamMembersForDMSidebar() {
}
}

const channelMembers = ChannelStore.getMyMembers();
const channels = ChannelStore.getChannels();
const newPreferences = [];
for (let i = 0; i < channels.length; i++) {
const channel = channels[i];
if (channel.type !== Constants.DM_CHANNEL) {
continue;
}

const member = channelMembers[channel.id];
if (!member) {
continue;
}

const teammateId = channel.name.replace(member.user_id, '').replace('__', '');

if (member.mention_count > 0 && membersToLoad.indexOf(teammateId) === -1) {
membersToLoad.push(teammateId);
newPreferences.push({
user_id: UserStore.getCurrentId(),
category: Preferences.CATEGORY_DIRECT_CHANNEL_SHOW,
name: teammateId,
value: 'true'
});
}
}

if (newPreferences.length > 0) {
AsyncClient.savePreferences(newPreferences);
}

if (profilesToLoad.length > 0) {
Client.getProfilesByIds(
profilesToLoad,
Expand Down
9 changes: 7 additions & 2 deletions webapp/routes/route_team.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import Constants from 'utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;
import * as AsyncClient from 'utils/async_client.jsx';
import * as Utils from 'utils/utils.jsx';
import Client from 'client/web_client.jsx';
import ChannelStore from 'stores/channel_store.jsx';
import BrowserStore from 'stores/browser_store.jsx';

import emojiRoute from 'routes/route_emoji.jsx';
import integrationsRoute from 'routes/route_integrations.jsx';

import {loadProfilesAndTeamMembersForDMSidebar} from 'actions/user_actions.jsx';
import {loadNewDMIfNeeded, loadProfilesAndTeamMembersForDMSidebar} from 'actions/user_actions.jsx';

function onChannelEnter(nextState, replace, callback) {
doChannelChange(nextState, replace, callback);
Expand All @@ -33,6 +34,10 @@ function doChannelChange(state, replace, callback) {
} else {
channel = ChannelStore.getByName(state.params.channel);

if (channel.type === Constants.DM_CHANNEL) {
loadNewDMIfNeeded(Utils.getUserIdFromChannelName(channel));
}

if (!channel) {
Client.joinChannelByName(
state.params.channel,
Expand Down Expand Up @@ -100,7 +105,6 @@ function preNeedsTeam(nextState, replace, callback) {

if (nextState.location.pathname.indexOf('/channels/') > -1 ||
nextState.location.pathname.indexOf('/pl/') > -1) {
loadProfilesAndTeamMembersForDMSidebar();
AsyncClient.getMyTeamsUnread();
AsyncClient.getMyChannelMembers();
}
Expand All @@ -115,6 +119,7 @@ function preNeedsTeam(nextState, replace, callback) {
});

loadStatusesForChannelAndSidebar();
loadProfilesAndTeamMembersForDMSidebar();

d1.resolve();
},
Expand Down

0 comments on commit 44b7fad

Please sign in to comment.