use std::ops::Deref;
use ruma::events::room::MediaSource;
use crate::{
media::{MediaFormat, MediaRequest},
BaseRoomMember, Client, Result,
};
#[derive(Debug, Clone)]
pub struct RoomMember {
inner: BaseRoomMember,
pub(crate) client: Client,
}
impl Deref for RoomMember {
type Target = BaseRoomMember;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl RoomMember {
pub(crate) fn new(client: Client, member: BaseRoomMember) -> Self {
Self { inner: member, client }
}
pub async fn avatar(&self, format: MediaFormat) -> Result<Option<Vec<u8>>> {
let Some(url) = self.avatar_url() else { return Ok(None) };
let request = MediaRequest { source: MediaSource::Plain(url.to_owned()), format };
Ok(Some(self.client.media().get_media_content(&request, true).await?))
}
pub async fn ignore(&self) -> Result<()> {
self.client.account().ignore_user(self.inner.user_id()).await
}
pub async fn unignore(&self) -> Result<()> {
self.client.account().unignore_user(self.inner.user_id()).await
}
pub fn is_account_user(&self) -> bool {
match self.client.user_id() {
Some(id) => id == self.inner.user_id(),
None => false,
}
}
pub fn suggested_role_for_power_level(&self) -> RoomMemberRole {
RoomMemberRole::suggested_role_for_power_level(self.power_level())
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
pub enum RoomMemberRole {
Administrator,
Moderator,
User,
}
impl RoomMemberRole {
pub fn suggested_role_for_power_level(power_level: i64) -> Self {
if power_level >= 100 {
Self::Administrator
} else if power_level >= 50 {
Self::Moderator
} else {
Self::User
}
}
pub fn suggested_power_level(&self) -> i64 {
match self {
Self::Administrator => 100,
Self::Moderator => 50,
Self::User => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_suggested_roles() {
assert_eq!(
RoomMemberRole::Administrator,
RoomMemberRole::suggested_role_for_power_level(100)
);
assert_eq!(RoomMemberRole::Moderator, RoomMemberRole::suggested_role_for_power_level(50));
assert_eq!(RoomMemberRole::User, RoomMemberRole::suggested_role_for_power_level(0));
}
#[test]
fn test_unexpected_power_levels() {
assert_eq!(
RoomMemberRole::Administrator,
RoomMemberRole::suggested_role_for_power_level(200)
);
assert_eq!(
RoomMemberRole::Administrator,
RoomMemberRole::suggested_role_for_power_level(101)
);
assert_eq!(RoomMemberRole::Moderator, RoomMemberRole::suggested_role_for_power_level(99));
assert_eq!(RoomMemberRole::Moderator, RoomMemberRole::suggested_role_for_power_level(51));
assert_eq!(RoomMemberRole::User, RoomMemberRole::suggested_role_for_power_level(-1));
assert_eq!(RoomMemberRole::User, RoomMemberRole::suggested_role_for_power_level(-100));
}
#[test]
fn test_default_power_levels() {
assert_eq!(100, RoomMemberRole::Administrator.suggested_power_level());
assert_eq!(50, RoomMemberRole::Moderator.suggested_power_level());
assert_eq!(0, RoomMemberRole::User.suggested_power_level());
assert_eq!(
RoomMemberRole::Administrator,
RoomMemberRole::suggested_role_for_power_level(
RoomMemberRole::Administrator.suggested_power_level()
)
);
assert_eq!(
RoomMemberRole::Moderator,
RoomMemberRole::suggested_role_for_power_level(
RoomMemberRole::Moderator.suggested_power_level()
)
);
assert_eq!(
RoomMemberRole::User,
RoomMemberRole::suggested_role_for_power_level(
RoomMemberRole::User.suggested_power_level()
)
);
}
}