github.com/mattermost/mattermost-server/v5@v5.39.3/api4/status.go (about) 1 // Copyright (c) 2015-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/v5/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 api.BaseRoutes.User.Handle("/status/custom", api.ApiSessionRequired(updateUserCustomStatus)).Methods("PUT") 17 api.BaseRoutes.User.Handle("/status/custom", api.ApiSessionRequired(removeUserCustomStatus)).Methods("DELETE") 18 19 // Both these handlers are for removing the recent custom status but the one with the POST method should be preferred 20 // as DELETE method doesn't support request body in the mobile app. 21 api.BaseRoutes.User.Handle("/status/custom/recent", api.ApiSessionRequired(removeUserRecentCustomStatus)).Methods("DELETE") 22 api.BaseRoutes.User.Handle("/status/custom/recent/delete", api.ApiSessionRequired(removeUserRecentCustomStatus)).Methods("POST") 23 } 24 25 func getUserStatus(c *Context, w http.ResponseWriter, r *http.Request) { 26 c.RequireUserId() 27 if c.Err != nil { 28 return 29 } 30 31 // No permission check required 32 33 statusMap, err := c.App.GetUserStatusesByIds([]string{c.Params.UserId}) 34 if err != nil { 35 c.Err = err 36 return 37 } 38 39 if len(statusMap) == 0 { 40 c.Err = model.NewAppError("UserStatus", "api.status.user_not_found.app_error", nil, "", http.StatusNotFound) 41 return 42 } 43 44 w.Write([]byte(statusMap[0].ToJson())) 45 } 46 47 func getUserStatusesByIds(c *Context, w http.ResponseWriter, r *http.Request) { 48 userIds := model.ArrayFromJson(r.Body) 49 50 if len(userIds) == 0 { 51 c.SetInvalidParam("user_ids") 52 return 53 } 54 55 for _, userId := range userIds { 56 if len(userId) != 26 { 57 c.SetInvalidParam("user_ids") 58 return 59 } 60 } 61 62 // No permission check required 63 64 statusMap, err := c.App.GetUserStatusesByIds(userIds) 65 if err != nil { 66 c.Err = err 67 return 68 } 69 70 w.Write([]byte(model.StatusListToJson(statusMap))) 71 } 72 73 func updateUserStatus(c *Context, w http.ResponseWriter, r *http.Request) { 74 c.RequireUserId() 75 if c.Err != nil { 76 return 77 } 78 79 status := model.StatusFromJson(r.Body) 80 if status == nil { 81 c.SetInvalidParam("status") 82 return 83 } 84 85 // The user being updated in the payload must be the same one as indicated in the URL. 86 if status.UserId != c.Params.UserId { 87 c.SetInvalidParam("user_id") 88 return 89 } 90 91 if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), c.Params.UserId) { 92 c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS) 93 return 94 } 95 96 currentStatus, err := c.App.GetStatus(c.Params.UserId) 97 if err == nil && currentStatus.Status == model.STATUS_OUT_OF_OFFICE && status.Status != model.STATUS_OUT_OF_OFFICE { 98 c.App.DisableAutoResponder(c.Params.UserId, c.IsSystemAdmin()) 99 } 100 101 switch status.Status { 102 case "online": 103 c.App.SetStatusOnline(c.Params.UserId, true) 104 case "offline": 105 c.App.SetStatusOffline(c.Params.UserId, true) 106 case "away": 107 c.App.SetStatusAwayIfNeeded(c.Params.UserId, true) 108 case "dnd": 109 if c.App.Config().FeatureFlags.TimedDND { 110 c.App.SetStatusDoNotDisturbTimed(c.Params.UserId, status.DNDEndTime) 111 } else { 112 c.App.SetStatusDoNotDisturb(c.Params.UserId) 113 } 114 default: 115 c.SetInvalidParam("status") 116 return 117 } 118 119 getUserStatus(c, w, r) 120 } 121 122 func updateUserCustomStatus(c *Context, w http.ResponseWriter, r *http.Request) { 123 c.RequireUserId() 124 if c.Err != nil { 125 return 126 } 127 128 if !*c.App.Config().TeamSettings.EnableCustomUserStatuses { 129 c.Err = model.NewAppError("updateUserCustomStatus", "api.custom_status.disabled", nil, "", http.StatusNotImplemented) 130 return 131 } 132 133 customStatus := model.CustomStatusFromJson(r.Body) 134 if customStatus == nil || (customStatus.Emoji == "" && customStatus.Text == "") || !customStatus.AreDurationAndExpirationTimeValid() { 135 c.SetInvalidParam("custom_status") 136 return 137 } 138 139 if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), c.Params.UserId) { 140 c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS) 141 return 142 } 143 144 customStatus.PreSave() 145 err := c.App.SetCustomStatus(c.Params.UserId, customStatus) 146 if err != nil { 147 c.Err = err 148 return 149 } 150 151 ReturnStatusOK(w) 152 } 153 154 func removeUserCustomStatus(c *Context, w http.ResponseWriter, r *http.Request) { 155 c.RequireUserId() 156 if c.Err != nil { 157 return 158 } 159 160 if !*c.App.Config().TeamSettings.EnableCustomUserStatuses { 161 c.Err = model.NewAppError("removeUserCustomStatus", "api.custom_status.disabled", nil, "", http.StatusNotImplemented) 162 return 163 } 164 165 if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), c.Params.UserId) { 166 c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS) 167 return 168 } 169 170 if err := c.App.RemoveCustomStatus(c.Params.UserId); err != nil { 171 c.Err = err 172 return 173 } 174 175 ReturnStatusOK(w) 176 } 177 178 func removeUserRecentCustomStatus(c *Context, w http.ResponseWriter, r *http.Request) { 179 c.RequireUserId() 180 if c.Err != nil { 181 return 182 } 183 184 if !*c.App.Config().TeamSettings.EnableCustomUserStatuses { 185 c.Err = model.NewAppError("removeUserRecentCustomStatus", "api.custom_status.disabled", nil, "", http.StatusNotImplemented) 186 return 187 } 188 189 recentCustomStatus := model.CustomStatusFromJson(r.Body) 190 if recentCustomStatus == nil { 191 c.SetInvalidParam("recent_custom_status") 192 return 193 } 194 195 if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), c.Params.UserId) { 196 c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS) 197 return 198 } 199 200 if err := c.App.RemoveRecentCustomStatus(c.Params.UserId, recentCustomStatus); err != nil { 201 c.Err = err 202 return 203 } 204 205 ReturnStatusOK(w) 206 }