summaryrefslogtreecommitdiffstats
path: root/api/post.go
diff options
context:
space:
mode:
authorCorey Hulen <corey@hulen.com>2015-12-11 06:40:37 -0800
committerCorey Hulen <corey@hulen.com>2015-12-11 06:40:37 -0800
commit412b45431ac913d13fb211b693e177bc92a09ecf (patch)
tree74f7e329090541f18ab30c7bf62d21987be92629 /api/post.go
parent607a8151de502bf45de131ef3632c741ad452333 (diff)
parentc55e7895d91c9d4cd31c7cefe81319d64d7fed16 (diff)
downloadchat-412b45431ac913d13fb211b693e177bc92a09ecf.tar.gz
chat-412b45431ac913d13fb211b693e177bc92a09ecf.tar.bz2
chat-412b45431ac913d13fb211b693e177bc92a09ecf.zip
Merge pull request #1652 from hmhealey/plt1319
PLT-1319 Fixed direct channels not being visible when no actual channel exists
Diffstat (limited to 'api/post.go')
-rw-r--r--api/post.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/api/post.go b/api/post.go
index 5fbacc906..97211b391 100644
--- a/api/post.go
+++ b/api/post.go
@@ -236,9 +236,68 @@ func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks boo
if triggerWebhooks {
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) {
go func() {
if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks {