Files
mattermost/model/terms_of_service.go
Harshil Sharma 0c5f60f89b #146 Terms of Service Phase 2 (#9731)
* #132 added UserTermsOfService model

* #132 added UserTermsOfService model

* #132 added logic to save user TOS data in a new table

* #132 Added logic to save and delete user TOS. Updated user TOS action logic

* #132 updated store mocks

* #132 added tests

* #132 removed cache from UserTermsOfService SQL store

* #132 fixed styling and license check

* #132 added message translations in en.json

* #132 fixed save user TOS logic to work second time as well

* #132 removed User.AcceptedTermsOfService colum and migrated accepted TOS data into new table

* #132 fixed formatting

* #132 fixed formatting

* #146 added field 'mandatory' to terms of service

* #146 updated tests

* #146 added getLatestTermsOfService API

* #146 Added tests

* #146 fixed styling

* #146 removed code for managing mandatory/optional TOS

* #146 Added TOS re-acceptance period config

* #146 fixed styling

* #146 removed some code left for debugging

* #146 added TOS re-acceptance period in config

* #146 fixed a json name from service_terms to terms_of_service

* #146 Minor refactoring and added TOS re-acceptance period to diagnistics

* Fixed style

* Updated upgraded script to keep app backward compatible
2018-11-08 15:48:14 -05:00

70 lines
1.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package model
import (
"encoding/json"
"fmt"
"io"
"net/http"
"unicode/utf8"
)
const TERMS_OF_SERVICE_CACHE_SIZE = 1
type TermsOfService struct {
Id string `json:"id"`
CreateAt int64 `json:"create_at"`
UserId string `json:"user_id"`
Text string `json:"text"`
}
func (t *TermsOfService) IsValid() *AppError {
if len(t.Id) != 26 {
return InvalidTermsOfServiceError("id", "")
}
if t.CreateAt == 0 {
return InvalidTermsOfServiceError("create_at", t.Id)
}
if len(t.UserId) != 26 {
return InvalidTermsOfServiceError("user_id", t.Id)
}
if utf8.RuneCountInString(t.Text) > POST_MESSAGE_MAX_RUNES_V2 {
return InvalidTermsOfServiceError("text", t.Id)
}
return nil
}
func (t *TermsOfService) ToJson() string {
b, _ := json.Marshal(t)
return string(b)
}
func TermsOfServiceFromJson(data io.Reader) *TermsOfService {
var termsOfService *TermsOfService
json.NewDecoder(data).Decode(&termsOfService)
return termsOfService
}
func InvalidTermsOfServiceError(fieldName string, termsOfServiceId string) *AppError {
id := fmt.Sprintf("model.terms_of_service.is_valid.%s.app_error", fieldName)
details := ""
if termsOfServiceId != "" {
details = "terms_of_service_id=" + termsOfServiceId
}
return NewAppError("TermsOfService.IsValid", id, map[string]interface{}{"MaxLength": POST_MESSAGE_MAX_RUNES_V2}, details, http.StatusBadRequest)
}
func (t *TermsOfService) PreSave() {
if t.Id == "" {
t.Id = NewId()
}
t.CreateAt = GetMillis()
}