Skip to content

Commit

Permalink
feat: search public interests
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Jul 1, 2024
1 parent e20d214 commit 9009e2d
Show file tree
Hide file tree
Showing 20 changed files with 867 additions and 236 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21.0
go-version: 1.22

- name: Install Protoc
uses: arduino/setup-protoc@v1
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COVERAGE_TMP_FILE_NAME=cover.tmp

proto:
go install github.com/golang/protobuf/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2.0
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.4.0
PATH=${PATH}:~/go/bin protoc --go_out=plugins=grpc:. --go_opt=paths=source_relative \
api/grpc/limits/*.proto \
api/grpc/permits/*.proto \
Expand Down
6 changes: 3 additions & 3 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Client interface {
DeleteSubscription(ctx context.Context, userId, subId string) (err error)

// SearchSubscriptions returns all subscription ids those have the requested user id.
SearchSubscriptions(ctx context.Context, userId string, limit uint32, cursor string) (ids []string, err error)
SearchSubscriptions(ctx context.Context, userId string, q subscription.Query, cursor subscription.Cursor) (ids []string, err error)
}

type client struct {
Expand Down Expand Up @@ -173,11 +173,11 @@ func (c client) DeleteSubscription(ctx context.Context, userId, subId string) (e
return
}

func (c client) SearchSubscriptions(ctx context.Context, userId string, limit uint32, cursor string) (ids []string, err error) {
func (c client) SearchSubscriptions(ctx context.Context, userId string, q subscription.Query, cursor subscription.Cursor) (ids []string, err error) {
if c.svcSubs == nil {
err = fmt.Errorf("%w: SearchSubscriptions(...)", ErrApiDisabled)
} else {
ids, err = c.svcSubs.SearchOwn(ctx, userId, limit, cursor)
ids, err = c.svcSubs.Search(ctx, userId, q, cursor)
}
return
}
4 changes: 3 additions & 1 deletion api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,9 @@ func TestClient_SearchSubscriptions(t *testing.T) {
svcSubs: c.svcSubs,
}
ctx := context.TODO()
ids, err := cl.SearchSubscriptions(ctx, "user0", 0, c.cursor)
ids, err := cl.SearchSubscriptions(ctx, "user0", subscription.Query{}, subscription.Cursor{
Id: c.cursor,
})
assert.Equal(t, c.ids, ids)
assert.ErrorIs(t, err, c.err)
assert.Nil(t, cl.Close())
Expand Down
6 changes: 3 additions & 3 deletions api/grpc/limits/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions api/grpc/permits/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/grpc/reader/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/grpc/resolver/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/grpc/subject/subject.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions api/grpc/subscriptions/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (cm clientMock) Read(ctx context.Context, req *ReadRequest, opts ...grpc.Ca
resp.Description = "subscription"
resp.Enabled = true
resp.Expires = timestamppb.New(time.Date(2023, 10, 4, 11, 44, 55, 0, time.UTC))
resp.Created = timestamppb.New(time.Date(2023, 10, 4, 11, 44, 54, 0, time.UTC))
resp.Updated = timestamppb.New(time.Date(2023, 10, 4, 11, 44, 53, 0, time.UTC))
resp.Public = true
resp.Followers = 42
resp.Cond = &Condition{
Cond: &Condition_Gc{
Gc: &GroupCondition{
Expand Down Expand Up @@ -119,3 +123,19 @@ func (cm clientMock) SearchOwn(ctx context.Context, req *SearchOwnRequest, opts
}
return
}

func (cm clientMock) Search(ctx context.Context, req *SearchRequest, opts ...grpc.CallOption) (resp *SearchResponse, err error) {
resp = &SearchResponse{}
switch req.Cursor.Id {
case "":
resp.Ids = []string{
"sub0",
"sub1",
}
case "fail":
err = status.Error(codes.Internal, "internal failure")
case "fail_auth":
err = status.Error(codes.Unauthenticated, "authentication failure")
}
return
}
53 changes: 42 additions & 11 deletions api/grpc/subscriptions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type Service interface {
// Returns ErrNotFound if a subscription with the specified id is missing.
Delete(ctx context.Context, userId, subId string) (err error)

// SearchOwn returns all subscription ids those have the requested user id.
SearchOwn(ctx context.Context, userId string, limit uint32, cursor string) (ids []string, err error)
// Search returns all subscription ids matching the query.
Search(ctx context.Context, userId string, q subscription.Query, cursor subscription.Cursor) (ids []string, err error)
}

type service struct {
Expand Down Expand Up @@ -60,6 +60,7 @@ func (svc service) Create(ctx context.Context, userId string, subData subscripti
Description: subData.Description,
Enabled: subData.Enabled,
Expires: timestamppb.New(subData.Expires.UTC()),
Public: subData.Public,
}
var resp *CreateResponse
resp, err = svc.client.Create(ctx, &req)
Expand All @@ -85,6 +86,14 @@ func (svc service) Read(ctx context.Context, userId, subId string) (subData subs
if resp.Expires != nil {
subData.Expires = resp.Expires.AsTime()
}
if resp.Created != nil {
subData.Created = resp.Created.AsTime()
}
if resp.Updated != nil {
subData.Created = resp.Updated.AsTime()
}
subData.Public = resp.Public
subData.Followers = resp.Followers
}
return
}
Expand All @@ -101,6 +110,7 @@ func (svc service) Update(ctx context.Context, userId, subId string, data subscr
Enabled: data.Enabled,
Expires: timestamppb.New(data.Expires.UTC()),
Cond: encodeCondition(data.Condition),
Public: data.Public,
}
_, err = svc.client.Update(ctx, &req)
err = decodeError(err)
Expand All @@ -118,18 +128,39 @@ func (svc service) Delete(ctx context.Context, userId, subId string) (err error)
return
}

func (svc service) SearchOwn(ctx context.Context, userId string, limit uint32, cursor string) (ids []string, err error) {
func (svc service) Search(ctx context.Context, userId string, q subscription.Query, cursor subscription.Cursor) (ids []string, err error) {
ctx = auth.SetOutgoingAuthInfo(ctx, userId)
req := SearchOwnRequest{
Cursor: cursor,
Limit: limit,
switch q.Public {
case true:
req := SearchRequest{
Cursor: &Cursor{
Followers: cursor.Followers,
Id: cursor.Id,
},
Limit: q.Limit,
Order: Order(q.Order),
Pattern: q.Pattern,
Sort: Sort(q.Sort),
}
var resp *SearchResponse
resp, err = svc.client.Search(ctx, &req)
if resp != nil {
ids = resp.Ids
}
default:
req := SearchOwnRequest{
Cursor: cursor.Id,
Limit: q.Limit,
Order: Order(q.Order),
Pattern: q.Pattern,
}
var resp *SearchOwnResponse
resp, err = svc.client.SearchOwn(ctx, &req)
if resp != nil {
ids = resp.Ids
}
}
var resp *SearchOwnResponse
resp, err = svc.client.SearchOwn(ctx, &req)
err = decodeError(err)
if err == nil {
ids = resp.Ids
}
return
}

Expand Down
Loading

0 comments on commit 9009e2d

Please sign in to comment.