github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/model/reaction.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  	"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  	UpdateAt  int64  `json:"update_at"`
    19  	DeleteAt  int64  `json:"delete_at"`
    20  }
    21  
    22  func (o *Reaction) ToJson() string {
    23  	b, _ := json.Marshal(o)
    24  	return string(b)
    25  }
    26  
    27  func ReactionFromJson(data io.Reader) *Reaction {
    28  	var o Reaction
    29  
    30  	if err := json.NewDecoder(data).Decode(&o); err != nil {
    31  		return nil
    32  	}
    33  	return &o
    34  }
    35  
    36  func ReactionsToJson(o []*Reaction) string {
    37  	b, _ := json.Marshal(o)
    38  	return string(b)
    39  }
    40  
    41  func MapPostIdToReactionsToJson(o map[string][]*Reaction) string {
    42  	b, _ := json.Marshal(o)
    43  	return string(b)
    44  }
    45  
    46  func MapPostIdToReactionsFromJson(data io.Reader) map[string][]*Reaction {
    47  	decoder := json.NewDecoder(data)
    48  
    49  	var objmap map[string][]*Reaction
    50  	if err := decoder.Decode(&objmap); err != nil {
    51  		return make(map[string][]*Reaction)
    52  	}
    53  	return objmap
    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  	}
    62  	return o
    63  }
    64  
    65  func (o *Reaction) IsValid() *AppError {
    66  	if !IsValidId(o.UserId) {
    67  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.user_id.app_error", nil, "user_id="+o.UserId, http.StatusBadRequest)
    68  	}
    69  
    70  	if !IsValidId(o.PostId) {
    71  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.post_id.app_error", nil, "post_id="+o.PostId, http.StatusBadRequest)
    72  	}
    73  
    74  	validName := regexp.MustCompile(`^[a-zA-Z0-9\-\+_]+$`)
    75  
    76  	if o.EmojiName == "" || len(o.EmojiName) > EMOJI_NAME_MAX_LENGTH || !validName.MatchString(o.EmojiName) {
    77  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.emoji_name.app_error", nil, "emoji_name="+o.EmojiName, http.StatusBadRequest)
    78  	}
    79  
    80  	if o.CreateAt == 0 {
    81  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
    82  	}
    83  
    84  	if o.UpdateAt == 0 {
    85  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.update_at.app_error", nil, "", http.StatusBadRequest)
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  func (o *Reaction) PreSave() {
    92  	if o.CreateAt == 0 {
    93  		o.CreateAt = GetMillis()
    94  	}
    95  	o.UpdateAt = GetMillis()
    96  	o.DeleteAt = 0
    97  }
    98  
    99  func (o *Reaction) PreUpdate() {
   100  	o.UpdateAt = GetMillis()
   101  }