Merge branch 'avatar_cache'
commit
c4b1d3bc44
|
@ -781,20 +781,20 @@ impl Client {
|
||||||
|
|
||||||
/// Gets the avatar of the owner of the client, if set.
|
/// Gets the avatar of the owner of the client, if set.
|
||||||
///
|
///
|
||||||
/// Returns the avatar. No guarantee on the size of the image is given.
|
/// Returns the avatar.
|
||||||
/// If no size is given the full-sized avatar will be returned.
|
/// If a thumbnail is requested no guarantee on the size of the image is
|
||||||
|
/// given.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `width` - The desired width of the avatar.
|
/// * `format` - The desired format of the avatar.
|
||||||
///
|
|
||||||
/// * `height` - The desired height of the avatar.
|
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # use futures::executor::block_on;
|
/// # use futures::executor::block_on;
|
||||||
/// # use matrix_sdk::Client;
|
/// # use matrix_sdk::Client;
|
||||||
/// # use matrix_sdk::identifiers::room_id;
|
/// # use matrix_sdk::identifiers::room_id;
|
||||||
|
/// # use matrix_sdk::media::MediaFormat;
|
||||||
/// # use url::Url;
|
/// # use url::Url;
|
||||||
/// # let homeserver = Url::parse("http://example.com").unwrap();
|
/// # let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
/// # block_on(async {
|
/// # block_on(async {
|
||||||
|
@ -802,24 +802,15 @@ impl Client {
|
||||||
/// let client = Client::new(homeserver).unwrap();
|
/// let client = Client::new(homeserver).unwrap();
|
||||||
/// client.login(user, "password", None, None).await.unwrap();
|
/// client.login(user, "password", None, None).await.unwrap();
|
||||||
///
|
///
|
||||||
/// if let Some(avatar) = client.avatar(Some(96), Some(96)).await.unwrap() {
|
/// if let Some(avatar) = client.avatar(MediaFormat::File).await.unwrap() {
|
||||||
/// std::fs::write("avatar.png", avatar);
|
/// std::fs::write("avatar.png", avatar);
|
||||||
/// }
|
/// }
|
||||||
/// # })
|
/// # })
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn avatar(&self, width: Option<u32>, height: Option<u32>) -> Result<Option<Vec<u8>>> {
|
pub async fn avatar(&self, format: MediaFormat) -> Result<Option<Vec<u8>>> {
|
||||||
// TODO: try to offer the avatar from cache, requires avatar cache
|
|
||||||
if let Some(url) = self.avatar_url().await? {
|
if let Some(url) = self.avatar_url().await? {
|
||||||
if let (Some(width), Some(height)) = (width, height) {
|
let request = MediaRequest { media_type: MediaType::Uri(url), format };
|
||||||
let request =
|
Ok(Some(self.get_media_content(&request, true).await?))
|
||||||
get_content_thumbnail::Request::from_url(&url, width.into(), height.into())?;
|
|
||||||
let response = self.send(request, None).await?;
|
|
||||||
Ok(Some(response.file))
|
|
||||||
} else {
|
|
||||||
let request = get_content::Request::from_url(&url)?;
|
|
||||||
let response = self.send(request, None).await?;
|
|
||||||
Ok(Some(response.file))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,14 +4,16 @@ use matrix_sdk_base::deserialized_responses::MembersResponse;
|
||||||
use matrix_sdk_common::locks::Mutex;
|
use matrix_sdk_common::locks::Mutex;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::client::r0::{
|
api::client::r0::{
|
||||||
media::{get_content, get_content_thumbnail},
|
|
||||||
membership::{get_member_events, join_room_by_id, leave_room},
|
membership::{get_member_events, join_room_by_id, leave_room},
|
||||||
message::get_message_events,
|
message::get_message_events,
|
||||||
},
|
},
|
||||||
UserId,
|
UserId,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{BaseRoom, Client, Result, RoomMember};
|
use crate::{
|
||||||
|
media::{MediaFormat, MediaRequest, MediaType},
|
||||||
|
BaseRoom, Client, Result, RoomMember,
|
||||||
|
};
|
||||||
|
|
||||||
/// A struct containing methods that are common for Joined, Invited and Left
|
/// A struct containing methods that are common for Joined, Invited and Left
|
||||||
/// Rooms
|
/// Rooms
|
||||||
|
@ -63,20 +65,20 @@ impl Common {
|
||||||
|
|
||||||
/// Gets the avatar of this room, if set.
|
/// Gets the avatar of this room, if set.
|
||||||
///
|
///
|
||||||
/// Returns the avatar. No guarantee on the size of the image is given.
|
/// Returns the avatar.
|
||||||
/// If no size is given the full-sized avatar will be returned.
|
/// If a thumbnail is requested no guarantee on the size of the image is
|
||||||
|
/// given.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `width` - The desired width of the avatar.
|
/// * `format` - The desired format of the avatar.
|
||||||
///
|
|
||||||
/// * `height` - The desired height of the avatar.
|
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # use futures::executor::block_on;
|
/// # use futures::executor::block_on;
|
||||||
/// # use matrix_sdk::Client;
|
/// # use matrix_sdk::Client;
|
||||||
/// # use matrix_sdk::identifiers::room_id;
|
/// # use matrix_sdk::identifiers::room_id;
|
||||||
|
/// # use matrix_sdk::media::MediaFormat;
|
||||||
/// # use url::Url;
|
/// # use url::Url;
|
||||||
/// # let homeserver = Url::parse("http://example.com").unwrap();
|
/// # let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
/// # block_on(async {
|
/// # block_on(async {
|
||||||
|
@ -87,24 +89,15 @@ impl Common {
|
||||||
/// let room = client
|
/// let room = client
|
||||||
/// .get_joined_room(&room_id)
|
/// .get_joined_room(&room_id)
|
||||||
/// .unwrap();
|
/// .unwrap();
|
||||||
/// if let Some(avatar) = room.avatar(Some(96), Some(96)).await.unwrap() {
|
/// if let Some(avatar) = room.avatar(MediaFormat::File).await.unwrap() {
|
||||||
/// std::fs::write("avatar.png", avatar);
|
/// std::fs::write("avatar.png", avatar);
|
||||||
/// }
|
/// }
|
||||||
/// # })
|
/// # })
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn avatar(&self, width: Option<u32>, height: Option<u32>) -> Result<Option<Vec<u8>>> {
|
pub async fn avatar(&self, format: MediaFormat) -> Result<Option<Vec<u8>>> {
|
||||||
// TODO: try to offer the avatar from cache, requires avatar cache
|
|
||||||
if let Some(url) = self.avatar_url() {
|
if let Some(url) = self.avatar_url() {
|
||||||
if let (Some(width), Some(height)) = (width, height) {
|
let request = MediaRequest { media_type: MediaType::Uri(url.clone()), format };
|
||||||
let request =
|
Ok(Some(self.client.get_media_content(&request, true).await?))
|
||||||
get_content_thumbnail::Request::from_url(&url, width.into(), height.into())?;
|
|
||||||
let response = self.client.send(request, None).await?;
|
|
||||||
Ok(Some(response.file))
|
|
||||||
} else {
|
|
||||||
let request = get_content::Request::from_url(&url)?;
|
|
||||||
let response = self.client.send(request, None).await?;
|
|
||||||
Ok(Some(response.file))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
use ruma::api::client::r0::media::{get_content, get_content_thumbnail};
|
use crate::{
|
||||||
|
media::{MediaFormat, MediaRequest, MediaType},
|
||||||
use crate::{BaseRoomMember, Client, Result};
|
BaseRoomMember, Client, Result,
|
||||||
|
};
|
||||||
|
|
||||||
/// The high-level `RoomMember` representation
|
/// The high-level `RoomMember` representation
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -26,14 +27,13 @@ impl RoomMember {
|
||||||
|
|
||||||
/// Gets the avatar of this member, if set.
|
/// Gets the avatar of this member, if set.
|
||||||
///
|
///
|
||||||
/// Returns the avatar. No guarantee on the size of the image is given.
|
/// Returns the avatar.
|
||||||
/// If no size is given the full-sized avatar will be returned.
|
/// If a thumbnail is requested no guarantee on the size of the image is
|
||||||
|
/// given.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `width` - The desired width of the avatar.
|
/// * `format` - The desired format of the avatar.
|
||||||
///
|
|
||||||
/// * `height` - The desired height of the avatar.
|
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
|
@ -41,6 +41,7 @@ impl RoomMember {
|
||||||
/// # use matrix_sdk::Client;
|
/// # use matrix_sdk::Client;
|
||||||
/// # use matrix_sdk::identifiers::room_id;
|
/// # use matrix_sdk::identifiers::room_id;
|
||||||
/// # use matrix_sdk::RoomMember;
|
/// # use matrix_sdk::RoomMember;
|
||||||
|
/// # use matrix_sdk::media::MediaFormat;
|
||||||
/// # use url::Url;
|
/// # use url::Url;
|
||||||
/// # let homeserver = Url::parse("http://example.com").unwrap();
|
/// # let homeserver = Url::parse("http://example.com").unwrap();
|
||||||
/// # block_on(async {
|
/// # block_on(async {
|
||||||
|
@ -53,24 +54,15 @@ impl RoomMember {
|
||||||
/// .unwrap();
|
/// .unwrap();
|
||||||
/// let members = room.members().await.unwrap();
|
/// let members = room.members().await.unwrap();
|
||||||
/// let member = members.first().unwrap();
|
/// let member = members.first().unwrap();
|
||||||
/// if let Some(avatar) = member.avatar(Some(96), Some(96)).await.unwrap() {
|
/// if let Some(avatar) = member.avatar(MediaFormat::File).await.unwrap() {
|
||||||
/// std::fs::write("avatar.png", avatar);
|
/// std::fs::write("avatar.png", avatar);
|
||||||
/// }
|
/// }
|
||||||
/// # })
|
/// # })
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn avatar(&self, width: Option<u32>, height: Option<u32>) -> Result<Option<Vec<u8>>> {
|
pub async fn avatar(&self, format: MediaFormat) -> Result<Option<Vec<u8>>> {
|
||||||
// TODO: try to offer the avatar from cache, requires avatar cache
|
|
||||||
if let Some(url) = self.avatar_url() {
|
if let Some(url) = self.avatar_url() {
|
||||||
if let (Some(width), Some(height)) = (width, height) {
|
let request = MediaRequest { media_type: MediaType::Uri(url.clone()), format };
|
||||||
let request =
|
Ok(Some(self.client.get_media_content(&request, true).await?))
|
||||||
get_content_thumbnail::Request::from_url(url, width.into(), height.into())?;
|
|
||||||
let response = self.client.send(request, None).await?;
|
|
||||||
Ok(Some(response.file))
|
|
||||||
} else {
|
|
||||||
let request = get_content::Request::from_url(url)?;
|
|
||||||
let response = self.client.send(request, None).await?;
|
|
||||||
Ok(Some(response.file))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue