From 3cce474bce1c2701135cab592704ce8db9970db7 Mon Sep 17 00:00:00 2001 From: brucexc <108378464+brucexc@users.noreply.github.com> Date: Fri, 18 Oct 2024 01:49:01 -0400 Subject: [PATCH] docs: federated example (#412) --- docs/openapi.json | 106 ++++++++++-------- .../service/hub/handler/dsl/decentralized.go | 20 ++++ .../handler/dsl/distributor/distributor.go | 3 +- internal/service/hub/handler/dsl/federated.go | 21 ++++ internal/service/hub/handler/dsl/rss.go | 5 + internal/service/hub/model/errorx/error.go | 3 + 6 files changed, 113 insertions(+), 45 deletions(-) diff --git a/docs/openapi.json b/docs/openapi.json index 071b8ca5..e12b523c 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -1448,85 +1448,85 @@ "type": "object", "anyOf": [ { - "$ref": "#/components/schemas/SocialPost" + "$ref": "#/components/schemas/TransactionApproval" }, { - "$ref": "#/components/schemas/SocialShare" + "$ref": "#/components/schemas/TransactionBridge" }, { - "$ref": "#/components/schemas/SocialMint" + "$ref": "#/components/schemas/TransactionTransfer" }, { - "$ref": "#/components/schemas/SocialProfile" + "$ref": "#/components/schemas/TransactionBurn" }, { - "$ref": "#/components/schemas/SocialProxy" + "$ref": "#/components/schemas/TransactionMint" }, { - "$ref": "#/components/schemas/SocialComment" + "$ref": "#/components/schemas/CollectibleApproval" }, { - "$ref": "#/components/schemas/SocialRevise" + "$ref": "#/components/schemas/CollectibleTrade" }, { - "$ref": "#/components/schemas/SocialReward" + "$ref": "#/components/schemas/CollectibleTransfer" }, { - "$ref": "#/components/schemas/SocialDelete" + "$ref": "#/components/schemas/CollectibleBurn" }, { - "$ref": "#/components/schemas/MetaverseTrade" + "$ref": "#/components/schemas/CollectibleMint" }, { - "$ref": "#/components/schemas/MetaverseBurn" + "$ref": "#/components/schemas/ExchangeStaking" }, { - "$ref": "#/components/schemas/MetaverseMint" + "$ref": "#/components/schemas/ExchangeSwap" }, { - "$ref": "#/components/schemas/MetaverseTransfer" + "$ref": "#/components/schemas/ExchangeLiquidity" }, { - "$ref": "#/components/schemas/RssFeed" + "$ref": "#/components/schemas/SocialRevise" }, { - "$ref": "#/components/schemas/TransactionMint" + "$ref": "#/components/schemas/SocialReward" }, { - "$ref": "#/components/schemas/TransactionApproval" + "$ref": "#/components/schemas/SocialPost" }, { - "$ref": "#/components/schemas/TransactionBridge" + "$ref": "#/components/schemas/SocialComment" }, { - "$ref": "#/components/schemas/TransactionTransfer" + "$ref": "#/components/schemas/SocialShare" }, { - "$ref": "#/components/schemas/TransactionBurn" + "$ref": "#/components/schemas/SocialDelete" }, { - "$ref": "#/components/schemas/CollectibleBurn" + "$ref": "#/components/schemas/SocialMint" }, { - "$ref": "#/components/schemas/CollectibleMint" + "$ref": "#/components/schemas/SocialProfile" }, { - "$ref": "#/components/schemas/CollectibleApproval" + "$ref": "#/components/schemas/SocialProxy" }, { - "$ref": "#/components/schemas/CollectibleTrade" + "$ref": "#/components/schemas/MetaverseTransfer" }, { - "$ref": "#/components/schemas/CollectibleTransfer" + "$ref": "#/components/schemas/MetaverseTrade" }, { - "$ref": "#/components/schemas/ExchangeLiquidity" + "$ref": "#/components/schemas/MetaverseBurn" }, { - "$ref": "#/components/schemas/ExchangeStaking" + "$ref": "#/components/schemas/MetaverseMint" }, { - "$ref": "#/components/schemas/ExchangeSwap" + "$ref": "#/components/schemas/RssFeed" } ] }, @@ -1637,18 +1637,36 @@ "tag": "social", "type": "comment", "platform": "Mastodon", - "from": "@thime@bolha.us", - "to": "@jedi@bolha.us", + "from": "@woofers@universeodon.com", + "to": "@georgetakei@universeodon.com", "metadata": { - "handle": "@thime@bolha.us", - "body": "
", - "profile_id": "https://bolha.us/users/thime", - "publication_id": "https://bolha.us/users/thime/statuses/113221776800523794", - "timestamp": 1727627209, + "handle": "@woofers@universeodon.com", + "body": "@ georgetakei ( https://universeodon.com/@georgetakei ) now hold on just a minute...\n\n# woof ( https://universeodon.com/tags/woof )", + "publication_id": "113324128515491294", + "tags": [ + "@georgetakei", + "#woof" + ], + "author_url": "https://universeodon.com/users/woofers", + "timestamp": 1729188972, "target": { - "publication_id": "https://bolha.us/users/jedi/statuses/113221045780040329" - } - } + "handle": "@georgetakei@universeodon.com", + "body": "Just a little treat.", + "media": [ + { + "address": "https://media.universeodon.com/media_attachments/files/113/319/346/314/075/495/original/26e989bcf15840a6.png", + "mime_type": "image/png" + } + ], + "publication_id": "113319346448373294", + "author_url": "https://universeodon.com/users/georgetakei", + "timestamp": 1729116004 + }, + "target_url": "https://universeodon.com/users/georgetakei/statuses/113319346448373294" + }, + "related_urls": [ + "https://universeodon.com/users/woofers/statuses/113324128515491294" + ] } }, "ActivitiesResponse": { @@ -1778,12 +1796,12 @@ "id": { "description": "The unique identifier for the federated activity.", "type": "string", - "example": "https://bolha.us/users/thime/statuses/113221776800523794/activity " + "example": "https://universeodon.com/users/woofers/statuses/113324128515491294/activity" }, "owner": { "description": "The owner of the federated activity.", "type": "string", - "example": "admin@kooapp.org" + "example": "@georgetakei@universeodon.com" }, "network": { "description": "The network of the federated activity.", @@ -1793,12 +1811,12 @@ "from": { "description": "The account from which the federated activity originated.", "type": "string", - "example": "@thime@bolha.us" + "example": "@woofers@universeodon.com" }, "to": { "description": "The account to which the activity is directed.", "type": "string", - "example": "@jedi@bolha.us" + "example": "@georgetakei@universeodon.com" }, "index": { "description": "The index of the activity in the list.", @@ -1823,7 +1841,7 @@ "type": { "description": "The type of federated activity.", "type": "string", - "example": "post", + "example": "comment", "enum": [ "comment", "post", @@ -1836,7 +1854,7 @@ "timestamp": { "description": "The timestamp of when the activity occurred.", "type": "integer", - "example": 1718689727 + "example": 1729188972 }, "total_actions": { "description": "The total number of actions within the activity.", @@ -5556,7 +5574,7 @@ }, "federated_activity_id_path": { "description": "Retrieve details for the specified federated activity ID", - "example": "https://bolha.us/users/thime/statuses/113221776800523794/activity", + "example": "https://universeodon.com/users/woofers/statuses/113324128515491294/activity", "in": "path", "name": "id", "required": true, diff --git a/internal/service/hub/handler/dsl/decentralized.go b/internal/service/hub/handler/dsl/decentralized.go index 1eae235c..f32f8214 100644 --- a/internal/service/hub/handler/dsl/decentralized.go +++ b/internal/service/hub/handler/dsl/decentralized.go @@ -43,6 +43,10 @@ func (d *DSL) GetDecentralizedActivity(c echo.Context) (err error) { activity, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestActivity, model.ComponentDecentralized, request, c.QueryParams(), nil, nil) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute activity request error", zap.Error(err)) return errorx.InternalError(c) @@ -87,6 +91,10 @@ func (d *DSL) GetDecentralizedAccountActivities(c echo.Context) (err error) { activities, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestAccountActivities, model.ComponentDecentralized, request, c.QueryParams(), workers, networks) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute activities data error", zap.Error(err)) return errorx.InternalError(c) @@ -130,6 +138,10 @@ func (d *DSL) BatchGetDecentralizedAccountsActivities(c echo.Context) (err error activities, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestBatchAccountActivities, model.ComponentDecentralized, request, nil, workers, networks) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute batch activities data error", zap.Error(err)) return errorx.InternalError(c) @@ -166,6 +178,10 @@ func (d *DSL) GetDecentralizedNetworkActivities(c echo.Context) (err error) { activities, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestNetworkActivities, model.ComponentDecentralized, request, c.QueryParams(), workers, networks) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute network activities data error", zap.Error(err)) return errorx.InternalError(c) @@ -202,6 +218,10 @@ func (d *DSL) GetDecentralizedPlatformActivities(c echo.Context) (err error) { activities, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestPlatformActivities, model.ComponentDecentralized, request, c.QueryParams(), workers, networks) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute platform activities data error", zap.Error(err)) return errorx.InternalError(c) diff --git a/internal/service/hub/handler/dsl/distributor/distributor.go b/internal/service/hub/handler/dsl/distributor/distributor.go index 57723e0c..1ff1bc77 100644 --- a/internal/service/hub/handler/dsl/distributor/distributor.go +++ b/internal/service/hub/handler/dsl/distributor/distributor.go @@ -20,6 +20,7 @@ import ( "github.com/rss3-network/global-indexer/internal/service/hub/handler/dsl/model" "github.com/rss3-network/global-indexer/internal/service/hub/handler/dsl/router" "github.com/rss3-network/global-indexer/internal/service/hub/model/dsl" + "github.com/rss3-network/global-indexer/internal/service/hub/model/errorx" "go.uber.org/zap" ) @@ -85,7 +86,7 @@ func (d *Distributor) DistributeData(ctx context.Context, requestType, component } if len(nodes) == 0 { - return nil, fmt.Errorf("no nodes available") + return nil, errorx.ErrNoNodesAvailable } nodeMap, err := d.generatePath(requestType, component, request, params, nodes) diff --git a/internal/service/hub/handler/dsl/federated.go b/internal/service/hub/handler/dsl/federated.go index f7a11155..74384219 100644 --- a/internal/service/hub/handler/dsl/federated.go +++ b/internal/service/hub/handler/dsl/federated.go @@ -1,6 +1,7 @@ package dsl import ( + "errors" "net/http" "github.com/creasty/defaults" @@ -31,6 +32,10 @@ func (d *DSL) GetFederatedActivity(c echo.Context) (err error) { activity, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestActivity, model.ComponentFederated, request, c.QueryParams(), nil, nil) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute activity request error", zap.Error(err)) return errorx.InternalError(c) @@ -62,6 +67,10 @@ func (d *DSL) GetFederatedAccountActivities(c echo.Context) (err error) { activities, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestAccountActivities, model.ComponentFederated, request, c.QueryParams(), nil, nil) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute activities data error", zap.Error(err)) return errorx.InternalError(c) @@ -96,6 +105,10 @@ func (d *DSL) BatchGetFederatedAccountsActivities(c echo.Context) (err error) { activities, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestBatchAccountActivities, model.ComponentFederated, request, nil, nil, request.Network) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute batch activities data error", zap.Error(err)) return errorx.InternalError(c) @@ -127,6 +140,10 @@ func (d *DSL) GetFederatedNetworkActivities(c echo.Context) (err error) { activities, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestNetworkActivities, model.ComponentFederated, request, c.QueryParams(), nil, []string{request.Network}) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute network activities data error", zap.Error(err)) return errorx.InternalError(c) @@ -158,6 +175,10 @@ func (d *DSL) GetFederatedPlatformActivities(c echo.Context) (err error) { activities, err := d.distributor.DistributeData(c.Request().Context(), model.DistributorRequestPlatformActivities, model.ComponentFederated, request, c.QueryParams(), nil, request.Network) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute platform activities data error", zap.Error(err)) return errorx.InternalError(c) diff --git a/internal/service/hub/handler/dsl/rss.go b/internal/service/hub/handler/dsl/rss.go index 4d506455..ef2539fa 100644 --- a/internal/service/hub/handler/dsl/rss.go +++ b/internal/service/hub/handler/dsl/rss.go @@ -1,6 +1,7 @@ package dsl import ( + "errors" "net/http" "github.com/labstack/echo/v4" @@ -17,6 +18,10 @@ func (d *DSL) GetRSSHub(c echo.Context) error { data, err := d.distributor.DistributeRSSHubData(c.Request().Context(), path, query) if err != nil { + if errors.Is(err, errorx.ErrNoNodesAvailable) { + return errorx.BadRequestError(c, err) + } + zap.L().Error("distribute rss hub data error", zap.Error(err)) return errorx.InternalError(c) diff --git a/internal/service/hub/model/errorx/error.go b/internal/service/hub/model/errorx/error.go index 0a9fa5d7..7b696fad 100644 --- a/internal/service/hub/model/errorx/error.go +++ b/internal/service/hub/model/errorx/error.go @@ -1,6 +1,7 @@ package errorx import ( + "errors" "fmt" "net/http" @@ -17,6 +18,8 @@ const ( ErrorCodeInternalError ) +var ErrNoNodesAvailable = errors.New("no nodes currently support this request in the network. Please wait for node data support or try again later") + type ErrorResponse struct { Error string `json:"error"` ErrorCode ErrorCode `json:"error_code"`