Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement GetReactionsByTarget RPC #211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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