github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/user_terms_of_service.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  	"fmt"
     9  	"io"
    10  	"net/http"
    11  )
    12  
    13  type UserTermsOfService struct {
    14  	UserId           string `json:"user_id"`
    15  	TermsOfServiceId string `json:"terms_of_service_id"`
    16  	CreateAt         int64  `json:"create_at"`
    17  }
    18  
    19  func (ut *UserTermsOfService) IsValid() *AppError {
    20  	if len(ut.UserId) != 26 {
    21  		return InvalidUserTermsOfServiceError("user_id", ut.UserId)
    22  	}
    23  
    24  	if len(ut.TermsOfServiceId) != 26 {
    25  		return InvalidUserTermsOfServiceError("terms_of_service_id", ut.UserId)
    26  	}
    27  
    28  	if ut.CreateAt == 0 {
    29  		return InvalidUserTermsOfServiceError("create_at", ut.UserId)
    30  	}
    31  
    32  	return nil
    33  }
    34  
    35  func (ut *UserTermsOfService) ToJson() string {
    36  	b, _ := json.Marshal(ut)
    37  	return string(b)
    38  }
    39  
    40  func (ut *UserTermsOfService) PreSave() {
    41  	if ut.UserId == "" {
    42  		ut.UserId = NewId()
    43  	}
    44  
    45  	ut.CreateAt = GetMillis()
    46  }
    47  
    48  func UserTermsOfServiceFromJson(data io.Reader) *UserTermsOfService {
    49  	var userTermsOfService *UserTermsOfService
    50  	json.NewDecoder(data).Decode(&userTermsOfService)
    51  	return userTermsOfService
    52  }
    53  
    54  func InvalidUserTermsOfServiceError(fieldName string, userTermsOfServiceId string) *AppError {
    55  	id := fmt.Sprintf("model.user_terms_of_service.is_valid.%s.app_error", fieldName)
    56  	details := ""
    57  	if userTermsOfServiceId != "" {
    58  		details = "user_terms_of_service_user_id=" + userTermsOfServiceId
    59  	}
    60  	return NewAppError("UserTermsOfService.IsValid", id, nil, details, http.StatusBadRequest)
    61  }