github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/model/compliance.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  	"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 (o *Compliance) ToJson() string {
    41  	b, _ := json.Marshal(o)
    42  	return string(b)
    43  }
    44  
    45  func (me *Compliance) PreSave() {
    46  	if me.Id == "" {
    47  		me.Id = NewId()
    48  	}
    49  
    50  	if me.Status == "" {
    51  		me.Status = COMPLIANCE_STATUS_CREATED
    52  	}
    53  
    54  	me.Count = 0
    55  	me.Emails = NormalizeEmail(me.Emails)
    56  	me.Keywords = strings.ToLower(me.Keywords)
    57  
    58  	me.CreateAt = GetMillis()
    59  }
    60  
    61  func (me *Compliance) JobName() string {
    62  	jobName := me.Type
    63  	if me.Type == COMPLIANCE_TYPE_DAILY {
    64  		jobName += "-" + me.Desc
    65  	}
    66  
    67  	jobName += "-" + me.Id
    68  
    69  	return jobName
    70  }
    71  
    72  func (me *Compliance) IsValid() *AppError {
    73  
    74  	if len(me.Id) != 26 {
    75  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.id.app_error", nil, "", http.StatusBadRequest)
    76  	}
    77  
    78  	if me.CreateAt == 0 {
    79  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
    80  	}
    81  
    82  	if len(me.Desc) > 512 || len(me.Desc) == 0 {
    83  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.desc.app_error", nil, "", http.StatusBadRequest)
    84  	}
    85  
    86  	if me.StartAt == 0 {
    87  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.start_at.app_error", nil, "", http.StatusBadRequest)
    88  	}
    89  
    90  	if me.EndAt == 0 {
    91  		return NewAppError("Compliance.IsValid", "model.compliance.is_valid.end_at.app_error", nil, "", http.StatusBadRequest)
    92  	}
    93  
    94  	if me.EndAt <= me.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 o *Compliance
   103  	json.NewDecoder(data).Decode(&o)
   104  	return o
   105  }
   106  
   107  func (o Compliances) ToJson() string {
   108  	if b, err := json.Marshal(o); 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  }