96 lines
2.7 KiB
Rust
96 lines
2.7 KiB
Rust
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, Code, Request, Response, Status};
|
|
|
|
impl From<BotError> for Status {
|
|
fn from(error: BotError) -> Status {
|
|
Status::new(Code::Internal, error.to_string())
|
|
}
|
|
}
|
|
|
|
impl From<DataError> for Status {
|
|
fn from(error: DataError) -> Status {
|
|
Status::new(Code::Internal, error.to_string())
|
|
}
|
|
}
|
|
|
|
pub struct DicebotRpcService {
|
|
config: Arc<Config>,
|
|
db: Database,
|
|
}
|
|
|
|
#[tonic::async_trait]
|
|
impl Dicebot for DicebotRpcService {
|
|
async fn set_variable(
|
|
&self,
|
|
request: Request<SetVariableRequest>,
|
|
) -> Result<Response<SetVariableReply>, Status> {
|
|
Ok(Response::new(SetVariableReply { success: true }))
|
|
}
|
|
|
|
async fn get_variable(
|
|
&self,
|
|
request: Request<GetVariableRequest>,
|
|
) -> Result<Response<GetVariableReply>, Status> {
|
|
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(
|
|
&self,
|
|
request: Request<GetAllVariablesRequest>,
|
|
) -> Result<Response<GetAllVariablesReply>, Status> {
|
|
Ok(Response::new(GetAllVariablesReply {
|
|
variables: std::collections::HashMap::new(),
|
|
}))
|
|
}
|
|
|
|
async fn rooms_for_user(
|
|
&self,
|
|
request: Request<UserIdRequest>,
|
|
) -> Result<Response<RoomsListReply>, Status> {
|
|
Ok(Response::new(RoomsListReply {
|
|
room_ids: Vec::new(),
|
|
}))
|
|
}
|
|
}
|
|
|
|
pub async fn serve_grpc(config: &Arc<Config>, db: &Database) -> Result<(), BotError> {
|
|
match config.rpc_addr() {
|
|
Some(addr) => {
|
|
let addr = addr.parse()?;
|
|
let rpc_service = DicebotRpcService {
|
|
db: db.clone(),
|
|
config: config.clone(),
|
|
};
|
|
|
|
info!("Serving Dicebot gRPC service on {}", addr);
|
|
Server::builder()
|
|
.add_service(DicebotServer::new(rpc_service))
|
|
.serve(addr)
|
|
.await
|
|
.map_err(|e| e.into())
|
|
}
|
|
_ => noop().await,
|
|
}
|
|
}
|
|
|
|
pub async fn noop() -> Result<(), BotError> {
|
|
info!("RPC address not specified. Not enabling gRPC.");
|
|
Ok(())
|
|
}
|