Finished moving to client.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
This commit is contained in:
141
src/lib.rs
141
src/lib.rs
@@ -236,147 +236,6 @@ impl MoreThanText {
|
||||
MTTClient::new(self.queue.clone(), Some(id), lang)
|
||||
}
|
||||
|
||||
// /*
|
||||
fn new_session(lang: Option<Language>) -> ClientAction {
|
||||
let mut output = Addition::new(Session::doc_names()[0].clone());
|
||||
match lang {
|
||||
Some(data) => {
|
||||
let name = Session::language_field_names()[0].clone();
|
||||
let field: Field = data.into();
|
||||
output.add_field(name, field);
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
output.into()
|
||||
}
|
||||
|
||||
fn recursive_message_request<CA>(&mut self, action: CA, lang: Option<Language>) -> Uuid
|
||||
where
|
||||
CA: Into<ClientAction>,
|
||||
{
|
||||
match self.records(action) {
|
||||
Ok(data) => {
|
||||
if data.len() == 0 {
|
||||
self.recursive_message_request(MoreThanText::new_session(lang), lang)
|
||||
} else {
|
||||
let rec = data.iter().last().unwrap();
|
||||
match rec.get(Name::english("id")).unwrap() {
|
||||
Field::Uuid(id) => id,
|
||||
_ => unreachable!("should always return uuid"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => self.recursive_message_request(MoreThanText::new_session(lang), lang),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate_session(&mut self, session: Option<String>, lang: Option<Language>) -> Uuid {
|
||||
let action = match session {
|
||||
Some(data) => match Uuid::try_from(data.as_str()) {
|
||||
Ok(id) => {
|
||||
let mut query = Query::new(Session::doc_names()[0].clone());
|
||||
let mut calc = Calculation::new(Operand::Equal);
|
||||
calc.add_value(CalcValue::Existing(FieldType::Uuid))
|
||||
.unwrap();
|
||||
calc.add_value(id).unwrap();
|
||||
query.add(Name::english("id"), calc);
|
||||
query.into()
|
||||
}
|
||||
Err(_) => MoreThanText::new_session(lang),
|
||||
},
|
||||
None => MoreThanText::new_session(lang),
|
||||
};
|
||||
self.recursive_message_request(action, lang)
|
||||
}
|
||||
|
||||
pub fn records<UA>(&mut self, request: UA) -> Result<Records, MTTError>
|
||||
where
|
||||
UA: Into<ClientAction>,
|
||||
{
|
||||
let req = request.into();
|
||||
let (tx, rx) = channel();
|
||||
let sender_id = self.queue.add_sender(tx);
|
||||
let doc_id = req.doc_name().clone();
|
||||
let msg = Message::new(req);
|
||||
let msg_id = msg.get_message_id();
|
||||
let paths = [
|
||||
Path::new(
|
||||
Include::Just(msg_id.clone()),
|
||||
Include::Just(doc_id.clone()),
|
||||
Include::Just(Action::Records),
|
||||
),
|
||||
Path::new(
|
||||
Include::Just(msg_id.clone()),
|
||||
Include::All,
|
||||
Include::Just(Action::Error),
|
||||
),
|
||||
];
|
||||
for path in paths.iter() {
|
||||
let reg_msg = Register::new(sender_id.clone(), RegMsg::AddRoute(path.clone()));
|
||||
self.queue.send(Message::new(reg_msg));
|
||||
let result = rx.recv().unwrap();
|
||||
let action = result.get_action();
|
||||
match action {
|
||||
MsgAction::Register(status) => match status.get_msg() {
|
||||
RegMsg::Error(err) => {
|
||||
let mut error = err.clone();
|
||||
error.add_parent(ErrorID::Document(msg.doc_name().clone()));
|
||||
self.queue.remove_sender(&sender_id);
|
||||
return Err(error);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => unreachable!("got {:?} should have been a registry message", action),
|
||||
}
|
||||
}
|
||||
self.queue.send(msg);
|
||||
let output = match rx.recv_timeout(TIMEOUT) {
|
||||
Ok(data) => match data.get_action() {
|
||||
MsgAction::Records(data) => Ok(data.clone()),
|
||||
MsgAction::Error(err) => Err(err.clone()),
|
||||
_ => unreachable!("should only receive records or errors"),
|
||||
},
|
||||
Err(_) => Err(MTTError::new(ErrorID::TimeOut)),
|
||||
};
|
||||
self.queue.remove_sender(&sender_id);
|
||||
output
|
||||
}
|
||||
|
||||
pub fn create_document(&mut self, docdef: DocDef) -> Result<(), MTTError> {
|
||||
let (tx, rx) = channel();
|
||||
let sender_id = self.queue.add_sender(tx);
|
||||
let msg = Message::new(docdef);
|
||||
let msg_id = msg.get_message_id();
|
||||
let paths = [
|
||||
Path::new(
|
||||
Include::Just(msg_id.clone()),
|
||||
Include::All,
|
||||
Include::Just(Action::DocumentCreated),
|
||||
),
|
||||
Path::new(
|
||||
Include::Just(msg_id.clone()),
|
||||
Include::All,
|
||||
Include::Just(Action::Error),
|
||||
),
|
||||
];
|
||||
for path in paths.iter() {
|
||||
let reg_msg = Register::new(sender_id.clone(), RegMsg::AddRoute(path.clone()));
|
||||
self.queue.send(Message::new(reg_msg));
|
||||
rx.recv().unwrap(); // Wait for completion.
|
||||
}
|
||||
self.queue.send(msg);
|
||||
let output = match rx.recv_timeout(TIMEOUT) {
|
||||
Ok(data) => match data.get_action() {
|
||||
MsgAction::DocumentCreated => Ok(()),
|
||||
MsgAction::Error(err) => Err(err.clone()),
|
||||
_ => unreachable!("should only receive records or errors"),
|
||||
},
|
||||
Err(_) => Err(MTTError::new(ErrorID::TimeOut)),
|
||||
};
|
||||
self.queue.remove_sender(&sender_id);
|
||||
output
|
||||
}
|
||||
// */
|
||||
pub fn get_document(&self, name: &str, id: &str) -> Result<String, MTTError> {
|
||||
if name == "page" {
|
||||
Ok("something".to_string())
|
||||
|
||||
13
src/main.rs
13
src/main.rs
@@ -55,7 +55,7 @@ async fn create_app(state: MoreThanText) -> Router {
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Clone)]
|
||||
struct SessionID(Uuid);
|
||||
struct SessionID(String);
|
||||
|
||||
impl<S> FromRequestParts<S> for SessionID
|
||||
where
|
||||
@@ -73,9 +73,11 @@ where
|
||||
let requested = req_id.clone();
|
||||
let (tx, mut rx) = channel(1);
|
||||
spawn(async move {
|
||||
tx.send(state.validate_session(requested, None))
|
||||
.await
|
||||
.unwrap();
|
||||
let id = match requested {
|
||||
Some(data) => state.client_with_session(data, None).session_id(),
|
||||
None => state.client().session_id(),
|
||||
};
|
||||
tx.send(id).await.unwrap();
|
||||
});
|
||||
let id = rx.recv().await.unwrap();
|
||||
if !req_id.is_some_and(|x| x == id.to_string()) {
|
||||
@@ -86,7 +88,7 @@ where
|
||||
}
|
||||
|
||||
async fn mtt_conn(
|
||||
_sess_id: SessionID,
|
||||
sess_id: SessionID,
|
||||
method: Method,
|
||||
path: Path<HashMap<String, String>>,
|
||||
state: State<MoreThanText>,
|
||||
@@ -94,6 +96,7 @@ async fn mtt_conn(
|
||||
) -> impl IntoResponse {
|
||||
let (tx, mut rx) = channel(1);
|
||||
spawn(async move {
|
||||
let client = state.client_with_session(sess_id.0, None);
|
||||
match method {
|
||||
Method::GET => match path.get("document") {
|
||||
Some(doc) => tx.send(state.get_document(doc, "home")).await.unwrap(),
|
||||
|
||||
Reference in New Issue
Block a user