From d4a041129b84fdec849d02f425f7e01669c55f92 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Tue, 1 Jun 2021 20:21:45 +0000 Subject: [PATCH] Implement get variable --- dicebot/src/bin/tonic_client.rs | 12 +++++++----- dicebot/src/rpc.rs | 26 ++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/dicebot/src/bin/tonic_client.rs b/dicebot/src/bin/tonic_client.rs index 5bec6ab..d049977 100644 --- a/dicebot/src/bin/tonic_client.rs +++ b/dicebot/src/bin/tonic_client.rs @@ -1,18 +1,20 @@ -use tenebrous_rpc::protos::dicebot::dicebot_client::DicebotClient; use tenebrous_rpc::protos::dicebot::UserIdRequest; +use tenebrous_rpc::protos::dicebot::{dicebot_client::DicebotClient, GetVariableRequest}; #[tokio::main] async fn main() -> Result<(), Box> { let mut client = DicebotClient::connect("http://0.0.0.0:9090").await?; - let request = tonic::Request::new(UserIdRequest { - user_id: "Tonic".into(), + let request = tonic::Request::new(GetVariableRequest { + user_id: "@projectmoon:agnos.is".into(), + room_id: "!agICWvldGfuCywUVUM:agnos.is".into(), + variable_name: "stuff".into(), }); - let response = client.rooms_for_user(request).await?.into_inner(); + let response = client.get_variable(request).await?.into_inner(); println!("RESPONSE={:?}", response); - println!("User friendly response is: {:?}", response.room_ids); + println!("User friendly response is: {:?}", response.value); Ok(()) } diff --git a/dicebot/src/rpc.rs b/dicebot/src/rpc.rs index bb59f9f..58373ff 100644 --- a/dicebot/src/rpc.rs +++ b/dicebot/src/rpc.rs @@ -1,15 +1,27 @@ -use std::sync::Arc; - +use crate::db::{errors::DataError, Variables}; use crate::error::BotError; use crate::{config::Config, db::sqlite::Database}; use log::info; +use std::sync::Arc; use tenebrous_rpc::protos::dicebot::{ dicebot_server::{Dicebot, DicebotServer}, GetAllVariablesReply, GetAllVariablesRequest, RoomsListReply, SetVariableReply, SetVariableRequest, UserIdRequest, }; use tenebrous_rpc::protos::dicebot::{GetVariableReply, GetVariableRequest}; -use tonic::{transport::Server, Request, Response, Status}; +use tonic::{transport::Server, Code, Request, Response, Status}; + +impl From for Status { + fn from(error: BotError) -> Status { + Status::new(Code::Internal, error.to_string()) + } +} + +impl From for Status { + fn from(error: DataError) -> Status { + Status::new(Code::Internal, error.to_string()) + } +} pub struct DicebotRpcService { config: Arc, @@ -29,7 +41,13 @@ impl Dicebot for DicebotRpcService { &self, request: Request, ) -> Result, Status> { - Ok(Response::new(GetVariableReply { value: 1 })) + let request = request.into_inner(); + let value = self + .db + .get_user_variable(&request.user_id, &request.room_id, &request.variable_name) + .await?; + + Ok(Response::new(GetVariableReply { value })) } async fn get_all_variables(