diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-10-30 00:28:51 -0300 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 8040825daac4a5ba24e947c4c657d169d7763543 (patch) | |
tree | 667058707ab1c2b2af5dc01decda25cc7709f9df /db/blacklist.go | |
parent | f76b8093b9ca69c7d4eca747d8c5d702d492e1fb (diff) |
restructuring, part 3 of many
Diffstat (limited to 'db/blacklist.go')
-rw-r--r-- | db/blacklist.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/db/blacklist.go b/db/blacklist.go new file mode 100644 index 0000000..4d88e10 --- /dev/null +++ b/db/blacklist.go @@ -0,0 +1,76 @@ +package db + +import "regexp" + +type PostBlacklist struct { + Id int + Regex string +} + +func WriteRegexBlacklistDB(regex string) error { + query := `select from postblacklist where regex=$1` + + rows, err := db.Query(query, regex) + if err != nil { + return err + } + defer rows.Close() + + var re string + rows.Next() + rows.Scan(&re) + + if re != "" { + return nil + } + + query = `insert into postblacklist (regex) values ($1)` + + _, err = db.Exec(query, regex) + return err +} + +func GetRegexBlacklistDB() ([]PostBlacklist, error) { + var list []PostBlacklist + + query := `select id, regex from postblacklist` + + rows, err := db.Query(query) + if err != nil { + return list, err + } + + defer rows.Close() + for rows.Next() { + var temp PostBlacklist + rows.Scan(&temp.Id, &temp.Regex) + + list = append(list, temp) + } + + return list, nil +} + +func DeleteRegexBlacklistDB(id int) error { + query := `delete from postblacklist where id=$1` + + _, err := db.Exec(query, id) + return err +} + +func IsPostBlacklist(comment string) (bool, error) { + postblacklist, err := GetRegexBlacklistDB() + if err != nil { + return false, err + } + + for _, e := range postblacklist { + re := regexp.MustCompile(e.Regex) + + if re.MatchString(comment) { + return true, nil + } + } + + return false, nil +} |