From 17dd3509e094471f1a476ed5a3a05d50c1ead244 Mon Sep 17 00:00:00 2001 From: qvkare Date: Mon, 13 Jan 2025 13:40:04 +0300 Subject: [PATCH] feat: Implement GetReactionsByTarget RPC --- src/network/rpc_extensions.rs | 9 ++++++- src/network/server.rs | 44 ++++++++++++++++++++++++++++++++++- src/proto/rpc.proto | 2 +- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/network/rpc_extensions.rs b/src/network/rpc_extensions.rs index c3da8cd..d18466f 100644 --- a/src/network/rpc_extensions.rs +++ b/src/network/rpc_extensions.rs @@ -1,7 +1,8 @@ use crate::core::error::HubError; use crate::proto; use crate::proto::{ - CastsByParentRequest, FidRequest, FidTimestampRequest, LinksByFidRequest, ReactionsByFidRequest, + CastsByParentRequest, FidRequest, FidTimestampRequest, LinksByFidRequest, + ReactionsByFidRequest, ReactionsByTargetRequest, }; use crate::storage::db::PageOptions; use crate::storage::store::account::MessagesPage; @@ -106,6 +107,12 @@ impl ReactionsByFidRequest { } } +impl ReactionsByTargetRequest { + pub fn page_options(&self) -> PageOptions { + page_options(self.page_size, self.page_token.clone(), self.reverse) + } +} + impl LinksByFidRequest { pub fn page_options(&self) -> PageOptions { page_options(self.page_size, self.page_token.clone(), self.reverse) diff --git a/src/network/server.rs b/src/network/server.rs index 32dbe7c..5647748 100644 --- a/src/network/server.rs +++ b/src/network/server.rs @@ -17,7 +17,7 @@ use crate::proto::{FidRequest, FidTimestampRequest}; use crate::proto::{GetInfoRequest, StorageLimitsResponse}; use crate::proto::{ LinkRequest, LinksByFidRequest, Message, MessagesResponse, ReactionRequest, - ReactionsByFidRequest, UserDataRequest, VerificationRequest, + ReactionsByFidRequest, ReactionsByTargetRequest, UserDataRequest, VerificationRequest, }; use crate::storage::constants::OnChainEventPostfix; use crate::storage::constants::RootPrefix; @@ -265,6 +265,41 @@ impl MyHubService { )), } } + + async fn get_reactions_by_target( + &self, + request: Request, + ) -> Result, Status> { + let request = request.into_inner(); + let target = match request.target { + Some(proto::reactions_by_target_request::Target::TargetCastId(ref cast_id)) => { + proto::reaction_body::Target::TargetCastId(cast_id.clone()) + } + Some(proto::reactions_by_target_request::Target::TargetUrl(ref url)) => { + proto::reaction_body::Target::TargetUrl(url.clone()) + } + None => return Err(Status::invalid_argument("missing target")), + }; + + // Get the shard ID from the target cast ID if available + let shard_id = match &target { + proto::reaction_body::Target::TargetCastId(cast_id) => self + .message_router + .route_message(cast_id.fid, self.num_shards), + _ => 0, // Default to shard 0 for URL targets + }; + + let stores = self.get_stores_for_shard(shard_id)?; + let options = request.page_options(); + + ReactionStore::get_reactions_by_target( + &stores.reaction_store, + &target, + request.reaction_type.unwrap_or(0), + &options, + ) + .as_response() + } } #[tonic::async_trait] @@ -808,4 +843,11 @@ impl HubService for MyHubService { children, })) } + + async fn get_reactions_by_target( + &self, + request: Request, + ) -> Result, Status> { + self.get_reactions_by_target(request).await + } } diff --git a/src/proto/rpc.proto b/src/proto/rpc.proto index 41e48b7..2ffafe8 100644 --- a/src/proto/rpc.proto +++ b/src/proto/rpc.proto @@ -36,7 +36,7 @@ service HubService { rpc GetReaction(ReactionRequest) returns (Message); rpc GetReactionsByFid(ReactionsByFidRequest) returns (MessagesResponse); // rpc GetReactionsByCast(ReactionsByTargetRequest) returns (MessagesResponse); // To be deprecated -// rpc GetReactionsByTarget(ReactionsByTargetRequest) returns (MessagesResponse); + rpc GetReactionsByTarget(ReactionsByTargetRequest) returns (MessagesResponse); // // // User Data rpc GetUserData(UserDataRequest) returns (Message);