github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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) JobName() string {
    62  	jobName := c.Type
    63  	if c.Type == COMPLIANCE_TYPE_DAILY {
    64  		jobName += "-" + c.Desc
    65  	}
    66  
    67  	jobName += "-" + c.Id
    68  
    69  	return jobName
    70  }
    71  
    72  func (c *Compliance) IsValid() *AppError {
    73  
    74  	if len(c.Id) != 26 {
    75  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.id.app_error", nil, "", http.StatusBadRequest)
    76  	}
    77  
    78  	if c.CreateAt == 0 {
    79  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
    80  	}
    81  
    82  	if len(c.Desc) > 512 || len(c.Desc) == 0 {
    83  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.desc.app_error", nil, "", http.StatusBadRequest)
    84  	}
    85  
    86  	if c.StartAt == 0 {
    87  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.start_at.app_error", nil, "", http.StatusBadRequest)
    88  	}
    89  
    90  	if c.EndAt == 0 {
    91  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.end_at.app_error", nil, "", http.StatusBadRequest)
    92  	}
    93  
    94  	if c.EndAt <= c.StartAt {
    95  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.start_end_at.app_error", nil, "", http.StatusBadRequest)
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  func ComplianceFromJson(data io.Reader) *Compliance {
   102  	var c *Compliance
   103  	json.NewDecoder(data).Decode(&c)
   104  	return c
   105  }
   106  
   107  func (c Compliances) ToJson() string {
   108  	if b, err := json.Marshal(c); err != nil {
   109  		return "[]"
   110  	} else {
   111  		return string(b)
   112  	}
   113  }
   114  
   115  func CompliancesFromJson(data io.Reader) Compliances {
   116  	var o Compliances
   117  	json.NewDecoder(data).Decode(&o)
   118  	return o
   119  }