github.com/crspeller/mattermost-server@v0.0.0-20190328001957-a200beb3d111/model/terms_of_service.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  	"fmt"
     9  	"io"
    10  	"net/http"
    11  	"unicode/utf8"
    12  )
    13  
    14  const TERMS_OF_SERVICE_CACHE_SIZE = 1
    15  
    16  type TermsOfService struct {
    17  	Id       string `json:"id"`
    18  	CreateAt int64  `json:"create_at"`
    19  	UserId   string `json:"user_id"`
    20  	Text     string `json:"text"`
    21  }
    22  
    23  func (t *TermsOfService) IsValid() *AppError {
    24  	if len(t.Id) != 26 {
    25  		return InvalidTermsOfServiceError("id", "")
    26  	}
    27  
    28  	if t.CreateAt == 0 {
    29  		return InvalidTermsOfServiceError("create_at", t.Id)
    30  	}
    31  
    32  	if len(t.UserId) != 26 {
    33  		return InvalidTermsOfServiceError("user_id", t.Id)
    34  	}
    35  
    36  	if utf8.RuneCountInString(t.Text) > POST_MESSAGE_MAX_RUNES_V2 {
    37  		return InvalidTermsOfServiceError("text", t.Id)
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  func (t *TermsOfService) ToJson() string {
    44  	b, _ := json.Marshal(t)
    45  	return string(b)
    46  }
    47  
    48  func TermsOfServiceFromJson(data io.Reader) *TermsOfService {
    49  	var termsOfService *TermsOfService
    50  	json.NewDecoder(data).Decode(&termsOfService)
    51  	return termsOfService
    52  }
    53  
    54  func InvalidTermsOfServiceError(fieldName string, termsOfServiceId string) *AppError {
    55  	id := fmt.Sprintf("model.terms_of_service.is_valid.%s.app_error", fieldName)
    56  	details := ""
    57  	if termsOfServiceId != "" {
    58  		details = "terms_of_service_id=" + termsOfServiceId
    59  	}
    60  	return NewAppError("TermsOfService.IsValid", id, map[string]interface{}{"MaxLength": POST_MESSAGE_MAX_RUNES_V2}, details, http.StatusBadRequest)
    61  }
    62  
    63  func (t *TermsOfService) PreSave() {
    64  	if t.Id == "" {
    65  		t.Id = NewId()
    66  	}
    67  
    68  	t.CreateAt = GetMillis()
    69  }