github.com/wgh-/mattermost-server@v4.8.0-rc2+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 ReactionsFromJson(data io.Reader) []*Reaction { 41 var o []*Reaction 42 43 if err := json.NewDecoder(data).Decode(&o); err != nil { 44 return nil 45 } else { 46 return o 47 } 48 } 49 50 func (o *Reaction) IsValid() *AppError { 51 if len(o.UserId) != 26 { 52 return NewAppError("Reaction.IsValid", "model.reaction.is_valid.user_id.app_error", nil, "user_id="+o.UserId, http.StatusBadRequest) 53 } 54 55 if len(o.PostId) != 26 { 56 return NewAppError("Reaction.IsValid", "model.reaction.is_valid.post_id.app_error", nil, "post_id="+o.PostId, http.StatusBadRequest) 57 } 58 59 validName := regexp.MustCompile(`^[a-zA-Z0-9\-\+_]+$`) 60 61 if len(o.EmojiName) == 0 || len(o.EmojiName) > EMOJI_NAME_MAX_LENGTH || !validName.MatchString(o.EmojiName) { 62 return NewAppError("Reaction.IsValid", "model.reaction.is_valid.emoji_name.app_error", nil, "emoji_name="+o.EmojiName, http.StatusBadRequest) 63 } 64 65 if o.CreateAt == 0 { 66 return NewAppError("Reaction.IsValid", "model.reaction.is_valid.create_at.app_error", nil, "", http.StatusBadRequest) 67 } 68 69 return nil 70 } 71 72 func (o *Reaction) PreSave() { 73 if o.CreateAt == 0 { 74 o.CreateAt = GetMillis() 75 } 76 }