github.com/turgay/mattermost-server@v5.3.2-0.20181002173352-2945e8a2b0ce+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  	statusMap, err := c.App.GetUserStatusesByIds([]string{c.Params.UserId})
    27  	if err != nil {
    28  		c.Err = err
    29  		return
    30  	}
    31  
    32  	if len(statusMap) == 0 {
    33  		c.Err = model.NewAppError("UserStatus", "api.status.user_not_found.app_error", nil, "", http.StatusNotFound)
    34  		return
    35  	}
    36  
    37  	w.Write([]byte(statusMap[0].ToJson()))
    38  }
    39  
    40  func getUserStatusesByIds(c *Context, w http.ResponseWriter, r *http.Request) {
    41  	userIds := model.ArrayFromJson(r.Body)
    42  
    43  	if len(userIds) == 0 {
    44  		c.SetInvalidParam("user_ids")
    45  		return
    46  	}
    47  
    48  	// No permission check required
    49  
    50  	statusMap, err := c.App.GetUserStatusesByIds(userIds)
    51  	if err != nil {
    52  		c.Err = err
    53  		return
    54  	}
    55  
    56  	w.Write([]byte(model.StatusListToJson(statusMap)))
    57  }
    58  
    59  func updateUserStatus(c *Context, w http.ResponseWriter, r *http.Request) {
    60  	c.RequireUserId()
    61  	if c.Err != nil {
    62  		return
    63  	}
    64  
    65  	status := model.StatusFromJson(r.Body)
    66  	if status == nil {
    67  		c.SetInvalidParam("status")
    68  		return
    69  	}
    70  
    71  	// The user being updated in the payload must be the same one as indicated in the URL.
    72  	if status.UserId != c.Params.UserId {
    73  		c.SetInvalidParam("user_id")
    74  		return
    75  	}
    76  
    77  	if !c.App.SessionHasPermissionToUser(c.Session, c.Params.UserId) {
    78  		c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS)
    79  		return
    80  	}
    81  
    82  	currentStatus, err := c.App.GetStatus(c.Params.UserId)
    83  	if err == nil && currentStatus.Status == model.STATUS_OUT_OF_OFFICE && status.Status != model.STATUS_OUT_OF_OFFICE {
    84  		c.App.DisableAutoResponder(c.Params.UserId, c.IsSystemAdmin())
    85  	}
    86  
    87  	switch status.Status {
    88  	case "online":
    89  		c.App.SetStatusOnline(c.Params.UserId, true)
    90  	case "offline":
    91  		c.App.SetStatusOffline(c.Params.UserId, true)
    92  	case "away":
    93  		c.App.SetStatusAwayIfNeeded(c.Params.UserId, true)
    94  	case "dnd":
    95  		c.App.SetStatusDoNotDisturb(c.Params.UserId)
    96  	default:
    97  		c.SetInvalidParam("status")
    98  		return
    99  	}
   100  
   101  	getUserStatus(c, w, r)
   102  }