diff --git a/internal/database/dialer/postgres/client_partitioned.go b/internal/database/dialer/postgres/client_partitioned.go index f8f724e7..30af2440 100644 --- a/internal/database/dialer/postgres/client_partitioned.go +++ b/internal/database/dialer/postgres/client_partitioned.go @@ -2,6 +2,7 @@ package postgres import ( "context" + "encoding/json" "errors" "fmt" "math" @@ -12,7 +13,6 @@ import ( "github.com/rss3-network/node/internal/database/dialer/postgres/table" "github.com/rss3-network/node/internal/database/model" activityx "github.com/rss3-network/protocol-go/schema/activity" - "github.com/rss3-network/protocol-go/schema/metadata" "github.com/rss3-network/protocol-go/schema/network" "github.com/samber/lo" "github.com/sourcegraph/conc/pool" @@ -1043,32 +1043,19 @@ func (c *client) buildFindActivitiesStatement(ctx context.Context, partitionedNa } if query.Metadata != nil { - databaseStatement = c.buildFindActivitiesMetadataStatement(ctx, databaseStatement, lo.FromPtr(query.Metadata)) - } - - return databaseStatement.Order("timestamp DESC, index DESC").Limit(query.Limit) -} - -// buildFindActivitiesMetadataStatement builds the query metadata statement. -func (c *client) buildFindActivitiesMetadataStatement(_ context.Context, databaseStatement *gorm.DB, meta metadata.Metadata) *gorm.DB { - statement := databaseStatement.Session(&gorm.Session{}) - - statement = statement.Joins("CROSS JOIN LATERAL jsonb_array_elements(actions::jsonb) AS action_element") - - switch data := meta.(type) { - case *metadata.ExchangeSwap: - if data.From.Address != nil { - statement = statement.Where("action_element -> 'metadata' -> 'from' ->> 'address' = ?", data.From.Address) - } - - if data.To.Address != nil { - statement = statement.Where("action_element -> 'metadata' -> 'to' ->> 'address' = ?", data.To.Address) + metadataSQL, err := json.Marshal([]map[string]interface{}{ + { + "metadata": query.Metadata, + }, + }) + if err != nil { + zap.L().Error("failed to marshal metadata", zap.Error(err), zap.Any("metadata", query.Metadata)) + } else { + databaseStatement = databaseStatement.Where("actions::jsonb @> ?", string(metadataSQL)) } - default: - return databaseStatement } - return statement + return databaseStatement.Order("timestamp DESC, index DESC").Limit(query.Limit) } // buildActivitiesTableNames builds the activities table names. diff --git a/internal/node/component/decentralized/handler_metadata.go b/internal/node/component/decentralized/handler_metadata.go index 0d319529..6729f09c 100644 --- a/internal/node/component/decentralized/handler_metadata.go +++ b/internal/node/component/decentralized/handler_metadata.go @@ -41,6 +41,17 @@ func (c *Component) BatchGetMetadataActivities(ctx echo.Context) (err error) { request.Type = lo.ToPtr(typex) + for i := range request.Accounts { + if common.IsHexAddress(request.Accounts[i]) { + request.Accounts[i] = common.HexToAddress(request.Accounts[i]).String() + } + } + + // Parse the metadata + if request.RawMetadata == nil || len(request.RawMetadata) == 0 || string(request.RawMetadata) == "{}" { + return response.BadRequestError(ctx, fmt.Errorf("empty metadata")) + } + meta, err := metadata.Unmarshal(typex, request.RawMetadata) if err != nil { return response.BadRequestError(ctx, fmt.Errorf("failed to unmarshal metadata: %w", err)) @@ -48,12 +59,6 @@ func (c *Component) BatchGetMetadataActivities(ctx echo.Context) (err error) { request.Metadata = lo.ToPtr(meta) - for i := range request.Accounts { - if common.IsHexAddress(request.Accounts[i]) { - request.Accounts[i] = common.HexToAddress(request.Accounts[i]).String() - } - } - if err = defaults.Set(&request); err != nil { return response.BadRequestError(ctx, err) }