From e6d15f77e8fa4ec27e104297667b28dc5f8517ca Mon Sep 17 00:00:00 2001 From: enahum Date: Wed, 24 Aug 2016 18:10:49 -0300 Subject: PLT-3942 Add real-time updates for center channel profile picture popover (#3871) * PLT-3942 Add real-time updates for center channel profile picture pop-over * Send user data with the websocket event to prevent further requests * Add helper for profile sanitation --- api/user.go | 65 ++++++++++++------------------------ model/user.go | 16 +++++++++ model/websocket_message.go | 1 + webapp/actions/websocket_actions.jsx | 11 ++++++ webapp/utils/constants.jsx | 1 + 5 files changed, 50 insertions(+), 44 deletions(-) diff --git a/api/user.go b/api/user.go index 6b9aa4f64..d7eab3d46 100644 --- a/api/user.go +++ b/api/user.go @@ -898,17 +898,7 @@ func getInitialLoad(c *Context, w http.ResponseWriter, r *http.Request) { profiles := dp.Data.(map[string]*model.User) for k, p := range profiles { - options := utils.Cfg.GetSanitizeOptions() - options["passwordupdate"] = false - - if c.IsSystemAdmin() { - options["fullname"] = true - options["email"] = true - } else { - p.ClearNonProfileFields() - } - - p.Sanitize(options) + p.SanitizeProfile(c.IsSystemAdmin(), false, true, true) profiles[k] = p } @@ -984,17 +974,7 @@ func getProfilesForDirectMessageList(c *Context, w http.ResponseWriter, r *http. profiles := result.Data.(map[string]*model.User) for k, p := range profiles { - options := utils.Cfg.GetSanitizeOptions() - options["passwordupdate"] = false - - if c.IsSystemAdmin() { - options["fullname"] = true - options["email"] = true - } else { - p.ClearNonProfileFields() - } - - p.Sanitize(options) + p.SanitizeProfile(c.IsSystemAdmin(), false, false, false) profiles[k] = p } @@ -1024,17 +1004,7 @@ func getProfiles(c *Context, w http.ResponseWriter, r *http.Request) { profiles := result.Data.(map[string]*model.User) for k, p := range profiles { - options := utils.Cfg.GetSanitizeOptions() - options["passwordupdate"] = false - - if c.IsSystemAdmin() { - options["fullname"] = true - options["email"] = true - } else { - p.ClearNonProfileFields() - } - - p.Sanitize(options) + p.SanitizeProfile(c.IsSystemAdmin(), false, true, true) profiles[k] = p } @@ -1056,17 +1026,7 @@ func getDirectProfiles(c *Context, w http.ResponseWriter, r *http.Request) { profiles := result.Data.(map[string]*model.User) for k, p := range profiles { - options := utils.Cfg.GetSanitizeOptions() - options["passwordupdate"] = false - - if c.IsSystemAdmin() { - options["fullname"] = true - options["email"] = true - } else { - p.ClearNonProfileFields() - } - - p.Sanitize(options) + p.SanitizeProfile(c.IsSystemAdmin(), false, true, true) profiles[k] = p } @@ -1312,6 +1272,16 @@ func uploadProfileImage(c *Context, w http.ResponseWriter, r *http.Request) { Srv.Store.User().UpdateLastPictureUpdate(c.Session.UserId) + if result := <-Srv.Store.User().Get(c.Session.UserId); result.Err != nil { + l4g.Error(utils.T("api.user.get_me.getting.error"), c.Session.UserId) + } else { + user := result.Data.(*model.User) + user.SanitizeProfile(c.IsSystemAdmin(), false, true, true) + message := model.NewWebSocketEvent("", "", c.Session.UserId, model.WEBSOCKET_EVENT_USER_UPDATED) + message.Add("user", user) + go Publish(message) + } + c.LogAudit("") // write something as the response since jQuery expects a json response @@ -1355,6 +1325,13 @@ func updateUser(c *Context, w http.ResponseWriter, r *http.Request) { go sendEmailChangeUsername(c, rusers[1].Username, rusers[0].Username, rusers[0].Email, c.GetSiteURL()) } + updatedUser := rusers[0] + updatedUser.SanitizeProfile(c.IsSystemAdmin(), false, true, true) + + message := model.NewWebSocketEvent("", "", user.Id, model.WEBSOCKET_EVENT_USER_UPDATED) + message.Add("user", updatedUser) + go Publish(message) + rusers[0].Password = "" rusers[0].AuthData = new(string) *rusers[0].AuthData = "" diff --git a/model/user.go b/model/user.go index 3da862b6b..f857d5ef4 100644 --- a/model/user.go +++ b/model/user.go @@ -251,6 +251,22 @@ func (u *User) ClearNonProfileFields() { u.FailedAttempts = 0 } +func (u *User) SanitizeProfile(isSystemAdmin, pwdupdate, fullname, email bool) { + options := map[string]bool{} + options["passwordupdate"] = pwdupdate + + if isSystemAdmin { + options["fullname"] = true + options["email"] = true + } else { + options["fullname"] = fullname + options["email"] = email + u.ClearNonProfileFields() + } + + u.Sanitize(options) +} + func (u *User) MakeNonNil() { if u.Props == nil { u.Props = make(map[string]string) diff --git a/model/websocket_message.go b/model/websocket_message.go index ae9a140c3..0ad455997 100644 --- a/model/websocket_message.go +++ b/model/websocket_message.go @@ -19,6 +19,7 @@ const ( WEBSOCKET_EVENT_NEW_USER = "new_user" WEBSOCKET_EVENT_LEAVE_TEAM = "leave_team" WEBSOCKET_EVENT_USER_ADDED = "user_added" + WEBSOCKET_EVENT_USER_UPDATED = "user_updated" WEBSOCKET_EVENT_USER_REMOVED = "user_removed" WEBSOCKET_EVENT_PREFERENCE_CHANGED = "preference_changed" WEBSOCKET_EVENT_EPHEMERAL_MESSAGE = "ephemeral_message" diff --git a/webapp/actions/websocket_actions.jsx b/webapp/actions/websocket_actions.jsx index fb0fbf513..2abc6ebd4 100644 --- a/webapp/actions/websocket_actions.jsx +++ b/webapp/actions/websocket_actions.jsx @@ -128,6 +128,10 @@ function handleEvent(msg) { handleUserRemovedEvent(msg); break; + case SocketEvents.USER_UPDATED: + handleUserUpdatedEvent(msg); + break; + case SocketEvents.CHANNEL_VIEWED: handleChannelViewedEvent(msg); break; @@ -241,6 +245,13 @@ function handleUserRemovedEvent(msg) { } } +function handleUserUpdatedEvent(msg) { + if (UserStore.getCurrentId() !== msg.user_id) { + UserStore.saveProfile(msg.data.user); + UserStore.emitChange(msg.user_id); + } +} + function handleChannelViewedEvent(msg) { // Useful for when multiple devices have the app open to different channels if (TeamStore.getCurrentId() === msg.team_id && diff --git a/webapp/utils/constants.jsx b/webapp/utils/constants.jsx index 8a31f6bfa..60cc82ff6 100644 --- a/webapp/utils/constants.jsx +++ b/webapp/utils/constants.jsx @@ -194,6 +194,7 @@ export const Constants = { LEAVE_TEAM: 'leave_team', USER_ADDED: 'user_added', USER_REMOVED: 'user_removed', + USER_UPDATED: 'user_updated', TYPING: 'typing', PREFERENCE_CHANGED: 'preference_changed', EPHEMERAL_MESSAGE: 'ephemeral_message', -- cgit v1.2.3-1-g7c22