use std::{ops::Deref, sync::Arc};
use async_trait::async_trait;
use ricq::{structs::FriendGroupInfo, RQError};
use thiserror::Error;
use crate::{
client::{friend_list::FetchFriendListError, Client},
meta::selector::{OptionSelector, Selector},
};
box_error_impl!(
FetchFriendGroupError,
FetchFriendGroupErrorImpl,
"获取好友分组错误。"
);
#[derive(Error, Debug)]
enum FetchFriendGroupErrorImpl {
#[error("获取好友列表失败")]
FetchFriendListError(#[from] FetchFriendListError),
}
#[derive(Debug, Clone)]
pub struct FriendGroup {
selector: FriendGroupSelector,
pub id: u8,
pub name: String,
pub friend_count: i32,
pub online_count: i32,
pub seq_id: u8,
}
impl FriendGroup {
pub(crate) fn new(client: &Arc<Client>, info: FriendGroupInfo) -> Self {
Self {
selector: client.friend_group(info.group_id),
id: info.group_id,
name: info.group_name,
friend_count: info.friend_count,
online_count: info.online_friend_count,
seq_id: info.seq_id,
}
}
}
impl Deref for FriendGroup {
type Target = FriendGroupSelector;
fn deref(&self) -> &Self::Target {
&self.selector
}
}
#[derive(Debug, Clone)]
pub struct FriendGroupSelector {
client: Arc<Client>,
pub id: u8,
}
impl FriendGroupSelector {
pub(crate) fn new(client: Arc<Client>, id: u8) -> Self {
Self { client, id }
}
pub async fn delete(&self) -> Result<(), RQError> {
self.client.inner.friend_list_del_group(self.id).await?;
self.client.friend_list.make_dirty().await;
Ok(())
}
pub async fn rename(&self, new_name: String) -> Result<(), RQError> {
self.client
.inner
.friend_list_rename_group(self.id, new_name)
.await?;
self.client.friend_list.make_dirty().await;
Ok(())
}
}
#[async_trait]
impl Selector for FriendGroupSelector {
type Target = Arc<FriendGroup>;
type Error = FetchFriendGroupError;
async fn flush(&self) -> &Self {
self.client.friend_list.make_dirty().await;
self
}
fn as_client(&self) -> &Arc<Client> {
&self.client
}
}
#[async_trait]
impl OptionSelector for FriendGroupSelector {
async fn fetch(&self) -> Result<Option<Self::Target>, Self::Error> {
Ok(self
.client
.get_friend_list()
.await?
.friend_groups()
.get(&self.id)
.cloned())
}
}