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 (me *msgProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse { splitMessage := strings.SplitN(message, " ", 2) parsedMessage := "" targetUsername := "" if len(splitMessage) > 1 { parsedMessage = strings.SplitN(message, " ", 2)[1] } targetUsername = strings.SplitN(message, " ", 2)[0] targetUsername = strings.TrimPrefix(targetUsername, "@") var userProfile *model.User if result := <-app.Srv.Store.User().GetByUsername(targetUsername); result.Err != nil { c.Err = result.Err return &model.CommandResponse{Text: c.T("api.command_msg.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } else { userProfile = result.Data.(*model.User) } if userProfile.Id == c.Session.UserId { return &model.CommandResponse{Text: c.T("api.command_msg.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } // Find the channel based on this user channelName := model.GetDMNameFromIds(c.Session.UserId, userProfile.Id) targetChannelId := "" if channel := <-app.Srv.Store.Channel().GetByName(c.TeamId, channelName); channel.Err != nil { if channel.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { if directChannel, err := app.CreateDirectChannel(c.Session.UserId, userProfile.Id); err != nil { c.Err = err return &model.CommandResponse{Text: c.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } else { targetChannelId = directChannel.Id } } else { c.Err = channel.Err return &model.CommandResponse{Text: c.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } } else { targetChannelId = channel.Data.(*model.Channel).Id } app.MakeDirectChannelVisible(targetChannelId) if len(parsedMessage) > 0 { post := &model.Post{} post.Message = parsedMessage post.ChannelId = targetChannelId post.UserId = c.Session.UserId if _, err := app.CreatePost(post, c.TeamId, true); err != nil { return &model.CommandResponse{Text: c.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } } return &model.CommandResponse{GotoLocation: c.GetTeamURL() + "/channels/" + channelName, Text: "", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} }