mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: scope combined chat source filters (#26137)
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -611,22 +612,29 @@ func Chats(query string) (database.GetChatsParams, []codersdk.ValidationError) {
|
||||
filter.TitleQuery = parser.String(values, "", "title")
|
||||
filter.PrTitleQuery = parser.String(values, "", "pr_title")
|
||||
filter.RepoQuery = parser.String(values, "", "repo")
|
||||
if source := parser.String(values, "", "source"); source != "" {
|
||||
sources := httpapi.ParseCustomList(parser, values, nil, "source", func(v string) (string, error) {
|
||||
source := strings.ToLower(strings.TrimSpace(v))
|
||||
switch source {
|
||||
case "created_by_me":
|
||||
case "created_by_me", "shared_with_me":
|
||||
return source, nil
|
||||
default:
|
||||
return "", xerrors.Errorf("%q is not a valid value", v)
|
||||
}
|
||||
})
|
||||
if len(sources) > 0 {
|
||||
hasCreatedByMe := slices.Contains(sources, "created_by_me")
|
||||
hasSharedWithMe := slices.Contains(sources, "shared_with_me")
|
||||
|
||||
switch {
|
||||
case hasCreatedByMe && hasSharedWithMe:
|
||||
filter.OwnedOnly = true
|
||||
filter.SharedOnly = false
|
||||
case "shared_with_me":
|
||||
filter.SharedOnly = true
|
||||
case hasSharedWithMe:
|
||||
filter.OwnedOnly = false
|
||||
filter.SharedOnly = true
|
||||
case "all":
|
||||
filter.OwnedOnly = false
|
||||
filter.SharedOnly = false
|
||||
default:
|
||||
parser.Errors = append(parser.Errors, codersdk.ValidationError{
|
||||
Field: "source",
|
||||
Detail: fmt.Sprintf("%q is not a valid value", source),
|
||||
})
|
||||
filter.OwnedOnly = true
|
||||
filter.SharedOnly = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1378,11 +1378,9 @@ func TestSearchChats(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "SourceAll",
|
||||
Query: "source:all",
|
||||
Expected: database.GetChatsParams{
|
||||
Archived: sql.NullBool{Bool: false, Valid: true},
|
||||
},
|
||||
Name: "SourceAllInvalid",
|
||||
Query: "source:all",
|
||||
ExpectedErrorContains: "source",
|
||||
},
|
||||
{
|
||||
Name: "SourceInvalid",
|
||||
@@ -1390,9 +1388,22 @@ func TestSearchChats(t *testing.T) {
|
||||
ExpectedErrorContains: "source",
|
||||
},
|
||||
{
|
||||
Name: "SourceRepeated",
|
||||
Query: "source:created_by_me source:shared_with_me",
|
||||
ExpectedErrorContains: "source",
|
||||
Name: "SourceCreatedByMeAndSharedWithMe",
|
||||
Query: "source:created_by_me,shared_with_me",
|
||||
Expected: database.GetChatsParams{
|
||||
Archived: sql.NullBool{Bool: false, Valid: true},
|
||||
OwnedOnly: true,
|
||||
SharedOnly: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "SourceRepeated",
|
||||
Query: "source:created_by_me source:shared_with_me",
|
||||
Expected: database.GetChatsParams{
|
||||
Archived: sql.NullBool{Bool: false, Valid: true},
|
||||
OwnedOnly: true,
|
||||
SharedOnly: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "ExtraParam",
|
||||
|
||||
Reference in New Issue
Block a user