1// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2// See LICENSE.txt for license information.
3
4package model
5
6import (
7	"fmt"
8	"net/http"
9	"unicode/utf8"
10)
11
12type TermsOfService struct {
13	Id       string `json:"id"`
14	CreateAt int64  `json:"create_at"`
15	UserId   string `json:"user_id"`
16	Text     string `json:"text"`
17}
18
19func (t *TermsOfService) IsValid() *AppError {
20	if !IsValidId(t.Id) {
21		return InvalidTermsOfServiceError("id", "")
22	}
23
24	if t.CreateAt == 0 {
25		return InvalidTermsOfServiceError("create_at", t.Id)
26	}
27
28	if !IsValidId(t.UserId) {
29		return InvalidTermsOfServiceError("user_id", t.Id)
30	}
31
32	if utf8.RuneCountInString(t.Text) > PostMessageMaxRunesV2 {
33		return InvalidTermsOfServiceError("text", t.Id)
34	}
35
36	return nil
37}
38
39func InvalidTermsOfServiceError(fieldName string, termsOfServiceId string) *AppError {
40	id := fmt.Sprintf("model.terms_of_service.is_valid.%s.app_error", fieldName)
41	details := ""
42	if termsOfServiceId != "" {
43		details = "terms_of_service_id=" + termsOfServiceId
44	}
45	return NewAppError("TermsOfService.IsValid", id, map[string]interface{}{"MaxLength": PostMessageMaxRunesV2}, details, http.StatusBadRequest)
46}
47
48func (t *TermsOfService) PreSave() {
49	if t.Id == "" {
50		t.Id = NewId()
51	}
52
53	t.CreateAt = GetMillis()
54}
55