[fractal] backend: clean up room avatar request
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] backend: clean up room avatar request
- Date: Fri, 1 Feb 2019 09:04:44 +0000 (UTC)
commit 9b1756575be1dbc760fc1318b43c5fc8c3ca80ac
Author: Julian Sparber <julian sparber net>
Date: Tue Dec 25 11:28:37 2018 +0100
backend: clean up room avatar request
We used to request the complete room state to get only the memebers list
if there was no avatar, which was expensive and we actually have already
the members list.
This reduces memory use.
fractal-gtk/src/app/backend_loop.rs | 3 +--
fractal-gtk/src/appop/room.rs | 5 ++--
fractal-matrix-api/src/backend/room.rs | 22 +++------------
fractal-matrix-api/src/backend/types.rs | 2 +-
fractal-matrix-api/src/util.rs | 48 ---------------------------------
5 files changed, 8 insertions(+), 72 deletions(-)
---
diff --git a/fractal-gtk/src/app/backend_loop.rs b/fractal-gtk/src/app/backend_loop.rs
index 0cf4b672..862518b3 100644
--- a/fractal-gtk/src/app/backend_loop.rs
+++ b/fractal-gtk/src/app/backend_loop.rs
@@ -122,8 +122,7 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
APPOP!(set_room_detail, (room, key, v));
}
Ok(BKResponse::RoomAvatar(room, avatar)) => {
- let a = Some(avatar);
- APPOP!(set_room_avatar, (room, a));
+ APPOP!(set_room_avatar, (room, avatar));
}
Ok(BKResponse::RoomMembers(room, members)) => {
APPOP!(set_room_members, (room, members));
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index 96c0e985..81e1d0ce 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -1,5 +1,6 @@
use crate::i18n::{i18n, i18n_k};
use log::{error, warn};
+use url::Url;
use gtk;
use gtk::prelude::*;
@@ -335,9 +336,9 @@ impl AppOp {
}
}
- pub fn set_room_avatar(&mut self, roomid: String, avatar: Option<String>) {
+ pub fn set_room_avatar(&mut self, roomid: String, avatar: Option<Url>) {
if let Some(r) = self.rooms.get_mut(&roomid) {
- r.avatar = avatar.clone();
+ 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 274b1289..64dbc219 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -11,10 +11,8 @@ 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;
@@ -31,8 +29,8 @@ use serde_json::Value as JsonValue;
pub fn set_room(bk: &Backend, id: String) -> Result<(), Error> {
/* FIXME: remove clone and pass id by reference */
- get_room_detail(bk, id.clone(), String::from("m.room.topic"))?;
get_room_avatar(bk, id.clone())?;
+ get_room_detail(bk, id.clone(), String::from("m.room.topic"))?;
get_room_members(bk, id)?;
Ok(())
@@ -58,34 +56,20 @@ pub fn get_room_detail(bk: &Backend, roomid: String, key: String) -> Result<(),
}
pub fn get_room_avatar(bk: &Backend, roomid: String) -> Result<(), Error> {
- let userid = bk.data.lock().unwrap().user_id.clone();
- let baseu = bk.get_base_url();
- let tk = bk.data.lock().unwrap().access_token.clone();
let url = bk.url(&format!("rooms/{}/state/m.room.avatar", roomid), vec![])?;
let tx = bk.tx.clone();
get!(
&url,
|r: JsonValue| {
- let avatar = match r["url"].as_str() {
- Some(u) => {
- if let Ok(dest) = cache_path(&roomid) {
- thumb(&baseu, u, Some(&dest)).unwrap_or_default()
- } else {
- String::new()
- }
- }
- None => util::get_room_avatar(&baseu, &tk, &userid, &roomid).unwrap_or_default(),
- };
+ let avatar = r["url"].as_str().and_then(|s| Url::parse(s).ok());
tx.send(BKResponse::RoomAvatar(roomid, avatar)).unwrap();
},
|err: Error| match err {
Error::MatrixError(ref js)
if js["errcode"].as_str().unwrap_or_default() == "M_NOT_FOUND" =>
{
- let avatar =
- util::get_room_avatar(&baseu, &tk, &userid, &roomid).unwrap_or_default();
- tx.send(BKResponse::RoomAvatar(roomid, avatar)).unwrap();
+ tx.send(BKResponse::RoomAvatar(roomid, None)).unwrap();
}
_ => {
tx.send(BKResponse::RoomAvatarError(err)).unwrap();
diff --git a/fractal-matrix-api/src/backend/types.rs b/fractal-matrix-api/src/backend/types.rs
index 6f2b8149..a18f1c4e 100644
--- a/fractal-matrix-api/src/backend/types.rs
+++ b/fractal-matrix-api/src/backend/types.rs
@@ -106,7 +106,7 @@ pub enum BKResponse {
Rooms(Vec<Room>, Option<Room>),
NewRooms(Vec<Room>),
RoomDetail(String, String, String),
- RoomAvatar(String, String),
+ RoomAvatar(String, Option<Url>),
NewRoomAvatar(String),
RoomMemberEvent(Event),
RoomMessages(Vec<Message>),
diff --git a/fractal-matrix-api/src/util.rs b/fractal-matrix-api/src/util.rs
index 0afc30a2..0f4e481d 100644
--- a/fractal-matrix-api/src/util.rs
+++ b/fractal-matrix-api/src/util.rs
@@ -418,54 +418,6 @@ pub fn get_user_avatar(baseu: &Url, userid: &str) -> Result<(String, String), Er
}
}
-pub fn get_room_st(base: &Url, tk: &str, roomid: &str) -> Result<JsonValue, Error> {
- let url = client_url(
- base,
- &format!("rooms/{}/state", roomid),
- &[("access_token", String::from(tk))],
- )?;
-
- let attrs = json!(null);
- let st = json_q("get", &url, &attrs, globals::TIMEOUT)?;
- Ok(st)
-}
-
-pub fn get_room_avatar(base: &Url, tk: &str, userid: &str, roomid: &str) -> Result<String, Error> {
- let st = get_room_st(base, tk, roomid)?;
- let events = st.as_array().ok_or(Error::BackendError)?;
-
- // we look for members that aren't me
- let filter = |x: &&JsonValue| {
- (x["type"] == "m.room.member"
- && x["content"]["membership"] == "join"
- && x["sender"] != userid)
- };
- let members = events.iter().filter(&filter);
- let mut members2 = events.iter().filter(&filter);
-
- let m1 = match members2.next() {
- Some(m) => m["content"]["avatar_url"].as_str().unwrap_or_default(),
- None => "",
- };
-
- let mut fname = match members.count() {
- 1 => {
- if let Ok(dest) = cache_path(&roomid) {
- media(&base, m1, Some(&dest)).unwrap_or_default()
- } else {
- String::new()
- }
- }
- _ => String::new(),
- };
-
- if fname.is_empty() {
- fname = String::new();
- }
-
- Ok(fname)
-}
-
pub fn build_url(base: &Url, path: &str, params: &[(&str, String)]) -> Result<Url, Error> {
let mut url = base.join(path)?;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]