...

Source file src/codeberg.org/tslocum/sriracha/model/model_keyword.go

Documentation: codeberg.org/tslocum/sriracha/model

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  )
     8  
     9  type Keyword struct {
    10  	ID     int
    11  	Text   string
    12  	Action string
    13  	Boards []*Board `diff:"-"`
    14  }
    15  
    16  func (k *Keyword) Validate() error {
    17  	switch {
    18  	case strings.TrimSpace(k.Text) == "":
    19  		return fmt.Errorf("text must be set")
    20  	case k.Action != "hide" && k.Action != "report" && k.Action != "delete" &&
    21  		k.Action != "ban1h" && k.Action != "ban1d" && k.Action != "ban2d" &&
    22  		k.Action != "ban1w" && k.Action != "ban2w" && k.Action != "ban1m" &&
    23  		k.Action != "ban0":
    24  		return fmt.Errorf("action must be set")
    25  	}
    26  	_, err := regexp.Compile(k.Text)
    27  	if err != nil {
    28  		return fmt.Errorf("keyword `%s` is invalid: %s", k.Text, err)
    29  	}
    30  	return nil
    31  }
    32  
    33  func (k *Keyword) HasBoard(id int) bool {
    34  	for _, b := range k.Boards {
    35  		if b.ID == id {
    36  			return true
    37  		}
    38  	}
    39  	return false
    40  }
    41  
    42  func (k *Keyword) ActionLabel(account *Account) string {
    43  	var label string
    44  	switch k.Action {
    45  	case "hide":
    46  		label = "Hide until approved"
    47  	case "report":
    48  		label = "Report"
    49  	case "delete":
    50  		label = "Delete"
    51  	case "ban1h":
    52  		label = "Delete & ban for 1 hour"
    53  	case "ban1d":
    54  		label = "Delete & ban for 1 day"
    55  	case "ban2d":
    56  		label = "Delete & ban for 2 days"
    57  	case "ban1w":
    58  		label = "Delete & ban for 1 week"
    59  	case "ban2w":
    60  		label = "Delete & ban for 2 weeks"
    61  	case "ban1m":
    62  		label = "Delete & ban for 1 month"
    63  	case "ban0":
    64  		label = "Delete & ban permanently"
    65  	default:
    66  		label = "Unknown"
    67  	}
    68  	return Get(nil, account, label)
    69  }
    70  

View as plain text