github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/api4/status.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "net/http" 8 9 "github.com/mattermost/mattermost-server/model" 10 ) 11 12 func (api *API) InitStatus() { 13 api.BaseRoutes.User.Handle("/status", api.ApiSessionRequired(getUserStatus)).Methods("GET") 14 api.BaseRoutes.Users.Handle("/status/ids", api.ApiSessionRequired(getUserStatusesByIds)).Methods("POST") 15 api.BaseRoutes.User.Handle("/status", api.ApiSessionRequired(updateUserStatus)).Methods("PUT") 16 } 17 18 func getUserStatus(c *Context, w http.ResponseWriter, r *http.Request) { 19 c.RequireUserId() 20 if c.Err != nil { 21 return 22 } 23 24 // No permission check required 25 26 if statusMap, err := c.App.GetUserStatusesByIds([]string{c.Params.UserId}); err != nil { 27 c.Err = err 28 return 29 } else { 30 if len(statusMap) == 0 { 31 c.Err = model.NewAppError("UserStatus", "api.status.user_not_found.app_error", nil, "", http.StatusNotFound) 32 return 33 } else { 34 w.Write([]byte(statusMap[0].ToJson())) 35 } 36 } 37 } 38 39 func getUserStatusesByIds(c *Context, w http.ResponseWriter, r *http.Request) { 40 userIds := model.ArrayFromJson(r.Body) 41 42 if len(userIds) == 0 { 43 c.SetInvalidParam("user_ids") 44 return 45 } 46 47 // No permission check required 48 49 if statusMap, err := c.App.GetUserStatusesByIds(userIds); err != nil { 50 c.Err = err 51 return 52 } else { 53 w.Write([]byte(model.StatusListToJson(statusMap))) 54 } 55 } 56 57 func updateUserStatus(c *Context, w http.ResponseWriter, r *http.Request) { 58 c.RequireUserId() 59 if c.Err != nil { 60 return 61 } 62 63 status := model.StatusFromJson(r.Body) 64 if status == nil { 65 c.SetInvalidParam("status") 66 return 67 } 68 69 if !c.App.SessionHasPermissionToUser(c.Session, c.Params.UserId) { 70 c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS) 71 return 72 } 73 74 currentStatus, err := c.App.GetStatus(c.Params.UserId) 75 if err == nil && currentStatus.Status == model.STATUS_OUT_OF_OFFICE && status.Status != model.STATUS_OUT_OF_OFFICE { 76 c.App.DisableAutoResponder(c.Params.UserId, c.IsSystemAdmin()) 77 } 78 79 switch status.Status { 80 case "online": 81 c.App.SetStatusOnline(c.Params.UserId, "", true) 82 case "offline": 83 c.App.SetStatusOffline(c.Params.UserId, true) 84 case "away": 85 c.App.SetStatusAwayIfNeeded(c.Params.UserId, true) 86 case "dnd": 87 c.App.SetStatusDoNotDisturb(c.Params.UserId) 88 default: 89 c.SetInvalidParam("status") 90 return 91 } 92 93 getUserStatus(c, w, r) 94 }