github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+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 inSystemEmoji(emojiName string) bool { 27 _, ok := SystemEmojis[emojiName] 28 return ok 29 } 30 31 func (emoji *Emoji) IsValid() *AppError { 32 if len(emoji.Id) != 26 { 33 return NewAppError("Emoji.IsValid", "model.emoji.id.app_error", nil, "", http.StatusBadRequest) 34 } 35 36 if emoji.CreateAt == 0 { 37 return NewAppError("Emoji.IsValid", "model.emoji.create_at.app_error", nil, "id="+emoji.Id, http.StatusBadRequest) 38 } 39 40 if emoji.UpdateAt == 0 { 41 return NewAppError("Emoji.IsValid", "model.emoji.update_at.app_error", nil, "id="+emoji.Id, http.StatusBadRequest) 42 } 43 44 if len(emoji.CreatorId) != 26 { 45 return NewAppError("Emoji.IsValid", "model.emoji.user_id.app_error", nil, "", http.StatusBadRequest) 46 } 47 48 if len(emoji.Name) == 0 || len(emoji.Name) > EMOJI_NAME_MAX_LENGTH || !IsValidAlphaNumHyphenUnderscore(emoji.Name, false) || inSystemEmoji(emoji.Name) { 49 return NewAppError("Emoji.IsValid", "model.emoji.name.app_error", nil, "", http.StatusBadRequest) 50 } 51 52 return nil 53 } 54 55 func (emoji *Emoji) PreSave() { 56 if emoji.Id == "" { 57 emoji.Id = NewId() 58 } 59 60 emoji.CreateAt = GetMillis() 61 emoji.UpdateAt = emoji.CreateAt 62 } 63 64 func (emoji *Emoji) ToJson() string { 65 b, _ := json.Marshal(emoji) 66 return string(b) 67 } 68 69 func EmojiFromJson(data io.Reader) *Emoji { 70 var emoji *Emoji 71 json.NewDecoder(data).Decode(&emoji) 72 return emoji 73 } 74 75 func EmojiListToJson(emojiList []*Emoji) string { 76 b, _ := json.Marshal(emojiList) 77 return string(b) 78 } 79 80 func EmojiListFromJson(data io.Reader) []*Emoji { 81 var emojiList []*Emoji 82 json.NewDecoder(data).Decode(&emojiList) 83 return emojiList 84 }