Skip to content

Commit

Permalink
feat: Implement GetReactionsByTarget RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
qvkare committed Jan 13, 2025
1 parent 8cba96e commit 17dd350
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/network/rpc_extensions.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down
44 changes: 43 additions & 1 deletion src/network/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -265,6 +265,41 @@ impl MyHubService {
)),
}
}

async fn get_reactions_by_target(
&self,
request: Request<ReactionsByTargetRequest>,
) -> Result<Response<MessagesResponse>, 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]
Expand Down Expand Up @@ -808,4 +843,11 @@ impl HubService for MyHubService {
children,
}))
}

async fn get_reactions_by_target(
&self,
request: Request<ReactionsByTargetRequest>,
) -> Result<Response<MessagesResponse>, Status> {
self.get_reactions_by_target(request).await
}
}
2 changes: 1 addition & 1 deletion src/proto/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 17dd350

Please sign in to comment.