github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/reaction.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 "regexp" 11 ) 12 13 type Reaction struct { 14 UserId string `json:"user_id"` 15 PostId string `json:"post_id"` 16 EmojiName string `json:"emoji_name"` 17 CreateAt int64 `json:"create_at"` 18 } 19 20 func (o *Reaction) ToJson() string { 21 b, _ := json.Marshal(o) 22 return string(b) 23 } 24 25 func ReactionFromJson(data io.Reader) *Reaction { 26 var o Reaction 27 28 if err := json.NewDecoder(data).Decode(&o); err != nil { 29 return nil 30 } else { 31 return &o 32 } 33 } 34 35 func ReactionsToJson(o []*Reaction) string { 36 b, _ := json.Marshal(o) 37 return string(b) 38 } 39 40 func MapPostIdToReactionsToJson(o map[string][]*Reaction) string { 41 b, _ := json.Marshal(o) 42 return string(b) 43 } 44 45 func MapPostIdToReactionsFromJson(data io.Reader) map[string][]*Reaction { 46 decoder := json.NewDecoder(data) 47 48 var objmap map[string][]*Reaction 49 if err := decoder.Decode(&objmap); err != nil { 50 return make(map[string][]*Reaction) 51 } else { 52 return objmap 53 } 54 } 55 56 func ReactionsFromJson(data io.Reader) []*Reaction { 57 var o []*Reaction 58 59 if err := json.NewDecoder(data).Decode(&o); err != nil { 60 return nil 61 } else { 62 return o 63 } 64 } 65 66 func (o *Reaction) IsValid() *AppError { 67 if len(o.UserId) != 26 { 68 return NewAppError("Reaction.IsValid", "model.reaction.is_valid.user_id.app_error", nil, "user_id="+o.UserId, http.StatusBadRequest) 69 } 70 71 if len(o.PostId) != 26 { 72 return NewAppError("Reaction.IsValid", "model.reaction.is_valid.post_id.app_error", nil, "post_id="+o.PostId, http.StatusBadRequest) 73 } 74 75 validName := regexp.MustCompile(`^[a-zA-Z0-9\-\+_]+$`) 76 77 if len(o.EmojiName) == 0 || len(o.EmojiName) > EMOJI_NAME_MAX_LENGTH || !validName.MatchString(o.EmojiName) { 78 return NewAppError("Reaction.IsValid", "model.reaction.is_valid.emoji_name.app_error", nil, "emoji_name="+o.EmojiName, http.StatusBadRequest) 79 } 80 81 if o.CreateAt == 0 { 82 return NewAppError("Reaction.IsValid", "model.reaction.is_valid.create_at.app_error", nil, "", http.StatusBadRequest) 83 } 84 85 return nil 86 } 87 88 func (o *Reaction) PreSave() { 89 if o.CreateAt == 0 { 90 o.CreateAt = GetMillis() 91 } 92 }