github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/emoji.go (about)

     1  // Copyright (c) 2016-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  	"net/http"
    10  )
    11  
    12  const (
    13  	EMOJI_NAME_MAX_LENGTH = 64
    14  	EMOJI_SORT_BY_NAME    = "name"
    15  )
    16  
    17  type Emoji struct {
    18  	Id        string `json:"id"`
    19  	CreateAt  int64  `json:"create_at"`
    20  	UpdateAt  int64  `json:"update_at"`
    21  	DeleteAt  int64  `json:"delete_at"`
    22  	CreatorId string `json:"creator_id"`
    23  	Name      string `json:"name"`
    24  }
    25  
    26  func (emoji *Emoji) IsValid() *AppError {
    27  	if len(emoji.Id) != 26 {
    28  		return NewAppError("Emoji.IsValid", "model.emoji.id.app_error", nil, "", http.StatusBadRequest)
    29  	}
    30  
    31  	if emoji.CreateAt == 0 {
    32  		return NewAppError("Emoji.IsValid", "model.emoji.create_at.app_error", nil, "id="+emoji.Id, http.StatusBadRequest)
    33  	}
    34  
    35  	if emoji.UpdateAt == 0 {
    36  		return NewAppError("Emoji.IsValid", "model.emoji.update_at.app_error", nil, "id="+emoji.Id, http.StatusBadRequest)
    37  	}
    38  
    39  	if len(emoji.CreatorId) != 26 {
    40  		return NewAppError("Emoji.IsValid", "model.emoji.user_id.app_error", nil, "", http.StatusBadRequest)
    41  	}
    42  
    43  	if len(emoji.Name) == 0 || len(emoji.Name) > EMOJI_NAME_MAX_LENGTH || !IsValidAlphaNumHyphenUnderscore(emoji.Name, false) {
    44  		return NewAppError("Emoji.IsValid", "model.emoji.name.app_error", nil, "", http.StatusBadRequest)
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func (emoji *Emoji) PreSave() {
    51  	if emoji.Id == "" {
    52  		emoji.Id = NewId()
    53  	}
    54  
    55  	emoji.CreateAt = GetMillis()
    56  	emoji.UpdateAt = emoji.CreateAt
    57  }
    58  
    59  func (emoji *Emoji) PreUpdate() {
    60  	emoji.UpdateAt = GetMillis()
    61  }
    62  
    63  func (emoji *Emoji) ToJson() string {
    64  	b, _ := json.Marshal(emoji)
    65  	return string(b)
    66  }
    67  
    68  func EmojiFromJson(data io.Reader) *Emoji {
    69  	var emoji *Emoji
    70  	json.NewDecoder(data).Decode(&emoji)
    71  	return emoji
    72  }
    73  
    74  func EmojiListToJson(emojiList []*Emoji) string {
    75  	b, _ := json.Marshal(emojiList)
    76  	return string(b)
    77  }
    78  
    79  func EmojiListFromJson(data io.Reader) []*Emoji {
    80  	var emojiList []*Emoji
    81  	json.NewDecoder(data).Decode(&emojiList)
    82  	return emojiList
    83  }