github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/push_response.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  )
    10  
    11  const (
    12  	PUSH_STATUS           = "status"
    13  	PUSH_STATUS_OK        = "OK"
    14  	PUSH_STATUS_FAIL      = "FAIL"
    15  	PUSH_STATUS_REMOVE    = "REMOVE"
    16  	PUSH_STATUS_ERROR_MSG = "error"
    17  )
    18  
    19  type PushResponse map[string]string
    20  
    21  func NewOkPushResponse() PushResponse {
    22  	m := make(map[string]string)
    23  	m[PUSH_STATUS] = PUSH_STATUS_OK
    24  	return m
    25  }
    26  
    27  func NewRemovePushResponse() PushResponse {
    28  	m := make(map[string]string)
    29  	m[PUSH_STATUS] = PUSH_STATUS_REMOVE
    30  	return m
    31  }
    32  
    33  func NewErrorPushResponse(message string) PushResponse {
    34  	m := make(map[string]string)
    35  	m[PUSH_STATUS] = PUSH_STATUS_FAIL
    36  	m[PUSH_STATUS_ERROR_MSG] = message
    37  	return m
    38  }
    39  
    40  func (me *PushResponse) ToJson() string {
    41  	b, _ := json.Marshal(me)
    42  	return string(b)
    43  }
    44  
    45  func PushResponseFromJson(data io.Reader) PushResponse {
    46  	decoder := json.NewDecoder(data)
    47  
    48  	var objmap PushResponse
    49  	if err := decoder.Decode(&objmap); err != nil {
    50  		return make(map[string]string)
    51  	} else {
    52  		return objmap
    53  	}
    54  }