[fractal] avatar: download, cache and remove room avatar



commit c40553c9d3a5e1d4fed13c24ac88f9ac1c6331fa
Author: Julian Sparber <julian sparber net>
Date:   Wed Jan 23 01:20:26 2019 +0100

    avatar: download, cache and remove room avatar

 fractal-gtk/src/appop/room.rs          | 23 +++++++++++++++++++++++
 fractal-matrix-api/src/backend/room.rs |  8 +++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)
---
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index 81e1d0ce..3c68444c 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -1,5 +1,7 @@
 use crate::i18n::{i18n, i18n_k};
 use log::{error, warn};
+use std::fs::remove_file;
+use std::os::unix::fs;
 use url::Url;
 
 use gtk;
@@ -9,6 +11,7 @@ use crate::appop::AppOp;
 
 use crate::backend;
 use crate::backend::BKCommand;
+use fractal_api::util::cache_path;
 
 use crate::actions;
 use crate::actions::AppState;
@@ -337,7 +340,27 @@ impl AppOp {
     }
 
     pub fn set_room_avatar(&mut self, roomid: String, avatar: Option<Url>) {
+        if avatar.is_none() {
+            if let Ok(dest) = cache_path(&roomid) {
+                let _ = remove_file(dest);
+            }
+        }
         if let Some(r) = self.rooms.get_mut(&roomid) {
+            if avatar.is_none() && r.members.len() == 2 {
+                if let Some(ref uid) = self.uid {
+                    for m in r.members.keys() {
+                        if m != uid {
+                            //FIXME: Find a better solution
+                            // create a symlink from user avatar to room avatar (works only on unix)
+                            if let Ok(source) = cache_path(m) {
+                                if let Ok(dest) = cache_path(&roomid) {
+                                    let _ = fs::symlink(source, dest);
+                                }
+                            }
+                        }
+                    }
+                }
+            }
             r.avatar = avatar.map(|s| s.into_string());
             self.roomlist
                 .set_room_avatar(roomid.clone(), r.avatar.clone());
diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs
index 64dbc219..a1b9bef6 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -11,8 +11,10 @@ use crate::globals;
 use std::thread;
 
 use crate::util;
+use crate::util::cache_path;
 use crate::util::json_q;
 use crate::util::put_media;
+use crate::util::thumb;
 use crate::util::{client_url, media_url};
 
 use crate::backend::types::BKCommand;
@@ -57,12 +59,16 @@ pub fn get_room_detail(bk: &Backend, roomid: String, key: String) -> Result<(),
 
 pub fn get_room_avatar(bk: &Backend, roomid: String) -> Result<(), Error> {
     let url = bk.url(&format!("rooms/{}/state/m.room.avatar", roomid), vec![])?;
-
+    let baseu = bk.get_base_url();
     let tx = bk.tx.clone();
     get!(
         &url,
         |r: JsonValue| {
             let avatar = r["url"].as_str().and_then(|s| Url::parse(s).ok());
+            let dest = cache_path(&roomid).ok();
+            if let Some(ref avatar) = avatar {
+                let _ = thumb(&baseu, avatar.as_str(), dest.as_ref().map(String::as_str));
+            }
             tx.send(BKResponse::RoomAvatar(roomid, avatar)).unwrap();
         },
         |err: Error| match err {


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