github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/model/reaction.go (about)

     1  package model
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"net/http"
     7  	"regexp"
     8  )
     9  
    10  type Reaction struct {
    11  	UserId    string  `json:"user_id"`
    12  	PostId    string  `json:"post_id"`
    13  	EmojiName string  `json:"emoji_name"`
    14  	CreateAt  int64   `json:"create_at"`
    15  	UpdateAt  int64   `json:"update_at"`
    16  	DeleteAt  int64   `json:"delete_at"`
    17  	RemoteId  *string `json:"remote_id"`
    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  	}
    31  	return &o
    32  }
    33  
    34  func ReactionsToJson(o []*Reaction) string {
    35  	b, _ := json.Marshal(o)
    36  	return string(b)
    37  }
    38  
    39  func MapPostIdToReactionsToJson(o map[string][]*Reaction) string {
    40  	b, _ := json.Marshal(o)
    41  	return string(b)
    42  }
    43  
    44  func MapPostIdToReactionsFromJson(data io.Reader) map[string][]*Reaction {
    45  	decoder := json.NewDecoder(data)
    46  
    47  	var objmap map[string][]*Reaction
    48  	if err := decoder.Decode(&objmap); err != nil {
    49  		return make(map[string][]*Reaction)
    50  	}
    51  	return objmap
    52  }
    53  
    54  func ReactionsFromJson(data io.Reader) []*Reaction {
    55  	var o []*Reaction
    56  
    57  	if err := json.NewDecoder(data).Decode(&o); err != nil {
    58  		return nil
    59  	}
    60  	return o
    61  }
    62  
    63  func (o *Reaction) IsValid() *AppError {
    64  	if !IsValidId(o.UserId) {
    65  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.user_id.app_error", nil, "user_id="+o.UserId, http.StatusBadRequest)
    66  	}
    67  
    68  	if !IsValidId(o.PostId) {
    69  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.post_id.app_error", nil, "post_id="+o.PostId, http.StatusBadRequest)
    70  	}
    71  
    72  	validName := regexp.MustCompile(`^[a-zA-Z0-9\-\+_]+$`)
    73  
    74  	if o.EmojiName == "" || len(o.EmojiName) > EMOJI_NAME_MAX_LENGTH || !validName.MatchString(o.EmojiName) {
    75  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.emoji_name.app_error", nil, "emoji_name="+o.EmojiName, http.StatusBadRequest)
    76  	}
    77  
    78  	if o.CreateAt == 0 {
    79  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
    80  	}
    81  
    82  	if o.UpdateAt == 0 {
    83  		return NewAppError("Reaction.IsValid", "model.reaction.is_valid.update_at.app_error", nil, "", http.StatusBadRequest)
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func (o *Reaction) PreSave() {
    90  	if o.CreateAt == 0 {
    91  		o.CreateAt = GetMillis()
    92  	}
    93  	o.UpdateAt = GetMillis()
    94  	o.DeleteAt = 0
    95  
    96  	if o.RemoteId == nil {
    97  		o.RemoteId = NewString("")
    98  	}
    99  }
   100  
   101  func (o *Reaction) PreUpdate() {
   102  	o.UpdateAt = GetMillis()
   103  
   104  	if o.RemoteId == nil {
   105  		o.RemoteId = NewString("")
   106  	}
   107  }