github.com/adacta-ru/mattermost-server/v6@v6.0.0/model/compliance.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  	"io"
     9  	"net/http"
    10  	"strings"
    11  )
    12  
    13  const (
    14  	COMPLIANCE_STATUS_CREATED  = "created"
    15  	COMPLIANCE_STATUS_RUNNING  = "running"
    16  	COMPLIANCE_STATUS_FINISHED = "finished"
    17  	COMPLIANCE_STATUS_FAILED   = "failed"
    18  	COMPLIANCE_STATUS_REMOVED  = "removed"
    19  
    20  	COMPLIANCE_TYPE_DAILY = "daily"
    21  	COMPLIANCE_TYPE_ADHOC = "adhoc"
    22  )
    23  
    24  type Compliance struct {
    25  	Id       string `json:"id"`
    26  	CreateAt int64  `json:"create_at"`
    27  	UserId   string `json:"user_id"`
    28  	Status   string `json:"status"`
    29  	Count    int    `json:"count"`
    30  	Desc     string `json:"desc"`
    31  	Type     string `json:"type"`
    32  	StartAt  int64  `json:"start_at"`
    33  	EndAt    int64  `json:"end_at"`
    34  	Keywords string `json:"keywords"`
    35  	Emails   string `json:"emails"`
    36  }
    37  
    38  type Compliances []Compliance
    39  
    40  func (c *Compliance) ToJson() string {
    41  	b, _ := json.Marshal(c)
    42  	return string(b)
    43  }
    44  
    45  func (c *Compliance) PreSave() {
    46  	if c.Id == "" {
    47  		c.Id = NewId()
    48  	}
    49  
    50  	if c.Status == "" {
    51  		c.Status = COMPLIANCE_STATUS_CREATED
    52  	}
    53  
    54  	c.Count = 0
    55  	c.Emails = NormalizeEmail(c.Emails)
    56  	c.Keywords = strings.ToLower(c.Keywords)
    57  
    58  	c.CreateAt = GetMillis()
    59  }
    60  
    61  func (c *Compliance) DeepCopy() *Compliance {
    62  	copy := *c
    63  	return &copy
    64  }
    65  
    66  func (c *Compliance) JobName() string {
    67  	jobName := c.Type
    68  	if c.Type == COMPLIANCE_TYPE_DAILY {
    69  		jobName += "-" + c.Desc
    70  	}
    71  
    72  	jobName += "-" + c.Id
    73  
    74  	return jobName
    75  }
    76  
    77  func (c *Compliance) IsValid() *AppError {
    78  
    79  	if !IsValidId(c.Id) {
    80  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.id.app_error", nil, "", http.StatusBadRequest)
    81  	}
    82  
    83  	if c.CreateAt == 0 {
    84  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
    85  	}
    86  
    87  	if len(c.Desc) > 512 || len(c.Desc) == 0 {
    88  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.desc.app_error", nil, "", http.StatusBadRequest)
    89  	}
    90  
    91  	if c.StartAt == 0 {
    92  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.start_at.app_error", nil, "", http.StatusBadRequest)
    93  	}
    94  
    95  	if c.EndAt == 0 {
    96  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.end_at.app_error", nil, "", http.StatusBadRequest)
    97  	}
    98  
    99  	if c.EndAt <= c.StartAt {
   100  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.start_end_at.app_error", nil, "", http.StatusBadRequest)
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  func ComplianceFromJson(data io.Reader) *Compliance {
   107  	var c *Compliance
   108  	json.NewDecoder(data).Decode(&c)
   109  	return c
   110  }
   111  
   112  func (c Compliances) ToJson() string {
   113  	b, err := json.Marshal(c)
   114  	if err != nil {
   115  		return "[]"
   116  	}
   117  	return string(b)
   118  }
   119  
   120  func CompliancesFromJson(data io.Reader) Compliances {
   121  	var o Compliances
   122  	json.NewDecoder(data).Decode(&o)
   123  	return o
   124  }