-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
palomar: basic query parsing, handle 'from:'
- Loading branch information
Showing
7 changed files
with
159 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package search | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"strings" | ||
|
||
"github.com/bluesky-social/indigo/atproto/identity" | ||
"github.com/bluesky-social/indigo/atproto/syntax" | ||
|
||
"github.com/google/shlex" | ||
) | ||
|
||
// takes a query string and pulls out some facet patterns ("from:handle.net") as filters | ||
func ParseQuery(ctx context.Context, dir identity.Directory, raw string) (string, []map[string]interface{}) { | ||
var filters []map[string]interface{} | ||
parts, err := shlex.Split(raw) | ||
if err != nil { | ||
// pass-through if failed to parse | ||
return raw, filters | ||
} | ||
keep := make([]string, len(parts)) | ||
for _, p := range parts { | ||
if !strings.ContainsRune(p, ':') || strings.ContainsRune(p, ' ') { | ||
// simple: quoted (whitespace), or just a token | ||
keep = append(keep, p) | ||
continue | ||
} | ||
if strings.HasPrefix(p, "did:") { | ||
filters = append(filters, map[string]interface{}{ | ||
"term": map[string]interface{}{"did": p}, | ||
}) | ||
continue | ||
} | ||
if strings.HasPrefix(p, "from:") && len(p) > 6 { | ||
handle, err := syntax.ParseHandle(p[5:]) | ||
if err != nil { | ||
keep = append(keep, p) | ||
continue | ||
} | ||
id, err := dir.LookupHandle(ctx, handle) | ||
if err != nil { | ||
if err != identity.ErrHandleNotFound { | ||
slog.Error("failed to resolve handle", "err", err) | ||
} | ||
continue | ||
} | ||
filters = append(filters, map[string]interface{}{ | ||
"term": map[string]interface{}{"did": id.DID.String()}, | ||
}) | ||
continue | ||
} | ||
keep = append(keep, p) | ||
} | ||
|
||
out := "" | ||
for _, p := range keep { | ||
if strings.ContainsRune(p, ' ') { | ||
out += fmt.Sprintf(" \"%s\"", p) | ||
} else { | ||
if out == "" { | ||
out = p | ||
} else { | ||
out += " " + p | ||
} | ||
} | ||
} | ||
return out, filters | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package search | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/bluesky-social/indigo/atproto/identity" | ||
"github.com/bluesky-social/indigo/atproto/syntax" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseQuery(t *testing.T) { | ||
ctx := context.Background() | ||
assert := assert.New(t) | ||
dir := identity.NewMockDirectory() | ||
dir.Insert(identity.Identity{ | ||
Handle: syntax.Handle("known.example.com"), | ||
DID: syntax.DID("did:plc:abc222"), | ||
}) | ||
|
||
var q string | ||
var f []map[string]interface{} | ||
|
||
q, f = ParseQuery(ctx, &dir, "") | ||
assert.Equal("", q) | ||
assert.Empty(f) | ||
|
||
p1 := "some +test \"with phrase\" -ok" | ||
q, f = ParseQuery(ctx, &dir, p1) | ||
assert.Equal(p1, q) | ||
assert.Empty(f) | ||
|
||
p2 := "missing from:missing.example.com" | ||
q, f = ParseQuery(ctx, &dir, p2) | ||
assert.Equal("missing", q) | ||
assert.Empty(f) | ||
|
||
p3 := "known from:known.example.com" | ||
q, f = ParseQuery(ctx, &dir, p3) | ||
assert.Equal("known", q) | ||
assert.Equal(1, len(f)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters