[fractal] BKResponse, media: remove error variants and compose over Result instead



commit b3e487b559728921472cde52f9c307b5cc6bfe73
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sun Aug 25 23:38:02 2019 +0200

    BKResponse, media: remove error variants and compose over Result instead

 fractal-gtk/src/app/backend_loop.rs     |   2 +-
 fractal-matrix-api/src/backend/media.rs | 109 ++++++++++----------------------
 fractal-matrix-api/src/backend/mod.rs   |  23 ++-----
 fractal-matrix-api/src/backend/types.rs |   4 +-
 4 files changed, 41 insertions(+), 97 deletions(-)
---
diff --git a/fractal-gtk/src/app/backend_loop.rs b/fractal-gtk/src/app/backend_loop.rs
index 73526692..d3ec32ef 100644
--- a/fractal-gtk/src/app/backend_loop.rs
+++ b/fractal-gtk/src/app/backend_loop.rs
@@ -167,7 +167,7 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
                 Ok(BKResponse::RoomMemberEvent(ev)) => {
                     APPOP!(room_member_event, (ev));
                 }
-                Ok(BKResponse::Media(fname)) => {
+                Ok(BKResponse::Media(Ok(fname))) => {
                     Command::new("xdg-open")
                         .arg(&fname)
                         .spawn()
diff --git a/fractal-matrix-api/src/backend/media.rs b/fractal-matrix-api/src/backend/media.rs
index f4a6b6e1..58168626 100644
--- a/fractal-matrix-api/src/backend/media.rs
+++ b/fractal-matrix-api/src/backend/media.rs
@@ -21,38 +21,23 @@ use crate::util::ResultExpectLog;
 use crate::r0::filter::RoomEventFilter;
 use crate::types::Message;
 
-pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
+pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) {
     let baseu = bk.get_base_url();
 
     semaphore(bk.limit_threads.clone(), move || {
-        match dw_media(&baseu, &media, ContentType::default_thumbnail(), None) {
-            Ok(fname) => {
-                tx.send(fname).expect_log("Connection closed");
-            }
-            Err(_) => {
-                tx.send(String::new()).expect_log("Connection closed");
-            }
-        };
+        let fname =
+            dw_media(&baseu, &media, ContentType::default_thumbnail(), None).unwrap_or_default();
+        tx.send(fname).expect_log("Connection closed");
     });
-
-    Ok(())
 }
 
-pub fn get_media_async(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
+pub fn get_media_async(bk: &Backend, media: String, tx: Sender<String>) {
     let baseu = bk.get_base_url();
 
     semaphore(bk.limit_threads.clone(), move || {
-        match dw_media(&baseu, &media, ContentType::Download, None) {
-            Ok(fname) => {
-                tx.send(fname).expect_log("Connection closed");
-            }
-            Err(_) => {
-                tx.send(String::new()).expect_log("Connection closed");
-            }
-        };
+        let fname = dw_media(&baseu, &media, ContentType::Download, None).unwrap_or_default();
+        tx.send(fname).expect_log("Connection closed");
     });
-
-    Ok(())
 }
 
 pub fn get_media_list_async(
@@ -61,84 +46,54 @@ pub fn get_media_list_async(
     first_media_id: Option<String>,
     prev_batch: Option<String>,
     tx: Sender<(Vec<Message>, String)>,
-) -> Result<(), Error> {
+) {
     let baseu = bk.get_base_url();
     let tk = bk.data.lock().unwrap().access_token.clone();
     let room = String::from(roomid);
 
-    semaphore(bk.limit_threads.clone(), move || match get_room_media_list(
-        &baseu,
-        &tk,
-        &room,
-        globals::PAGE_LIMIT,
-        first_media_id,
-        &prev_batch,
-    ) {
-        Ok(media_list) => {
-            tx.send(media_list).expect_log("Connection closed");
-        }
-        Err(_) => {
-            tx.send((Vec::new(), String::new()))
-                .expect_log("Connection closed");
-        }
+    semaphore(bk.limit_threads.clone(), move || {
+        let media_list = get_room_media_list(
+            &baseu,
+            &tk,
+            &room,
+            globals::PAGE_LIMIT,
+            first_media_id,
+            &prev_batch,
+        )
+        .unwrap_or_default();
+        tx.send(media_list).expect_log("Connection closed");
     });
-
-    Ok(())
 }
 
-pub fn get_media(bk: &Backend, media: String) -> Result<(), Error> {
+pub fn get_media(bk: &Backend, media: String) {
     let baseu = bk.get_base_url();
 
     let tx = bk.tx.clone();
     thread::spawn(move || {
-        match dw_media(&baseu, &media, ContentType::Download, None) {
-            Ok(fname) => {
-                tx.send(BKResponse::Media(fname))
-                    .expect_log("Connection closed");
-            }
-            Err(err) => {
-                tx.send(BKResponse::MediaError(err))
-                    .expect_log("Connection closed");
-            }
-        };
+        let fname = dw_media(&baseu, &media, ContentType::Download, None);
+        tx.send(BKResponse::Media(fname))
+            .expect_log("Connection closed");
     });
-
-    Ok(())
 }
 
-pub fn get_media_url(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
+pub fn get_media_url(bk: &Backend, media: String, tx: Sender<String>) {
     let baseu = bk.get_base_url();
 
     semaphore(bk.limit_threads.clone(), move || {
-        match resolve_media_url(&baseu, &media, ContentType::Download) {
-            Ok(uri) => {
-                tx.send(uri.to_string()).expect_log("Connection closed");
-            }
-            Err(_) => {
-                tx.send(String::new()).expect_log("Connection closed");
-            }
-        };
+        let uri = resolve_media_url(&baseu, &media, ContentType::Download)
+            .map(Url::into_string)
+            .unwrap_or_default();
+        tx.send(uri).expect_log("Connection closed");
     });
-
-    Ok(())
 }
 
 pub fn get_file_async(url: String, tx: Sender<String>) -> Result<(), Error> {
-    let fname;
-    {
-        let name = url.split('/').last().unwrap_or_default();
-        fname = cache_dir_path(Some("files"), name)?.clone();
-    }
+    let name = url.split('/').last().unwrap_or_default();
+    let fname = cache_dir_path(Some("files"), name)?;
 
     thread::spawn(move || {
-        match download_file(&url, fname, None) {
-            Ok(fname) => {
-                tx.send(fname).expect_log("Connection closed");
-            }
-            Err(_) => {
-                tx.send(String::new()).expect_log("Connection closed");
-            }
-        };
+        let fname = download_file(&url, fname, None).unwrap_or_default();
+        tx.send(fname).expect_log("Connection closed");
     });
 
     Ok(())
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index 4d178b22..c98f2bee 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -242,29 +242,18 @@ impl Backend {
             }
 
             // Media module
-            Ok(BKCommand::GetThumbAsync(media, ctx)) => {
-                let r = media::get_thumb_async(self, media, ctx);
-                bkerror!(r, tx, BKResponse::CommandError);
-            }
-            Ok(BKCommand::GetMediaAsync(media, ctx)) => {
-                let r = media::get_media_async(self, media, ctx);
-                bkerror!(r, tx, BKResponse::CommandError);
-            }
+            Ok(BKCommand::GetThumbAsync(media, ctx)) => media::get_thumb_async(self, media, ctx),
+            Ok(BKCommand::GetMediaAsync(media, ctx)) => media::get_media_async(self, media, ctx),
             Ok(BKCommand::GetMediaListAsync(roomid, first_media_id, prev_batch, ctx)) => {
-                let r = media::get_media_list_async(self, &roomid, first_media_id, prev_batch, ctx);
-                bkerror!(r, tx, BKResponse::CommandError);
-            }
-            Ok(BKCommand::GetMedia(media)) => {
-                let r = media::get_media(self, media);
-                bkerror!(r, tx, BKResponse::CommandError);
+                media::get_media_list_async(self, &roomid, first_media_id, prev_batch, ctx)
             }
+            Ok(BKCommand::GetMedia(media)) => media::get_media(self, media),
             Ok(BKCommand::GetMediaUrl(media, ctx)) => {
-                let r = media::get_media_url(self, media.to_string(), ctx);
-                bkerror!(r, tx, BKResponse::CommandError);
+                media::get_media_url(self, media.to_string(), ctx)
             }
             Ok(BKCommand::GetFileAsync(url, ctx)) => {
                 let r = media::get_file_async(url, ctx);
-                bkerror!(r, tx, BKResponse::CommandError);
+                bkerror!(r, tx, BKResponse::GetFileAsyncError);
             }
 
             // Directory module
diff --git a/fractal-matrix-api/src/backend/types.rs b/fractal-matrix-api/src/backend/types.rs
index 2fea1f52..709723e7 100644
--- a/fractal-matrix-api/src/backend/types.rs
+++ b/fractal-matrix-api/src/backend/types.rs
@@ -127,7 +127,7 @@ pub enum BKResponse {
     SetRoomAvatar,
     RoomName(String, String),
     RoomTopic(String, String),
-    Media(String),
+    Media(Result<String, Error>),
     MediaUrl(Url),
     AttachedFile(Message),
     NewRoom(Room, String),
@@ -162,6 +162,7 @@ pub enum BKResponse {
     SendTypingError(Error),
     SetRoomError(Error),
     CommandError(Error),
+    GetFileAsyncError(Error),
     JoinRoomError(Error),
     MarkAsReadError(Error),
     LeaveRoomError(Error),
@@ -169,7 +170,6 @@ pub enum BKResponse {
     SetRoomTopicError(Error),
     SetRoomAvatarError(Error),
     GetRoomAvatarError(Error),
-    MediaError(Error),
     AttachFileError(Error),
     NewRoomError(Error, String),
     AddToFavError(Error),


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