Skip to content

Commit

Permalink
feat: return snap name in vote data
Browse files Browse the repository at this point in the history
  • Loading branch information
d-loose committed Dec 12, 2024
1 parent 2b74d7d commit 2a2b964
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions proto/ratings_features_user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ message Vote {
int32 snap_revision = 2;
bool vote_up = 3;
google.protobuf.Timestamp timestamp = 4;
string snap_name = 5;
}

message VoteRequest {
Expand Down
21 changes: 17 additions & 4 deletions src/grpc/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use crate::{
AuthenticateRequest, AuthenticateResponse, GetSnapVotesRequest, GetSnapVotesResponse,
Vote as PbVote, VoteRequest,
},
ratings::update_categories,
ratings::{get_snap_name, update_categories, Error},
Context,
};
use futures::future::try_join_all;
use std::sync::Arc;
use time::OffsetDateTime;
use tonic::{Request, Response, Status};
Expand Down Expand Up @@ -120,7 +121,18 @@ impl user_server::User for UserService {

match Vote::get_all_by_client_hash(&client_hash, Some(snap_id), conn).await {
Ok(votes) => {
let votes = votes.into_iter().map(|vote| vote.into()).collect();
let votes = try_join_all(votes.into_iter().map(|vote| async {
let snap_name = get_snap_name(
&vote.snap_id,
&self.ctx.config.snapcraft_io_uri,
&self.ctx.http_client,
)
.await?;

Result::<PbVote, Error>::Ok(PbVote::from_vote_and_snap_name(vote, &snap_name))
}))
.await
.map_err(|_| Status::unknown("Internal server error"))?;
let payload = GetSnapVotesResponse { votes };

Ok(Response::new(payload))
Expand All @@ -134,8 +146,8 @@ impl user_server::User for UserService {
}
}

impl From<Vote> for PbVote {
fn from(value: Vote) -> Self {
impl PbVote {
fn from_vote_and_snap_name(value: Vote, snap_name: &str) -> Self {
let timestamp = Some(prost_types::Timestamp {
seconds: value.timestamp.unix_timestamp(),
nanos: value.timestamp.nanosecond() as i32,
Expand All @@ -146,6 +158,7 @@ impl From<Vote> for PbVote {
snap_revision: value.snap_revision as i32,
vote_up: value.vote_up,
timestamp,
snap_name: snap_name.into(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/proto/ratings.features.user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub struct Vote {
pub vote_up: bool,
#[prost(message, optional, tag = "4")]
pub timestamp: ::core::option::Option<::prost_types::Timestamp>,
#[prost(string, tag = "5")]
pub snap_name: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down

0 comments on commit 2a2b964

Please sign in to comment.