From ba6e370ca71abacaa30234cb164427d27c86df13 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Wed, 21 Dec 2016 16:35:01 -0500 Subject: PLT-5012 Combine updateLastViewedAt, setLastViewedAt and setActiveChannel into a single API (#4840) * Combine updateLastViewedAt, setLastViewedAt and setActiveChannel into a single API * Remove preference DB writes --- api/deprecated.go | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) (limited to 'api/deprecated.go') diff --git a/api/deprecated.go b/api/deprecated.go index 427abdedf..183552414 100644 --- a/api/deprecated.go +++ b/api/deprecated.go @@ -7,6 +7,7 @@ import ( "net/http" l4g "github.com/alecthomas/log4go" + "github.com/gorilla/mux" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" ) @@ -16,7 +17,15 @@ import ( func InitDeprecated() { l4g.Debug(utils.T("api.channel.init.debug")) - BaseRoutes.Channels.Handle("/more", ApiUserRequired(getMoreChannels)).Methods("GET") // SCHEDULED FOR DEPRECATION IN 3.7 + /* start - SCHEDULED FOR DEPRECATION IN 3.7 */ + BaseRoutes.Channels.Handle("/more", ApiUserRequired(getMoreChannels)).Methods("GET") + /* end - SCHEDULED FOR DEPRECATION IN 3.7 */ + + /* start - SCHEDULED FOR DEPRECATION IN 3.8 */ + BaseRoutes.NeedChannel.Handle("/update_last_viewed_at", ApiUserRequired(updateLastViewedAt)).Methods("POST") + BaseRoutes.NeedChannel.Handle("/set_last_viewed_at", ApiUserRequired(setLastViewedAt)).Methods("POST") + BaseRoutes.Users.Handle("/status/set_active_channel", ApiUserRequired(setActiveChannel)).Methods("POST") + /* end - SCHEDULED FOR DEPRECATION IN 3.8 */ } func getMoreChannels(c *Context, w http.ResponseWriter, r *http.Request) { @@ -37,3 +46,118 @@ func getMoreChannels(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(data.ToJson())) } } + +func updateLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + id := params["channel_id"] + + data := model.StringInterfaceFromJson(r.Body) + + var active bool + var ok bool + if active, ok = data["active"].(bool); !ok { + active = true + } + + doClearPush := false + if *utils.Cfg.EmailSettings.SendPushNotifications && !c.Session.IsMobileApp() && active { + if result := <-Srv.Store.User().GetUnreadCountForChannel(c.Session.UserId, id); result.Err != nil { + l4g.Error(utils.T("api.channel.update_last_viewed_at.get_unread_count_for_channel.error"), c.Session.UserId, id, result.Err.Error()) + } else { + if result.Data.(int64) > 0 { + doClearPush = true + } + } + } + + go func() { + if err := SetActiveChannel(c.Session.UserId, id); err != nil { + l4g.Error(err.Error()) + } + }() + + Srv.Store.Channel().UpdateLastViewedAt(id, c.Session.UserId) + + // Must be after update so that unread count is correct + if doClearPush { + go clearPushNotification(c.Session.UserId, id) + } + + chanPref := model.Preference{ + UserId: c.Session.UserId, + Category: c.TeamId, + Name: model.PREFERENCE_NAME_LAST_CHANNEL, + Value: id, + } + + teamPref := model.Preference{ + UserId: c.Session.UserId, + Category: model.PREFERENCE_CATEGORY_LAST, + Name: model.PREFERENCE_NAME_LAST_TEAM, + Value: c.TeamId, + } + + Srv.Store.Preference().Save(&model.Preferences{teamPref, chanPref}) + + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_VIEWED, c.TeamId, "", c.Session.UserId, nil) + message.Add("channel_id", id) + + go Publish(message) + + result := make(map[string]string) + result["id"] = id + w.Write([]byte(model.MapToJson(result))) +} + +func setLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + id := params["channel_id"] + + data := model.StringInterfaceFromJson(r.Body) + newLastViewedAt := int64(data["last_viewed_at"].(float64)) + + Srv.Store.Channel().SetLastViewedAt(id, c.Session.UserId, newLastViewedAt) + + chanPref := model.Preference{ + UserId: c.Session.UserId, + Category: c.TeamId, + Name: model.PREFERENCE_NAME_LAST_CHANNEL, + Value: id, + } + + teamPref := model.Preference{ + UserId: c.Session.UserId, + Category: model.PREFERENCE_CATEGORY_LAST, + Name: model.PREFERENCE_NAME_LAST_TEAM, + Value: c.TeamId, + } + + Srv.Store.Preference().Save(&model.Preferences{teamPref, chanPref}) + + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_VIEWED, c.TeamId, "", c.Session.UserId, nil) + message.Add("channel_id", id) + + go Publish(message) + + result := make(map[string]string) + result["id"] = id + w.Write([]byte(model.MapToJson(result))) +} + +func setActiveChannel(c *Context, w http.ResponseWriter, r *http.Request) { + data := model.MapFromJson(r.Body) + + var channelId string + var ok bool + if channelId, ok = data["channel_id"]; !ok || len(channelId) > 26 { + c.SetInvalidParam("setActiveChannel", "channel_id") + return + } + + if err := SetActiveChannel(c.Session.UserId, channelId); err != nil { + c.Err = err + return + } + + ReturnStatusOK(w) +} -- cgit v1.2.3-1-g7c22