Remove MakeDirectChannelVisible and add client handling for showing DMs (#5430)

Этот коммит содержится в:
Joram Wilander
2017-02-15 18:54:24 -05:00
коммит произвёл GitHub
родитель 745e2f4923
Коммит db2966b7cb
8 изменённых файлов: 61 добавлений и 104 удалений

Просмотреть файл

@@ -80,7 +80,6 @@ func (me *msgProvider) DoCommand(c *Context, args *model.CommandArgs, message st
targetChannelId = channel.Data.(*model.Channel).Id
}
app.MakeDirectChannelVisible(targetChannelId)
if len(parsedMessage) > 0 {
post := &model.Post{}
post.Message = parsedMessage

Просмотреть файл

@@ -1058,42 +1058,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)
if err := app.MakeDirectChannelVisible(channel.Id); err != nil {
t.Fatal(err)
}
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 TestGetFlaggedPosts(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient

Просмотреть файл

@@ -14,61 +14,6 @@ import (
"github.com/mattermost/platform/utils"
)
func MakeDirectChannelVisible(channelId string) *model.AppError {
var members model.ChannelMembers
if result := <-Srv.Store.Channel().GetMembers(channelId, 0, 100); result.Err != nil {
return result.Err
} else {
members = *(result.Data.(*model.ChannelMembers))
}
if len(members) != 2 {
return model.NewLocAppError("MakeDirectChannelVisible", "api.post.make_direct_channel_visible.get_2_members.error", map[string]interface{}{"ChannelId": channelId}, "")
}
// 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 {
return saveResult.Err
} 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 {
return updateResult.Err
} else {
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCE_CHANGED, "", "", member.UserId, nil)
message.Add("preference", preference.ToJson())
go Publish(message)
}
}
}
}
return nil
}
func CreateDefaultChannels(teamId string) ([]*model.Channel, *model.AppError) {
townSquare := &model.Channel{DisplayName: utils.T("api.channel.create_default_channels.town_square"), Name: "town-square", Type: model.CHANNEL_OPEN, TeamId: teamId}

Просмотреть файл

@@ -166,14 +166,6 @@ func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *mo
}()
}
if channel.Type == model.CHANNEL_DIRECT {
go func() {
if err := MakeDirectChannelVisible(post.ChannelId); err != nil {
l4g.Error(err.Error())
}
}()
}
return nil
}

Просмотреть файл

@@ -8,6 +8,7 @@ import PostStore from 'stores/post_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';
@@ -22,6 +23,10 @@ export function handleNewPost(post, msg) {
websocketMessageProps = msg.data;
}
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,

Просмотреть файл

@@ -16,7 +16,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';
import {browserHistory} from 'react-router/es6';
export function switchFromLdapToEmail(email, password, token, ldapPassword, onSuccess, onError) {
@@ -225,6 +225,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();
@@ -240,6 +253,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,

Просмотреть файл

@@ -12,6 +12,7 @@ import * as GlobalActions from 'actions/global_actions.jsx';
import * as Websockets from 'actions/websocket_actions.jsx';
import BrowserStore from 'stores/browser_store.jsx';
import ChannelStore from 'stores/channel_store.jsx';
import UserStore from 'stores/user_store.jsx';
import * as I18n from 'i18n/i18n.jsx';
import * as AsyncClient from 'utils/async_client.jsx';
@@ -60,7 +61,9 @@ function preRenderSetup(callwhendone) {
$(window).on('beforeunload',
() => {
BrowserStore.setLastServerVersion('');
AsyncClient.viewChannel('', ChannelStore.getCurrentId() || '');
if (UserStore.getCurrentUser()) {
AsyncClient.viewChannel('', ChannelStore.getCurrentId() || '');
}
Websockets.close();
}
);

Просмотреть файл

@@ -13,6 +13,7 @@ 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';
@@ -20,7 +21,7 @@ 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);
@@ -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,
@@ -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();
const teams = TeamStore.getAll();
for (const id in teams) {
@@ -120,6 +124,7 @@ function preNeedsTeam(nextState, replace, callback) {
});
loadStatusesForChannelAndSidebar();
loadProfilesAndTeamMembersForDMSidebar();
d1.resolve();
},