Page MenuHomePhabricator

D10981.id37276.diff
No OneTemporary

D10981.id37276.diff

diff --git a/services/identity/src/websockets/errors.rs b/services/identity/src/websockets/errors.rs
--- a/services/identity/src/websockets/errors.rs
+++ b/services/identity/src/websockets/errors.rs
@@ -7,7 +7,6 @@
)]
pub enum WebsocketError {
InvalidMessage,
- InvalidSearchQuery,
UnauthorizedDevice,
SendError,
SearchError,
diff --git a/services/identity/src/websockets/mod.rs b/services/identity/src/websockets/mod.rs
--- a/services/identity/src/websockets/mod.rs
+++ b/services/identity/src/websockets/mod.rs
@@ -3,15 +3,16 @@
use std::pin::Pin;
use std::sync::Arc;
-use elastic::client::responses::SearchResponse;
+use elastic::client::responses::SearchResponse as ElasticSearchResponse;
use futures::lock::Mutex;
use futures_util::{SinkExt, StreamExt};
use hyper::{Body, Request, Response, StatusCode};
use hyper_tungstenite::tungstenite::Message;
use hyper_tungstenite::HyperWebsocket;
use identity_search_messages::{
- ConnectionInitializationResponse, ConnectionInitializationStatus, Heartbeat,
- IdentitySearchUser, Messages, SearchQuery, SearchResult,
+ ConnectionInitializationResponse, ConnectionInitializationStatus, Failure,
+ Heartbeat, IdentitySearchUser, Messages, SearchMethod, SearchQuery,
+ SearchResponse, SearchResult,
};
use serde::{Deserialize, Serialize};
use tokio::net::TcpListener;
@@ -142,12 +143,13 @@
}
async fn handle_prefix_search(
- prefix: &str,
-) -> Result<String, errors::WebsocketError> {
+ request_id: &str,
+ prefix_request: identity_search_messages::Prefix,
+) -> Result<SearchResult, errors::WebsocketError> {
let prefix_query = Query {
query: Prefix {
prefix: Username {
- username: prefix.trim().to_string(),
+ username: prefix_request.prefix.trim().to_string(),
},
},
};
@@ -157,13 +159,16 @@
let search_response = send_search_request(&opensearch_url, prefix_query)
.await?
- .json::<SearchResponse<IdentitySearchUser>>()
+ .json::<ElasticSearchResponse<IdentitySearchUser>>()
.await?;
let usernames: Vec<IdentitySearchUser> =
search_response.into_documents().collect();
- let search_result = serde_json::to_string(&SearchResult { hits: usernames })?;
+ let search_result = SearchResult {
+ id: request_id.to_string(),
+ hits: usernames,
+ };
Ok(search_result)
}
@@ -181,18 +186,24 @@
debug!("Received heartbeat");
Ok(())
}
- Messages::SearchQuery(search_request) => {
- let search_result = match search_request.search_method {
- SearchMethod::Prefix(prefix_request) => {
- handle_prefix_search(&prefix_request.prefix).await
+ Messages::SearchQuery(search_query) => {
+ let handler_result = match search_query.search_method {
+ SearchMethod::Prefix(prefix_query) => {
+ handle_prefix_search(&search_query.id, prefix_query).await
}
- }?;
+ };
+
+ let search_response = match handler_result {
+ Ok(search_result) => SearchResponse::Success(search_result),
+ Err(e) => SearchResponse::Error(Failure {
+ id: search_query.id,
+ error: e.to_string(),
+ }),
+ };
+
+ let serialized_message = serde_json::to_string(&search_response)?;
- send_message(
- Message::Text(format!("{}", search_result.to_string())),
- outgoing.clone(),
- )
- .await;
+ send_message(Message::Text(serialized_message), outgoing.clone()).await;
Ok(())
}
diff --git a/shared/identity_search_messages/src/messages/mod.rs b/shared/identity_search_messages/src/messages/mod.rs
--- a/shared/identity_search_messages/src/messages/mod.rs
+++ b/shared/identity_search_messages/src/messages/mod.rs
@@ -2,11 +2,11 @@
pub mod auth_messages;
pub mod search_query;
-pub mod search_result;
+pub mod search_response;
pub use auth_messages::*;
pub use search_query::*;
-pub use search_result::*;
+pub use search_response::*;
use serde::{Deserialize, Serialize};
pub use websocket_messages::{
@@ -20,5 +20,5 @@
SearchQuery(SearchQuery),
Heartbeat(Heartbeat),
ConnectionInitializationResponse(ConnectionInitializationResponse),
- SearchResult(SearchResult),
+ SearchResponse(SearchResponse),
}
diff --git a/shared/identity_search_messages/src/messages/search_query.rs b/shared/identity_search_messages/src/messages/search_query.rs
--- a/shared/identity_search_messages/src/messages/search_query.rs
+++ b/shared/identity_search_messages/src/messages/search_query.rs
@@ -16,5 +16,6 @@
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct SearchQuery {
+ pub id: String,
pub search_method: SearchMethod,
}
diff --git a/shared/identity_search_messages/src/messages/search_result.rs b/shared/identity_search_messages/src/messages/search_response.rs
rename from shared/identity_search_messages/src/messages/search_result.rs
rename to shared/identity_search_messages/src/messages/search_response.rs
--- a/shared/identity_search_messages/src/messages/search_result.rs
+++ b/shared/identity_search_messages/src/messages/search_response.rs
@@ -2,6 +2,12 @@
use serde::{Deserialize, Serialize};
+#[derive(Debug, Serialize, Deserialize)]
+pub struct Failure {
+ pub id: String,
+ pub error: String,
+}
+
#[derive(Debug, Serialize, Deserialize)]
pub struct IdentitySearchUser {
#[serde(rename = "userID")]
@@ -10,7 +16,14 @@
}
#[derive(Debug, Serialize, Deserialize)]
-#[serde(tag = "type")]
pub struct SearchResult {
+ pub id: String,
pub hits: Vec<IdentitySearchUser>,
}
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(tag = "type", content = "data")]
+pub enum SearchResponse {
+ Success(SearchResult),
+ Error(Failure),
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Dec 19, 4:59 AM (19 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2675373
Default Alt Text
D10981.id37276.diff (5 KB)

Event Timeline