Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
wip(backend): adding trusted filter
Browse files Browse the repository at this point in the history
  • Loading branch information
juligasa committed Feb 8, 2024
1 parent b2c213b commit 1847bdc
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 56 deletions.
39 changes: 30 additions & 9 deletions backend/daemon/api/activity/v1alpha/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"mintter/backend/pkg/dqb"
"mintter/backend/pkg/future"
"strconv"
sync "sync"
"time"

"github.com/ipfs/go-cid"
Expand All @@ -28,9 +27,9 @@ type Repo interface {

// Server implements the Activity gRPC API.
type Server struct {
db *sqlitex.Pool
startTime time.Time
mu sync.Mutex // we only want one register request at a time.
db *sqlitex.Pool
startTime time.Time
NextPageToken string
}

// NewServer creates a new Server.
Expand All @@ -51,6 +50,7 @@ func (srv *Server) ListEvents(ctx context.Context, req *activity.ListEventsReque
return nil, fmt.Errorf("Token not valid: %w", err)
}
}
fmt.Println(token)
conn, cancel, err := srv.db.Conn(ctx)
if err != nil {
return nil, err
Expand All @@ -59,15 +59,28 @@ func (srv *Server) ListEvents(ctx context.Context, req *activity.ListEventsReque

var events []*activity.Event

var qGetEvents = dqb.Str(`
var qGetEventsTrusted = dqb.Str(`
SELECT structural_blobs.type ,public_keys.principal, resources.iri, structural_blobs.ts, blobs.insert_time, blobs.multihash, blobs.codec
FROM structural_blobs
JOIN blobs ON blobs.id=structural_blobs.id
JOIN public_keys ON structural_blobs.author=public_keys.id
JOIN resources ON structural_blobs.resource=resources.id
WHERE structural_blobs.id >= ?;
JOIN trusted_accounts ON trusted_accounts.id=public_keys.id
ORDER BY blobs.id desc limit ?;
`)
err = sqlitex.Exec(conn, qGetEvents(), func(stmt *sqlite.Stmt) error {
var qGetEventsAll = dqb.Str(`
SELECT structural_blobs.type ,public_keys.principal, resources.iri, structural_blobs.ts, blobs.insert_time, blobs.multihash, blobs.codec
FROM structural_blobs
JOIN blobs ON blobs.id=structural_blobs.id
JOIN public_keys ON structural_blobs.author=public_keys.id
JOIN resources ON structural_blobs.resource=resources.id
ORDER BY blobs.id desc limit ?;
`)
query := qGetEventsAll()
if req.TrustedOnly {
query = qGetEventsTrusted()
}
err = sqlitex.Exec(conn, query, func(stmt *sqlite.Stmt) error {
eventType := stmt.ColumnText(0)
author := stmt.ColumnBytes(1)
resource := stmt.ColumnText(2)
Expand All @@ -91,10 +104,18 @@ func (srv *Server) ListEvents(ctx context.Context, req *activity.ListEventsReque
}
events = append(events, &event)
return nil
}, token)
}, req.PageSize)
var pageSize int32 = 20
if req.PageSize != 0 {
pageSize = req.PageSize
}
var nextToken string
if pageSize < int32(len(events)) {
nextToken = strconv.Itoa(len(events))
}
return &activity.ListEventsResponse{
Events: events,
NextPageToken: "",
NextPageToken: nextToken,
}, err

}

Check failure on line 121 in backend/daemon/api/activity/v1alpha/activity.go

View workflow job for this annotation

GitHub Actions / lint-go

unnecessary trailing newline (whitespace)
1 change: 1 addition & 0 deletions backend/daemon/api/activity/v1alpha/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestListEvents(t *testing.T) {
events, err := alice.ListEvents(ctx, req)
require.NoError(t, err)
require.NotNil(t, events)
require.Len(t, events.Events, 0)
}

// TODO: update profile idempotent no change
Expand Down
95 changes: 53 additions & 42 deletions backend/genproto/activity/v1alpha/activity.pb.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export class ListEventsRequest extends Message<ListEventsRequest> {
*/
pageToken = "";

/**
* Optional. If we want events from trusted peers only. All peers by default.
*
* @generated from field: bool trusted_only = 3;
*/
trustedOnly = false;

constructor(data?: PartialMessage<ListEventsRequest>) {
super();
proto3.util.initPartial(data, this);
Expand All @@ -36,6 +43,7 @@ export class ListEventsRequest extends Message<ListEventsRequest> {
static readonly fields: FieldList = proto3.util.newFieldList(() => [
{ no: 1, name: "page_size", kind: "scalar", T: 5 /* ScalarType.INT32 */ },
{ no: 2, name: "page_token", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 3, name: "trusted_only", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
]);

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): ListEventsRequest {
Expand Down
4 changes: 3 additions & 1 deletion proto/activity/v1alpha/activity.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ message ListEventsRequest {
// Optional. The page token for requesting next pages.
string page_token = 2;

// TODO: add filtering options.
// Optional. If we want events from trusted peers only. All peers by default.
bool trusted_only = 3;

}

// The response with the list of events.
Expand Down
4 changes: 2 additions & 2 deletions proto/activity/v1alpha/go.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: 67073275389dafc231a09fc47630ec57
outs: 89ed2f89f62bf355718fabc3f8d15d2d
srcs: 08c7670fc311b065826a85e61a2dc9a0
outs: f7394d08dcc87852df0deeb0dd8cfdc4
4 changes: 2 additions & 2 deletions proto/activity/v1alpha/js.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: 67073275389dafc231a09fc47630ec57
outs: ff6e3daacd6da650a65cce6d39dc5dec
srcs: 08c7670fc311b065826a85e61a2dc9a0
outs: 7eb64f85ef272bfadf32da03a366cdc7

0 comments on commit 1847bdc

Please sign in to comment.