Merge pull request #1652 from hmhealey/plt1319
PLT-1319 Fixed direct channels not being visible when no actual channel exists
Этот коммит содержится в:
59
api/post.go
59
api/post.go
@@ -236,9 +236,68 @@ func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks boo
|
|||||||
if triggerWebhooks {
|
if triggerWebhooks {
|
||||||
handleWebhookEventsAndForget(c, post, team, channel, user)
|
handleWebhookEventsAndForget(c, post, team, channel, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if channel.Type == model.CHANNEL_DIRECT {
|
||||||
|
go makeDirectChannelVisible(c.Session.TeamId, post.ChannelId)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeDirectChannelVisible(teamId string, channelId string) {
|
||||||
|
var members []model.ChannelMember
|
||||||
|
if result := <-Srv.Store.Channel().GetMembers(channelId); result.Err != nil {
|
||||||
|
l4g.Error("Failed to get channel members channel_id=%v err=%v", channelId, result.Err.Message)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
members = result.Data.([]model.ChannelMember)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(members) != 2 {
|
||||||
|
l4g.Error("Failed to get 2 members for a direct channel channel_id=%v", 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("Failed to save direct channel preference user_id=%v other_user_id=%v err=%v", member.UserId, otherUserId, saveResult.Err.Message)
|
||||||
|
} else {
|
||||||
|
message := model.NewMessage(teamId, channelId, member.UserId, model.ACTION_PREFERENCE_CHANGED)
|
||||||
|
message.Add("preference", preference.ToJson())
|
||||||
|
|
||||||
|
PublishAndForget(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("Failed to update direct channel preference user_id=%v other_user_id=%v err=%v", member.UserId, otherUserId, updateResult.Err.Message)
|
||||||
|
} else {
|
||||||
|
message := model.NewMessage(teamId, channelId, member.UserId, model.ACTION_PREFERENCE_CHANGED)
|
||||||
|
message.Add("preference", preference.ToJson())
|
||||||
|
|
||||||
|
PublishAndForget(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func handleWebhookEventsAndForget(c *Context, post *model.Post, team *model.Team, channel *model.Channel, user *model.User) {
|
func handleWebhookEventsAndForget(c *Context, post *model.Post, team *model.Team, channel *model.Channel, user *model.User) {
|
||||||
go func() {
|
go func() {
|
||||||
if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks {
|
if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks {
|
||||||
|
|||||||
@@ -805,3 +805,51 @@ func TestFuzzyPosts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMakeDirectChannelVisible(t *testing.T) {
|
||||||
|
Setup()
|
||||||
|
|
||||||
|
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||||
|
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||||
|
|
||||||
|
user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||||
|
user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||||
|
store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||||
|
|
||||||
|
user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||||
|
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
|
||||||
|
store.Must(Srv.Store.User().VerifyEmail(user2.Id))
|
||||||
|
|
||||||
|
// user2 will be created with prefs created to show user1 in the sidebar so set that to false to get rid of it
|
||||||
|
Client.LoginByEmail(team.Name, user2.Email, "pwd")
|
||||||
|
|
||||||
|
preferences := &model.Preferences{
|
||||||
|
{
|
||||||
|
UserId: user2.Id,
|
||||||
|
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
|
||||||
|
Name: user1.Id,
|
||||||
|
Value: "false",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
Client.Must(Client.SetPreferences(preferences))
|
||||||
|
|
||||||
|
Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||||
|
|
||||||
|
channel := Client.Must(Client.CreateDirectChannel(map[string]string{"user_id": user2.Id})).Data.(*model.Channel)
|
||||||
|
|
||||||
|
makeDirectChannelVisible(team.Id, 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")
|
||||||
|
}
|
||||||
|
|
||||||
|
Client.LoginByEmail(team.Name, user2.Email, "pwd")
|
||||||
|
|
||||||
|
if result, err := Client.GetPreference(model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, user1.Id); err != nil {
|
||||||
|
t.Fatal("Errored trying to set direct channel to be visible for user2")
|
||||||
|
} else if pref := result.Data.(*model.Preference); pref.Value != "true" {
|
||||||
|
t.Fatal("Failed to set direct channel to be visible for user2")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -95,9 +95,11 @@ func ShouldSendEvent(webCon *WebConn, msg *model.Message) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Don't share a user's view events with other users
|
// Don't share a user's view or preference events with other users
|
||||||
if msg.Action == model.ACTION_CHANNEL_VIEWED {
|
if msg.Action == model.ACTION_CHANNEL_VIEWED {
|
||||||
return false
|
return false
|
||||||
|
} else if msg.Action == model.ACTION_PREFERENCE_CHANGED {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only report events to a user who is the subject of the event, or is in the channel of the event
|
// Only report events to a user who is the subject of the event, or is in the channel of the event
|
||||||
|
|||||||
@@ -9,14 +9,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ACTION_TYPING = "typing"
|
ACTION_TYPING = "typing"
|
||||||
ACTION_POSTED = "posted"
|
ACTION_POSTED = "posted"
|
||||||
ACTION_POST_EDITED = "post_edited"
|
ACTION_POST_EDITED = "post_edited"
|
||||||
ACTION_POST_DELETED = "post_deleted"
|
ACTION_POST_DELETED = "post_deleted"
|
||||||
ACTION_CHANNEL_VIEWED = "channel_viewed"
|
ACTION_CHANNEL_VIEWED = "channel_viewed"
|
||||||
ACTION_NEW_USER = "new_user"
|
ACTION_NEW_USER = "new_user"
|
||||||
ACTION_USER_ADDED = "user_added"
|
ACTION_USER_ADDED = "user_added"
|
||||||
ACTION_USER_REMOVED = "user_removed"
|
ACTION_USER_REMOVED = "user_removed"
|
||||||
|
ACTION_PREFERENCE_CHANGED = "preference_changed"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
|
|||||||
@@ -71,49 +71,47 @@ export default class Sidebar extends React.Component {
|
|||||||
getStateFromStores() {
|
getStateFromStores() {
|
||||||
const members = ChannelStore.getAllMembers();
|
const members = ChannelStore.getAllMembers();
|
||||||
const currentChannelId = ChannelStore.getCurrentId();
|
const currentChannelId = ChannelStore.getCurrentId();
|
||||||
|
const currentUserId = UserStore.getCurrentId();
|
||||||
|
|
||||||
const channels = Object.assign([], ChannelStore.getAll());
|
const channels = Object.assign([], ChannelStore.getAll());
|
||||||
channels.sort((a, b) => a.display_name.localeCompare(b.display_name));
|
channels.sort((a, b) => a.display_name.localeCompare(b.display_name));
|
||||||
|
|
||||||
const publicChannels = channels.filter((channel) => channel.type === Constants.OPEN_CHANNEL);
|
const publicChannels = channels.filter((channel) => channel.type === Constants.OPEN_CHANNEL);
|
||||||
const privateChannels = channels.filter((channel) => channel.type === Constants.PRIVATE_CHANNEL);
|
const privateChannels = channels.filter((channel) => channel.type === Constants.PRIVATE_CHANNEL);
|
||||||
const directChannels = channels.filter((channel) => channel.type === Constants.DM_CHANNEL);
|
|
||||||
|
|
||||||
const preferences = PreferenceStore.getPreferences(Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW);
|
const preferences = PreferenceStore.getPreferences(Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW);
|
||||||
|
|
||||||
var visibleDirectChannels = [];
|
const directChannels = [];
|
||||||
for (var i = 0; i < directChannels.length; i++) {
|
for (const preference of preferences) {
|
||||||
const dm = directChannels[i];
|
if (preference.value !== 'true') {
|
||||||
const teammate = Utils.getDirectTeammate(dm.id);
|
|
||||||
if (!teammate) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const member = members[dm.id];
|
const teammateId = preference.name;
|
||||||
const msgCount = dm.total_msg_count - member.msg_count;
|
|
||||||
|
|
||||||
// always show a channel if either it is the current one or if it is unread, but it is not currently being left
|
let directChannel = channels.find(Utils.isDirectChannelForUser.bind(null, teammateId));
|
||||||
const forceShow = (currentChannelId === dm.id || msgCount > 0) && !this.isLeaving.get(dm.id);
|
|
||||||
const preferenceShow = preferences.some((preference) => (preference.name === teammate.id && preference.value !== 'false'));
|
|
||||||
|
|
||||||
if (preferenceShow || forceShow) {
|
// a direct channel doesn't exist yet so create a fake one
|
||||||
dm.display_name = Utils.displayUsername(teammate.id);
|
if (!directChannel) {
|
||||||
dm.teammate_id = teammate.id;
|
directChannel = {
|
||||||
dm.status = UserStore.getStatus(teammate.id);
|
name: Utils.getDirectChannelName(currentUserId, teammateId),
|
||||||
|
last_post_at: 0,
|
||||||
visibleDirectChannels.push(dm);
|
total_msg_count: 0,
|
||||||
|
type: Constants.DM_CHANNEL,
|
||||||
if (forceShow && !preferenceShow) {
|
fake: true
|
||||||
// make sure that unread direct channels are visible
|
};
|
||||||
const preference = PreferenceStore.setPreference(Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, teammate.id, 'true');
|
|
||||||
AsyncClient.savePreferences([preference]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
directChannel.display_name = Utils.displayUsername(teammateId);
|
||||||
|
directChannel.teammate_id = teammateId;
|
||||||
|
directChannel.status = UserStore.getStatus(teammateId);
|
||||||
|
|
||||||
|
directChannels.push(directChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
const hiddenDirectChannelCount = UserStore.getActiveOnlyProfileList(true).length - visibleDirectChannels.length;
|
directChannels.sort(this.sortChannelsByDisplayName);
|
||||||
|
|
||||||
visibleDirectChannels.sort(this.sortChannelsByDisplayName);
|
const hiddenDirectChannelCount = UserStore.getActiveOnlyProfileList(true).length - directChannels.length;
|
||||||
|
|
||||||
const tutorialPref = PreferenceStore.getPreference(Preferences.TUTORIAL_STEP, UserStore.getCurrentId(), {value: '999'});
|
const tutorialPref = PreferenceStore.getPreference(Preferences.TUTORIAL_STEP, UserStore.getCurrentId(), {value: '999'});
|
||||||
|
|
||||||
@@ -122,7 +120,7 @@ export default class Sidebar extends React.Component {
|
|||||||
members,
|
members,
|
||||||
publicChannels,
|
publicChannels,
|
||||||
privateChannels,
|
privateChannels,
|
||||||
visibleDirectChannels,
|
directChannels,
|
||||||
hiddenDirectChannelCount,
|
hiddenDirectChannelCount,
|
||||||
unreadCounts: JSON.parse(JSON.stringify(ChannelStore.getUnreadCounts())),
|
unreadCounts: JSON.parse(JSON.stringify(ChannelStore.getUnreadCounts())),
|
||||||
showTutorialTip: parseInt(tutorialPref.value, 10) === TutorialSteps.CHANNEL_POPOVER
|
showTutorialTip: parseInt(tutorialPref.value, 10) === TutorialSteps.CHANNEL_POPOVER
|
||||||
@@ -484,7 +482,7 @@ export default class Sidebar extends React.Component {
|
|||||||
|
|
||||||
const privateChannelItems = this.state.privateChannels.map(this.createChannelElement);
|
const privateChannelItems = this.state.privateChannels.map(this.createChannelElement);
|
||||||
|
|
||||||
const directMessageItems = this.state.visibleDirectChannels.map((channel, index, arr) => {
|
const directMessageItems = this.state.directChannels.map((channel, index, arr) => {
|
||||||
return this.createChannelElement(channel, index, arr, this.handleLeaveDirectChannel);
|
return this.createChannelElement(channel, index, arr, this.handleLeaveDirectChannel);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -173,3 +173,10 @@ export function emitClearSuggestions(suggestionId) {
|
|||||||
id: suggestionId
|
id: suggestionId
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function emitPreferenceChangedEvent(preference) {
|
||||||
|
AppDispatcher.handleServerAction({
|
||||||
|
type: Constants.ActionTypes.RECIEVED_PREFERENCE,
|
||||||
|
preference
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -90,8 +90,8 @@ class PreferenceStoreClass extends EventEmitter {
|
|||||||
return preference;
|
return preference;
|
||||||
}
|
}
|
||||||
|
|
||||||
emitChange(preferences) {
|
emitChange() {
|
||||||
this.emit(CHANGE_EVENT, preferences);
|
this.emit(CHANGE_EVENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
addChangeListener(callback) {
|
addChangeListener(callback) {
|
||||||
@@ -106,6 +106,12 @@ class PreferenceStoreClass extends EventEmitter {
|
|||||||
const action = payload.action;
|
const action = payload.action;
|
||||||
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
case ActionTypes.RECIEVED_PREFERENCE: {
|
||||||
|
const preference = action.preference;
|
||||||
|
this.setPreference(preference.category, preference.name, preference.value);
|
||||||
|
this.emitChange();
|
||||||
|
break;
|
||||||
|
}
|
||||||
case ActionTypes.RECIEVED_PREFERENCES: {
|
case ActionTypes.RECIEVED_PREFERENCES: {
|
||||||
const preferences = this.getAllPreferences();
|
const preferences = this.getAllPreferences();
|
||||||
|
|
||||||
@@ -114,7 +120,7 @@ class PreferenceStoreClass extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.setAllPreferences(preferences);
|
this.setAllPreferences(preferences);
|
||||||
this.emitChange(preferences);
|
this.emitChange();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,6 +136,10 @@ class SocketStoreClass extends EventEmitter {
|
|||||||
handleChannelViewedEvent(msg);
|
handleChannelViewedEvent(msg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case SocketEvents.PREFERENCE_CHANGED:
|
||||||
|
handlePreferenceChangedEvent(msg);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -281,6 +285,11 @@ function handleChannelViewedEvent(msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handlePreferenceChangedEvent(msg) {
|
||||||
|
const preference = JSON.parse(msg.props.preference);
|
||||||
|
EventHelpers.emitPreferenceChangedEvent(preference);
|
||||||
|
}
|
||||||
|
|
||||||
var SocketStore = new SocketStoreClass();
|
var SocketStore = new SocketStoreClass();
|
||||||
|
|
||||||
/*SocketStore.dispatchToken = AppDispatcher.register((payload) => {
|
/*SocketStore.dispatchToken = AppDispatcher.register((payload) => {
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ export default {
|
|||||||
RECIEVED_AUDITS: null,
|
RECIEVED_AUDITS: null,
|
||||||
RECIEVED_TEAMS: null,
|
RECIEVED_TEAMS: null,
|
||||||
RECIEVED_STATUSES: null,
|
RECIEVED_STATUSES: null,
|
||||||
|
RECIEVED_PREFERENCE: null,
|
||||||
RECIEVED_PREFERENCES: null,
|
RECIEVED_PREFERENCES: null,
|
||||||
|
|
||||||
RECIEVED_MSG: null,
|
RECIEVED_MSG: null,
|
||||||
@@ -74,7 +75,8 @@ export default {
|
|||||||
NEW_USER: 'new_user',
|
NEW_USER: 'new_user',
|
||||||
USER_ADDED: 'user_added',
|
USER_ADDED: 'user_added',
|
||||||
USER_REMOVED: 'user_removed',
|
USER_REMOVED: 'user_removed',
|
||||||
TYPING: 'typing'
|
TYPING: 'typing',
|
||||||
|
PREFERENCE_CHANGED: 'preference_changed'
|
||||||
},
|
},
|
||||||
|
|
||||||
//SPECIAL_MENTIONS: ['all', 'channel'],
|
//SPECIAL_MENTIONS: ['all', 'channel'],
|
||||||
|
|||||||
@@ -1138,6 +1138,11 @@ export function getUserIdFromChannelName(channel) {
|
|||||||
return otherUserId;
|
return otherUserId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns true if the given channel is a direct channel between the current user and the given one
|
||||||
|
export function isDirectChannelForUser(otherUserId, channel) {
|
||||||
|
return channel.type === Constants.DM_CHANNEL && getUserIdFromChannelName(channel) === otherUserId;
|
||||||
|
}
|
||||||
|
|
||||||
export function importSlack(file, success, error) {
|
export function importSlack(file, success, error) {
|
||||||
var formData = new FormData();
|
var formData = new FormData();
|
||||||
formData.append('file', file, file.name);
|
formData.append('file', file, file.name);
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user