From bddde5c637c470dc6297751294571843ba5990fd Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sat, 1 Oct 2022 15:50:48 -0400 Subject: [PATCH] Bump Go version, Generics whoooo --- .github/workflows/build.yaml | 2 +- .github/workflows/release.yaml | 2 +- .github/workflows/test.yaml | 4 ++-- cmd/access.go | 6 +++--- cmd/serve.go | 4 ++-- cmd/user.go | 2 +- go.mod | 18 +++++++++--------- go.sum | 16 ++++++++++++++++ server/actions.go | 10 +++++----- server/server.go | 4 ++-- server/smtp_sender.go | 2 +- server/types.go | 4 ++-- util/util.go | 18 ++++-------------- util/util_test.go | 14 +++++++------- 14 files changed, 56 insertions(+), 50 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 45a5ae2a..c1e2a247 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -13,7 +13,7 @@ jobs: name: Install node uses: actions/setup-node@v2 with: - node-version: '16' + node-version: '17' - name: Checkout code uses: actions/checkout@v2 diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index a2a6e335..6ce2297b 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -16,7 +16,7 @@ jobs: name: Install node uses: actions/setup-node@v2 with: - node-version: '16' + node-version: '17' - name: Checkout code uses: actions/checkout@v2 diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 96c43d85..852654ac 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -3,7 +3,7 @@ on: [push, pull_request] jobs: test: runs-on: ubuntu-latest - steps: + steps: - name: Install Go uses: actions/setup-go@v2 @@ -13,7 +13,7 @@ jobs: name: Install node uses: actions/setup-node@v2 with: - node-version: '16' + node-version: '17' - name: Checkout code uses: actions/checkout@v2 diff --git a/cmd/access.go b/cmd/access.go index e1f61bc1..9bd65f84 100644 --- a/cmd/access.go +++ b/cmd/access.go @@ -97,11 +97,11 @@ func execUserAccess(c *cli.Context) error { } func changeAccess(c *cli.Context, manager auth.Manager, username string, topic string, perms string) error { - if !util.InStringList([]string{"", "read-write", "rw", "read-only", "read", "ro", "write-only", "write", "wo", "none", "deny"}, perms) { + if !util.Contains([]string{"", "read-write", "rw", "read-only", "read", "ro", "write-only", "write", "wo", "none", "deny"}, perms) { return errors.New("permission must be one of: read-write, read-only, write-only, or deny (or the aliases: read, ro, write, wo, none)") } - read := util.InStringList([]string{"read-write", "rw", "read-only", "read", "ro"}, perms) - write := util.InStringList([]string{"read-write", "rw", "write-only", "write", "wo"}, perms) + read := util.Contains([]string{"read-write", "rw", "read-only", "read", "ro"}, perms) + write := util.Contains([]string{"read-write", "rw", "write-only", "write", "wo"}, perms) user, err := manager.User(username) if err == auth.ErrNotFound { return fmt.Errorf("user %s does not exist", username) diff --git a/cmd/serve.go b/cmd/serve.go index 126cf0dc..952c426e 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -161,9 +161,9 @@ func execServe(c *cli.Context) error { return errors.New("if set, base-url must start with http:// or https://") } else if baseURL != "" && strings.HasSuffix(baseURL, "/") { return errors.New("if set, base-url must not end with a slash (/)") - } else if !util.InStringList([]string{"read-write", "read-only", "write-only", "deny-all"}, authDefaultAccess) { + } else if !util.Contains([]string{"read-write", "read-only", "write-only", "deny-all"}, authDefaultAccess) { return errors.New("if set, auth-default-access must start set to 'read-write', 'read-only', 'write-only' or 'deny-all'") - } else if !util.InStringList([]string{"app", "home", "disable"}, webRoot) { + } else if !util.Contains([]string{"app", "home", "disable"}, webRoot) { return errors.New("if set, web-root must be 'home' or 'app'") } else if upstreamBaseURL != "" && !strings.HasPrefix(upstreamBaseURL, "http://") && !strings.HasPrefix(upstreamBaseURL, "https://") { return errors.New("if set, upstream-base-url must start with http:// or https://") diff --git a/cmd/user.go b/cmd/user.go index e89ee5e5..052c0800 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -273,7 +273,7 @@ func createAuthManager(c *cli.Context) (auth.Manager, error) { return nil, errors.New("option auth-file not set; auth is unconfigured for this server") } else if !util.FileExists(authFile) { return nil, errors.New("auth-file does not exist; please start the server at least once to create it") - } else if !util.InStringList([]string{"read-write", "read-only", "write-only", "deny-all"}, authDefaultAccess) { + } else if !util.Contains([]string{"read-write", "read-only", "write-only", "deny-all"}, authDefaultAccess) { return nil, errors.New("if set, auth-default-access must start set to 'read-write', 'read-only' or 'deny-all'") } authDefaultRead := authDefaultAccess == "read-write" || authDefaultAccess == "read-only" diff --git a/go.mod b/go.mod index a149167c..56c13e19 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module heckel.io/ntfy -go 1.17 +go 1.18 require ( cloud.google.com/go/firestore v1.6.1 // indirect @@ -13,13 +13,13 @@ require ( github.com/mattn/go-sqlite3 v1.14.15 github.com/olebedev/when v0.0.0-20211212231525-59bd4edcf9d6 github.com/stretchr/testify v1.7.0 - github.com/urfave/cli/v2 v2.16.3 + github.com/urfave/cli/v2 v2.17.1 golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1 // indirect - golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 + golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 golang.org/x/term v0.0.0-20220919170432-7a66f970e087 golang.org/x/time v0.0.0-20220922220347-f3bd1da661af - google.golang.org/api v0.97.0 + google.golang.org/api v0.98.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -30,7 +30,7 @@ require firebase.google.com/go/v4 v4.8.0 require ( cloud.google.com/go v0.104.0 // indirect cloud.google.com/go/compute v1.10.0 // indirect - cloud.google.com/go/iam v0.4.0 // indirect + cloud.google.com/go/iam v0.5.0 // indirect github.com/AlekSi/pointer v1.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead // indirect @@ -38,19 +38,19 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect + github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect github.com/googleapis/gax-go/v2 v2.5.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.opencensus.io v0.23.0 // indirect - golang.org/x/net v0.0.0-20220927155233-aa73b2587036 // indirect - golang.org/x/sys v0.0.0-20220926163933-8cfa568d3c25 // indirect + golang.org/x/net v0.0.0-20220930213112-107f3e3c3b0b // indirect + golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/appengine/v2 v2.0.2 // indirect - google.golang.org/genproto v0.0.0-20220927151529-dcaddaf36704 // indirect + google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 // indirect google.golang.org/grpc v1.49.0 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 5b070196..403df2d1 100644 --- a/go.sum +++ b/go.sum @@ -83,6 +83,8 @@ cloud.google.com/go/iam v0.1.1/go.mod h1:CKqrcnI/suGpybEHxZ7BMehL0oA4LpdyJdUlTl9 cloud.google.com/go/iam v0.3.0/go.mod h1:XzJPvDayI+9zsASAFO68Hk07u3z+f+JrT2xXNdp4bnY= cloud.google.com/go/iam v0.4.0 h1:YBYU00SCDzZJdHqVc4I5d6lsklcYIjQZa1YmEz4jlSE= cloud.google.com/go/iam v0.4.0/go.mod h1:cbaZxyScUhxl7ZAkNWiALgihfP75wS/fUsVNaa1r3vA= +cloud.google.com/go/iam v0.5.0 h1:fz9X5zyTWBmamZsqvqZqD7khbifcZF/q+Z1J8pfhIUg= +cloud.google.com/go/iam v0.5.0/go.mod h1:wPU9Vt0P4UmCux7mqtRu6jcpPAb74cP1fh50J3QpkUc= cloud.google.com/go/language v1.4.0/go.mod h1:F9dRpNFQmJbkaop6g0JhSBXCNlO90e1KWx5iDdxbWic= cloud.google.com/go/lifesciences v0.5.0/go.mod h1:3oIKy8ycWGPUyZDR/8RNnTOYevhaMLqh5vLUXs9zvT8= cloud.google.com/go/mediatranslation v0.5.0/go.mod h1:jGPUhGTybqsPQn91pNXw0xVHfuJ3leR1wj37oU3y1f4= @@ -262,6 +264,8 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8= github.com/googleapis/enterprise-certificate-proxy v0.1.0 h1:zO8WHNx/MYiAKJ3d5spxZXZE6KHmIQGQcAzwUzV7qQw= github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8= +github.com/googleapis/enterprise-certificate-proxy v0.2.0 h1:y8Yozv7SZtlU//QXbezB6QkpuE6jMD2/gfzk4AftXjs= +github.com/googleapis/enterprise-certificate-proxy v0.2.0/go.mod h1:8C0jb7/mgJe/9KK8Lm7X9ctZC2t60YyIpYEI16jx0Qg= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0= @@ -311,6 +315,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/urfave/cli/v2 v2.16.3 h1:gHoFIwpPjoyIMbJp/VFd+/vuD0dAgFK4B6DpEMFJfQk= github.com/urfave/cli/v2 v2.16.3/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI= +github.com/urfave/cli/v2 v2.17.1 h1:UzjDEw2dJQUE3iRaiNQ1VrVFbyAtKGH3VdkMoHA58V0= +github.com/urfave/cli/v2 v2.17.1/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -417,6 +423,8 @@ golang.org/x/net v0.0.0-20220708220712-1185a9018129/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.0.0-20220927155233-aa73b2587036 h1:GDWXwjBkdo4XMin5T4iul98eH4BfGOR7TucJ057FxjY= golang.org/x/net v0.0.0-20220927155233-aa73b2587036/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.0.0-20220930213112-107f3e3c3b0b h1:uKO3Js8lXGjpjdc4J3rqs0/Ex5yDKUGfk43tTYWVLas= +golang.org/x/net v0.0.0-20220930213112-107f3e3c3b0b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -456,6 +464,8 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 h1:ZrnxWX62AgTKOSagEqxvb3ffipvEDX2pl7E1TdqLqIc= golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 h1:cu5kTvlzcw1Q5S9f5ip1/cpiB4nXvw1XYzFPGgzLUOY= +golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -522,6 +532,8 @@ golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220926163933-8cfa568d3c25 h1:nwzwVf0l2Y/lkov/+IYgMMbFyI+QypZDds9RxlSmsFQ= golang.org/x/sys v0.0.0-20220926163933-8cfa568d3c25/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI= +golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20220919170432-7a66f970e087 h1:tPwmk4vmvVCMdr98VgL4JH+qZxPL8fqlUOHnyOM8N3w= @@ -653,6 +665,8 @@ google.golang.org/api v0.95.0/go.mod h1:eADj+UBuxkh5zlrSntJghuNeg8HwQ1w5lTKkuqaE google.golang.org/api v0.96.0/go.mod h1:w7wJQLTM+wvQpNf5JyEcBoxK0RH7EDrh/L4qfsuJ13s= google.golang.org/api v0.97.0 h1:x/vEL1XDF/2V4xzdNgFPaKHluRESo2aTsL7QzHnBtGQ= google.golang.org/api v0.97.0/go.mod h1:w7wJQLTM+wvQpNf5JyEcBoxK0RH7EDrh/L4qfsuJ13s= +google.golang.org/api v0.98.0 h1:yxZrcxXESimy6r6mdL5Q6EnZwmewDJK2dVg3g75s5Dg= +google.golang.org/api v0.98.0/go.mod h1:w7wJQLTM+wvQpNf5JyEcBoxK0RH7EDrh/L4qfsuJ13s= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -769,6 +783,8 @@ google.golang.org/genproto v0.0.0-20220919141832-68c03719ef51/go.mod h1:0Nb8Qy+S google.golang.org/genproto v0.0.0-20220920201722-2b89144ce006/go.mod h1:ht8XFiar2npT/g4vkk7O0WYS1sHOHbdujxbEp7CJWbw= google.golang.org/genproto v0.0.0-20220927151529-dcaddaf36704 h1:H1AcWFV69NFCMeBJ8nVLtv8uHZZ5Ozcgoq012hHEFuU= google.golang.org/genproto v0.0.0-20220927151529-dcaddaf36704/go.mod h1:woMGP53BroOrRY3xTxlbr8Y3eB/nzAvvFM83q7kG2OI= +google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 h1:Ezh2cpcnP5Rq60sLensUsFnxh7P6513NLvNtCm9iyJ4= +google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91/go.mod h1:3526vdqwhZAwq4wsRUaVG555sVgsNmIjRtO7t/JH29U= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= diff --git a/server/actions.go b/server/actions.go index dc8ad970..80065873 100644 --- a/server/actions.go +++ b/server/actions.go @@ -60,13 +60,13 @@ func parseActions(s string) (actions []*action, err error) { return nil, fmt.Errorf("only %d actions allowed", actionsMax) } for _, action := range actions { - if !util.InStringList(actionsAll, action.Action) { + if !util.Contains(actionsAll, action.Action) { return nil, fmt.Errorf("parameter 'action' cannot be '%s', valid values are 'view', 'broadcast' and 'http'", action.Action) } else if action.Label == "" { return nil, fmt.Errorf("parameter 'label' is required") - } else if util.InStringList(actionsWithURL, action.Action) && action.URL == "" { + } else if util.Contains(actionsWithURL, action.Action) && action.URL == "" { return nil, fmt.Errorf("parameter 'url' is required for action '%s'", action.Action) - } else if action.Action == actionHTTP && util.InStringList([]string{"GET", "HEAD"}, action.Method) && action.Body != "" { + } else if action.Action == actionHTTP && util.Contains([]string{"GET", "HEAD"}, action.Method) && action.Body != "" { return nil, fmt.Errorf("parameter 'body' cannot be set if method is %s", action.Method) } } @@ -156,7 +156,7 @@ func populateAction(newAction *action, section int, key, value string) error { key = "action" } else if key == "" && section == 1 { key = "label" - } else if key == "" && section == 2 && util.InStringList(actionsWithURL, newAction.Action) { + } else if key == "" && section == 2 && util.Contains(actionsWithURL, newAction.Action) { key = "url" } @@ -178,7 +178,7 @@ func populateAction(newAction *action, section int, key, value string) error { newAction.Label = value case "clear": lvalue := strings.ToLower(value) - if !util.InStringList([]string{"true", "yes", "1", "false", "no", "0"}, lvalue) { + if !util.Contains([]string{"true", "yes", "1", "false", "no", "0"}, lvalue) { return fmt.Errorf("parameter 'clear' cannot be '%s', only boolean values are allowed (true/yes/1/false/no/0)", value) } newAction.Clear = lvalue == "true" || lvalue == "yes" || lvalue == "1" diff --git a/server/server.go b/server/server.go index 60261693..276e56fa 100644 --- a/server/server.go +++ b/server/server.go @@ -1088,7 +1088,7 @@ func (s *Server) topicsFromIDs(ids ...string) ([]*topic, error) { defer s.mu.Unlock() topics := make([]*topic, 0) for _, id := range ids { - if util.InStringList(disallowedTopics, id) { + if util.Contains(disallowedTopics, id) { return nil, errHTTPBadRequestTopicDisallowed } if _, ok := s.topics[id]; !ok { @@ -1286,7 +1286,7 @@ func (s *Server) sendDelayedMessage(v *visitor, m *message) error { func (s *Server) limitRequests(next handleFunc) handleFunc { return func(w http.ResponseWriter, r *http.Request, v *visitor) error { - if util.InStringList(s.config.VisitorRequestExemptIPAddrs, v.ip) { + if util.Contains(s.config.VisitorRequestExemptIPAddrs, v.ip) { return next(w, r, v) } else if err := v.RequestAllowed(); err != nil { return errHTTPTooManyRequestsLimitRequests diff --git a/server/smtp_sender.go b/server/smtp_sender.go index 1ccaf084..ecefd9c2 100644 --- a/server/smtp_sender.go +++ b/server/smtp_sender.go @@ -137,7 +137,7 @@ func toEmojis(tags []string) (emojisOut []string, tagsOut []string, err error) { nextTag: for _, t := range tags { // TODO Super inefficient; we should just create a .json file with a map for _, e := range emojis { - if util.InStringList(e.Aliases, t) { + if util.Contains(e.Aliases, t) { emojisOut = append(emojisOut, e.Emoji) continue nextTag } diff --git a/server/types.go b/server/types.go index b502dcd7..b217b9db 100644 --- a/server/types.go +++ b/server/types.go @@ -203,10 +203,10 @@ func (q *queryFilter) Pass(msg *message) bool { if messagePriority == 0 { messagePriority = 3 // For query filters, default priority (3) is the same as "not set" (0) } - if len(q.Priority) > 0 && !util.InIntList(q.Priority, messagePriority) { + if len(q.Priority) > 0 && !util.Contains(q.Priority, messagePriority) { return false } - if len(q.Tags) > 0 && !util.InStringListAll(msg.Tags, q.Tags) { + if len(q.Tags) > 0 && !util.ContainsAll(msg.Tags, q.Tags) { return false } return true diff --git a/util/util.go b/util/util.go index 6c7fab69..3f65c8b7 100644 --- a/util/util.go +++ b/util/util.go @@ -35,8 +35,8 @@ func FileExists(filename string) bool { return stat != nil } -// InStringList returns true if needle is contained in haystack -func InStringList(haystack []string, needle string) bool { +// Contains returns true if needle is contained in haystack +func Contains[T comparable](haystack []T, needle T) bool { for _, s := range haystack { if s == needle { return true @@ -45,8 +45,8 @@ func InStringList(haystack []string, needle string) bool { return false } -// InStringListAll returns true if all needles are contained in haystack -func InStringListAll(haystack []string, needles []string) bool { +// ContainsAll returns true if all needles are contained in haystack +func ContainsAll[T comparable](haystack []T, needles []T) bool { matches := 0 for _, s := range haystack { for _, needle := range needles { @@ -58,16 +58,6 @@ func InStringListAll(haystack []string, needles []string) bool { return matches == len(needles) } -// InIntList returns true if needle is contained in haystack -func InIntList(haystack []int, needle int) bool { - for _, s := range haystack { - if s == needle { - return true - } - } - return false -} - // SplitNoEmpty splits a string using strings.Split, but filters out empty strings func SplitNoEmpty(s string, sep string) []string { res := make([]string, 0) diff --git a/util/util_test.go b/util/util_test.go index 58abe429..807d5997 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -26,20 +26,20 @@ func TestFileExists(t *testing.T) { func TestInStringList(t *testing.T) { s := []string{"one", "two"} - require.True(t, InStringList(s, "two")) - require.False(t, InStringList(s, "three")) + require.True(t, Contains(s, "two")) + require.False(t, Contains(s, "three")) } func TestInStringListAll(t *testing.T) { s := []string{"one", "two", "three", "four"} - require.True(t, InStringListAll(s, []string{"two", "four"})) - require.False(t, InStringListAll(s, []string{"three", "five"})) + require.True(t, ContainsAll(s, []string{"two", "four"})) + require.False(t, ContainsAll(s, []string{"three", "five"})) } -func TestInIntList(t *testing.T) { +func TestContains(t *testing.T) { s := []int{1, 2} - require.True(t, InIntList(s, 2)) - require.False(t, InIntList(s, 3)) + require.True(t, Contains(s, 2)) + require.False(t, Contains(s, 3)) } func TestSplitNoEmpty(t *testing.T) {