use std::collections::HashMap; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use crate::model::{ instance::Instance, repository::RepositorySummary, settings::Setting, user::User, }; use super::MessageTarget; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserDisplayNameRequest { pub user: User, } impl MessageTarget for UserDisplayNameRequest { fn target(&self) -> Option { Some(self.user.instance.clone()) } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserDisplayNameResponse { pub display_name: Option, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserDisplayImageRequest { pub user: User, } impl MessageTarget for UserDisplayImageRequest { fn target(&self) -> Option { Some(self.user.instance.clone()) } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserDisplayImageResponse { pub image_url: Option, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserBioRequest { pub user: User, } impl MessageTarget for UserBioRequest { fn target(&self) -> Option { Some(self.user.instance.clone()) } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserBioResponse { pub bio: Option, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserRepositoriesRequest { pub instance: Instance, pub user: User, } impl MessageTarget for UserRepositoriesRequest { fn target(&self) -> Option { Some(self.instance.clone()) } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserRepositoriesResponse { pub repositories: Vec, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserSettingsRequest { pub user: User, } impl MessageTarget for UserSettingsRequest { fn target(&self) -> Option { Some(self.user.instance.clone()) } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize)] pub struct UserSettingsResponse { pub settings: HashMap, } impl UserSettingsResponse { pub fn try_get(&self) -> Option { let setting_member = self .settings .iter() .filter(|(key, _)| key.as_str() == S::name()) .next()?; serde_json::from_value(setting_member.1.clone()).ok() } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserWriteSettingsRequest { pub user: User, pub settings: Vec<(String, String)>, } impl MessageTarget for UserWriteSettingsRequest { fn target(&self) -> Option { Some(self.user.instance.clone()) } } impl UserWriteSettingsRequest { pub fn new(user: impl ToOwned) -> Self { Self { user: user.to_owned(), settings: Default::default(), } } pub fn set(&mut self, setting: S) { self.settings.push(( S::name().to_string(), serde_json::to_string(&setting).unwrap(), )); } pub fn try_get(&self) -> Option { let setting_member = self .settings .iter() .filter(|(key, _)| key.as_str() == S::name()) .next()?; serde_json::from_str(&setting_member.1).ok() } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct UserWriteSettingsResponse { // IDK? }