[fractal] Backend: implement PublicRoomsRequest



commit b6bde7e99ca0fdc9a58fc22d1ec804f67114612c
Author: Alejandro Domínguez <adomu net-c com>
Date:   Mon Jan 14 13:18:21 2019 +0100

    Backend: implement PublicRoomsRequest
    
    Previously we built the JSON request for listing the rooms directly. Here we do the same
    by constructing a struct and then serializing it.

 fractal-matrix-api/src/backend/directory.rs | 34 ++++++++++++++++-------------
 fractal-matrix-api/src/model/room.rs        | 31 ++++++++++++++++++++++++++
 fractal-matrix-api/src/types.rs             |  2 ++
 3 files changed, 52 insertions(+), 15 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index 4416c2ff..e6501c14 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -13,9 +13,11 @@ use crate::util::cache_path;
 use crate::util::json_q;
 use crate::util::media;
 
+use crate::types::PublicRoomsRequest;
 use crate::types::PublicRoomsResponse;
 use crate::types::Room;
 use crate::types::SupportedProtocols;
+use crate::types::ThirdPartyNetworks;
 
 pub fn protocols(bk: &Backend) {
     let baseu = bk.get_base_url();
@@ -51,7 +53,7 @@ pub fn protocols(bk: &Backend) {
 pub fn room_search(
     bk: &Backend,
     homeserver: Option<String>,
-    query: Option<String>,
+    filter: Option<String>,
     third_party: Option<String>,
     more: bool,
 ) -> Result<(), Error> {
@@ -69,20 +71,22 @@ pub fn room_search(
     let url = bk.url("publicRooms", params)?;
     let base = bk.get_base_url();
 
-    let mut attrs = json!({ "limit": globals::ROOM_DIRECTORY_LIMIT });
-
-    if let Some(q) = query {
-        attrs["filter"] = json!({ "generic_search_term": q });
-    }
-
-    if let Some(tp) = third_party {
-        attrs["third_party_instance_id"] = json!(tp);
-    }
-
-    if more {
-        let since = bk.data.lock().unwrap().rooms_since.clone();
-        attrs["since"] = json!(since);
-    }
+    let since = if more {
+        Some(bk.data.lock().unwrap().rooms_since.clone())
+    } else {
+        None
+    };
+
+    let request = PublicRoomsRequest {
+        limit: Some(globals::ROOM_DIRECTORY_LIMIT),
+        filter,
+        since,
+        third_party_networks: third_party
+            .map(|tp| ThirdPartyNetworks::Only(tp))
+            .unwrap_or_default(),
+    };
+
+    let attrs = serde_json::to_value(request).expect("Failed to serialize the search request");
 
     let tx = bk.tx.clone();
     let data = bk.data.clone();
diff --git a/fractal-matrix-api/src/model/room.rs b/fractal-matrix-api/src/model/room.rs
index e3423458..3bdcf6e3 100644
--- a/fractal-matrix-api/src/model/room.rs
+++ b/fractal-matrix-api/src/model/room.rs
@@ -164,6 +164,37 @@ impl PartialEq for Room {
 
 pub type RoomList = HashMap<String, Room>;
 
+#[derive(Clone, Debug, Serialize)]
+pub struct PublicRoomsRequest {
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub limit: Option<i32>,
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub since: Option<String>,
+    // This field doesn't follow the spec but for some reason
+    // it fails with matrix.org if it's not set this way
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub filter: Option<String>,
+    #[serde(flatten)]
+    pub third_party_networks: ThirdPartyNetworks,
+}
+
+#[derive(Clone, Debug, Serialize)]
+#[serde(tag = "include_all_networks", content = "third_party_instance_id")]
+pub enum ThirdPartyNetworks {
+    #[serde(rename = "false")]
+    None,
+    #[serde(rename = "false")]
+    Only(String),
+    #[serde(rename = "true")]
+    All,
+}
+
+impl Default for ThirdPartyNetworks {
+    fn default() -> Self {
+        ThirdPartyNetworks::None
+    }
+}
+
 #[derive(Clone, Debug, Deserialize)]
 pub struct PublicRoomsResponse {
     pub chunk: Vec<PublicRoomsChunk>,
diff --git a/fractal-matrix-api/src/types.rs b/fractal-matrix-api/src/types.rs
index 9bdc699b..0a534e76 100644
--- a/fractal-matrix-api/src/types.rs
+++ b/fractal-matrix-api/src/types.rs
@@ -4,11 +4,13 @@ pub use crate::model::member::MemberList;
 pub use crate::model::message::Message;
 pub use crate::model::protocol::ProtocolInstance;
 pub use crate::model::protocol::SupportedProtocols;
+pub use crate::model::room::PublicRoomsRequest;
 pub use crate::model::room::PublicRoomsResponse;
 pub use crate::model::room::Room;
 pub use crate::model::room::RoomList;
 pub use crate::model::room::RoomMembership;
 pub use crate::model::room::RoomTag;
+pub use crate::model::room::ThirdPartyNetworks;
 pub use crate::model::stickers::Sticker;
 pub use crate::model::stickers::StickerGroup;
 pub use crate::model::userinfo::UserInfo;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]